如何在布局视图中保留X和Y?

时间:2014-02-03 05:08:49

标签: java android android-framelayout motionevent

我有一个FrameLayout,它有一个用户可以拖动的拇指图像。

拇指宽度为10dp,高度为10dp。

    f = (FrameLayout) findViewById(R.id.fl);
    f.setOnTouchListener(flt);

    f.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
    width = f.getMeasuredWidth();
    height = f.getMeasuredHeight();

@Override
        public boolean onTouch(View v, MotionEvent event) {

            float x = event.getX();
            float y = event.getY();

            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (x<0) {
                    x = x + 10;
                    iv.setX(x);
                    iv.setY(y);
                }
                if (x>f.getWidth()) {
                    x = x - 10;
                    iv.setX(x);
                    iv.setY(y);
                }
                else {
                    iv.setX(x);
                    iv.setY(y);
                }
                // Write your code to perform an action on down
                break;
            case MotionEvent.ACTION_MOVE:
                if (x<0) {
                    x = x + 10;
                    iv.setX(x);
                    iv.setY(y);
                }
                if (x>f.getWidth()) {
                    x = x - 10;
                    iv.setX(x);
                    iv.setY(y);
                }
                else {
                    iv.setX(x);
                    iv.setY(y);
                }
                // Write your code to perform an action on contineus touch move
                break;
            case MotionEvent.ACTION_UP:
                // Write your code to perform an action on touch up
                break;
        }
            // TODO Auto-generated method stub
            return true;
        }

我的目标是,如果使用拖动左侧视图外的框,则拇指按x + 10保持在视图中,如果用户向视图外侧拖动,则拇指按x-10保持在视野内。但是如果我在FrameLayout外面向左和向右拖动,拇指就会消失。

这是我的XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:padding="20dp"
    android:id="@+id/ll"
    android:background="#000000" >

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/palette2"
        android:id="@+id/fl" >

        <ImageView
            android:id="@+id/iv"
            android:layout_width="10dp"
            android:layout_height="10dp"
            android:src="@drawable/esquare" />

    </FrameLayout>
</LinearLayout>

如何修改代码以便达到结果?

1 个答案:

答案 0 :(得分:1)

您是否尝试过https://stackoverflow.com/a/9112808/663370的代码?

你让图像离开边界然后试图纠正它。首先,不要让它超越边界。在处理事件之前检查坐标是否有效,否则只是中断。

f = (FrameLayout) findViewById(R.id.fl);
f.setOnTouchListener(flt);

f.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
width = f.getMeasuredWidth();
height = f.getMeasuredHeight();

@Override
public boolean onTouch(View v, MotionEvent event) {

    float x = event.getX();
    float y = event.getY();

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            // Write your code to perform an action on down
            break;
        case MotionEvent.ACTION_MOVE:
            if ( (x <= 0 || x >= width) || (y <= 0 || y >= height) )
                break;
            iv.setX(x);
            iv.setY(y);
            break;
        case MotionEvent.ACTION_UP:
            // Write your code to perform an action on touch up
            break;
    }
    // TODO Auto-generated method stub
    return true;
}