Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
184 views
in Technique[技术] by (71.8m points)

android - Custom Slide to unlock

Hi I am trying to develop a custom lock screen in which I want to replace the slide to unlock with a ImageView as shown in the image.

enter image description here

This what I have tried so far.

I have placed an Image in the left corner of the screen and used onTouchListner to drag the Image horizontally Code below.

left_Locker.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                int eid = event.getAction();
                switch (eid) {
                case MotionEvent.ACTION_DOWN:
                    toastText.setVisibility(View.VISIBLE);
                    toastText.setTextColor(Color.BLACK);
                    toastText.setText("Slide to Unlock");
                    break;
                case MotionEvent.ACTION_MOVE:
                    RelativeLayout.LayoutParams mParams = (RelativeLayout.LayoutParams) left_Locker.getLayoutParams();
                    int x = (int) event.getRawX();
                    mParams.leftMargin = x - 50;
                    left_Locker.setLayoutParams(mParams);
                    break;
                case MotionEvent.ACTION_UP:
                        toastText.setVisibility(View.GONE);
                        break;
                default:
                    break;
                }
                return true;
            }
        });

The Image does move horizontally but What I am looking is to get the Image background also to drag as shown in the picture above. Am I on the right track by using a ImageView ??

Below is the Image what I have tried.

enter image description here

I can move the Image horizontally but how to get the background as I scroll ??

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I do some modification in your code and I achieved.

Below is the code to slide image at the end of the screen to both side (Left & Right).

For this you have to use 9patch image for both side which you want to slide.

public class MainActivity extends Activity implements OnTouchListener {

ImageView left,right;
int leftPosition,rightPosition;
boolean getSize = false;
int width;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    left = (ImageView)findViewById(R.id.left);
    right = (ImageView)findViewById(R.id.right);

    leftPosition = left.getRight();
    rightPosition = right.getLeft();

    width = getWindowManager().getDefaultDisplay().getWidth();

    left.setOnTouchListener(new OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {
            int eid = event.getAction();
            switch (eid) {
            case MotionEvent.ACTION_DOWN:
               /* toastText.setVisibility(View.VISIBLE);
                toastText.setTextColor(Color.BLACK);
                toastText.setText("Slide to Unlock");*/

                if(!getSize)
                {
                    leftPosition = left.getRight();
                    rightPosition = right.getLeft();
                    getSize =true;
                }
                break;

            case MotionEvent.ACTION_MOVE:

                RelativeLayout.LayoutParams mParams = (RelativeLayout.LayoutParams) left.getLayoutParams();
                int x = (int) event.getRawX();

                if (x>leftPosition) 
                    mParams.width = x;
                left.setLayoutParams(mParams);
                break;

            case MotionEvent.ACTION_UP:

                RelativeLayout.LayoutParams mParam = (RelativeLayout.LayoutParams) left.getLayoutParams();
                mParam.width = leftPosition;
                left.setLayoutParams(mParam);
                break;

            default:
                break;
            }
            return true;
        }
    });


    right.setOnTouchListener(new OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {
            int eid = event.getAction();
            switch (eid) {
            case MotionEvent.ACTION_DOWN:
               /* toastText.setVisibility(View.VISIBLE);
                toastText.setTextColor(Color.BLACK);
                toastText.setText("Slide to Unlock");*/
                break;

            case MotionEvent.ACTION_MOVE:

                RelativeLayout.LayoutParams mParams = (RelativeLayout.LayoutParams) right.getLayoutParams();
                int x = (int) event.getRawX();

                if (x<rightPosition) 
                    mParams.width = width-x;
                right.setLayoutParams(mParams);
                break;

            case MotionEvent.ACTION_UP:
                    //toastText.setVisibility(View.GONE);
                RelativeLayout.LayoutParams mParam = (RelativeLayout.LayoutParams) right.getLayoutParams();
                mParam.width = width - rightPosition;
                right.setLayoutParams(mParam);
                break;
            default:
                break;
            }
            return true;
        }
    });
}
@Override
public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub
    return false;
}   }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...