自定义滑动解锁

时间:2013-10-29 03:16:50

标签: android android-imageview android-sliding

您好我正在尝试开发一个自定义锁定屏幕,我想在其中替换幻灯片以使用ImageView解锁,如图所示。

enter image description here

这是我到目前为止所尝试过的。

我在屏幕左下角放置了一个图像,并使用onTouchListner将图像水平拖动到下面。

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;
            }
        });

图像确实水平移动但我正在寻找的是如图所示也可以拖动图像背景。我使用ImageView吗?

在正确的轨道上

以下是我尝试过的图片。

enter image description here

我可以水平移动图像但是在滚动时如何获取背景

2 个答案:

答案 0 :(得分:1)

我在你的代码中做了一些修改,我实现了。

以下是将屏幕末尾的图像滑动到两侧(Left & Right)的代码。

为此,您必须对要滑动的两侧使用9patch图像。

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;
}   }

答案 1 :(得分:0)

我认为你应该尝试this之类的东西。

OR

还有另一种方法。使用相对布局在搜索栏下方设置背景图像,并使其进度条透明,并以拇指图像。

接下来将图像宽度设置为与搜索栏进度成比例

OR 可能还有另一个解决方案,你可以在你的imageview上使用ontouchlistener并且只改变x并且每次都保持y相同并改变背景,就像上面的方法一样。