Question

Digging with custom lock to lock and unlock the device, same as Start app, so far I Google and got some question but no luck even I found one similar question this but no hope.

Yet I tried and use setOnTouchListener to slide but its glitch and not smoothly slide as in Start app.

Note: My code is somewhat similar to Custom Slide to unlock

I'm just looking for same functionality of sliding to lock and unlock the device.

Your suggestion are appreciable.

Était-ce utile?

La solution

Wow, I'm happy, I got my answer myself. Here is the code of my difficulty.

Note: Use 9patch image to set in image view

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:

                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:
                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:

                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;
}   }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top