如何布局的视图中保持X和Y? [英] How to keep X and Y within layout's view ?

查看:155
本文介绍了如何布局的视图中保持X和Y?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有有拇指图像的FrameLayout用户可以拖动。

I have a FrameLayout which has a thumb image which the user can drag around.

拇指宽度为10dp和高度是10dp。

The thumb width is 10dp and height is 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的左右外。

My goal is, if the use drags the box outside of the view on the left give the thumb a x+10 to keep with in view and if the user drags to the right outside the view give the thumb a x-10 to keep within view. But the thumb just disappears if I drag left and right outside of the 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>

我如何修改code所以可达到的结果?

How can I modify the code so I can achieve the result ?

推荐答案

你试过从 HTTP的code:// stackoverflow.com/a/9112808/663370

你让图像走出去的边界,然后试图纠正这一点。根本就不放手边界外摆在首位。检查坐标处理事件之前有效,否则刚刚突破。

You're letting the image go out of the boundary and then trying to correct for that. Simply don't let it go outside the boundary in the first place. Check the coordinates are valid before processing the event, otherwise just break.

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

这篇关于如何布局的视图中保持X和Y?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆