【问题标题】:Touch Event Problem of Image View图像视图的触摸事件问题
【发布时间】:2011-09-09 05:28:37
【问题描述】:

我的问题是我正在移动 ImageView 的 onTouchEvent 图像,我在一个屏幕上有两个图像但问题是如果我触摸 image2 并移动它是完美的工作但如果我触摸 image1 时 image2 移动到它原来的位置,那么有什么问题呢?抱歉英语交流不好。

请帮帮我。

以下是我的代码:-

Main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ImageView android:layout_width="50sp" android:layout_height="50sp"
        android:id="@+id/image" android:src="@drawable/image">
    </ImageView>
    <ImageView android:layout_y="30dip" android:layout_x="118dip"
        android:layout_width="50sp" android:layout_height="50sp" android:id="@+id/image1"
        android:src="@drawable/image1">
    </ImageView>
</RelativeLayout>

MainActivity.java:-

public class MainActivity extends Activity implements OnTouchListener {
    int windowwidth;
    int windowheight;

    private RelativeLayout.LayoutParams layoutParams;
    private RelativeLayout.LayoutParams layoutParams1;

        ImageView image, image1;
    int x_cord, y_cord;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        windowwidth = getWindowManager().getDefaultDisplay().getWidth();
        windowheight = getWindowManager().getDefaultDisplay().getHeight();

        image = (ImageView) findViewById(R.id.image);
        image.setOnTouchListener(this);

        image1 = (ImageView) findViewById(R.id.image1);
        image1.setOnTouchListener(this);

    }

    public boolean onTouch(View v, MotionEvent event) {
        switch (v.getId()) {
        case R.id.image:
            System.out.println("Image is Touched");

            image = (ImageView) findViewById(R.id.image);
            layoutParams = (RelativeLayout.LayoutParams) image.getLayoutParams();
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                int x_cord = (int) event.getRawX();
                int y_cord = (int) event.getRawY();

                if (x_cord > windowwidth) {
                    x_cord = windowwidth;
                }
                if (y_cord > windowheight) {
                    y_cord = windowheight;
                }

                layoutParams.leftMargin = x_cord - 25;
                layoutParams.topMargin = y_cord - 75;

                image.setLayoutParams(layoutParams);
                break;
            default:
                break;
            }
        case R.id.image1:
            System.out.println("Image 1 is Touched");
            image1 = (ImageView) findViewById(R.id.image1);
            layoutParams1 = (RelativeLayout.LayoutParams) image1
                    .getLayoutParams();
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                x_cord = (int) event.getRawX();
                y_cord = (int) event.getRawY();

                if (x_cord > windowwidth) {
                    x_cord = windowwidth;
                }
                if (y_cord > windowheight) {
                    y_cord = windowheight;
                }

                layoutParams1.leftMargin = x_cord - 25;
                layoutParams1.topMargin = y_cord - 75;

                image1.setLayoutParams(layoutParams1);

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

【问题讨论】:

    标签: android scaling android-imageview


    【解决方案1】:

    这样使用你可以实现:

    tv1 = (TextView)findViewById(R.id.text_view1);
        tv1.setOnTouchListener(new View.OnTouchListener() {         
    
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                layoutParams1 = (RelativeLayout.LayoutParams) tv1.getLayoutParams();
               switch(event.getActionMasked())
               {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                int x_cord = (int) event.getRawX();
                int y_cord = (int) event.getRawY();
                if (x_cord > windowwidth) {
                    x_cord = windowwidth;
                }
                if (y_cord > windowheight) {
                    y_cord = windowheight;
                }
                layoutParams1.leftMargin = x_cord - 25;
                layoutParams1.topMargin = y_cord - 75;
                tv1.setLayoutParams(layoutParams1);
                break;
            default:
                break;
            }
            return true;
        }
        });
    
        tv2 = (TextView)findViewById(R.id.text_view2);
        tv2.setTextColor(Color.MAGENTA);
        tv2.setOnTouchListener(new View.OnTouchListener() {         
    
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                layoutParams2 = (RelativeLayout.LayoutParams) tv2.getLayoutParams();
               switch(event.getActionMasked())
               {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                int x_cord = (int) event.getRawX();
                int y_cord = (int) event.getRawY();
                if (x_cord > windowwidth) {
                    x_cord = windowwidth;
                }
                if (y_cord > windowheight) {
                    y_cord = windowheight;
                }
                layoutParams2.leftMargin = x_cord - 25;
                layoutParams2.topMargin = y_cord - 75;
                tv2.setLayoutParams(layoutParams2);
                break;
            default:
                break;
            }
            return true;
        }
        });
    

    【讨论】:

    • 您的代码也正在使用图像视图,但问题是如果我使用大分辨率图像(例如 320*480),则图像未正确移动。如果您有任何想法,请告诉我。
    • Hello Raj 如何在此代码上执行放大、缩小和旋转。请帮帮我。
    • 使用这个小代码是不可能看到这个链接的:dl.dropbox.com/u/38493970/Full.java 通过使用它可以平移和缩放将这个代码集成到你的代码中..
    • 我整合了这段代码,但问题是图像只缩放了固定宽度和高度。
    • 如果我在发生相同问题时删除了跨越部件,则图像视图将放大并缩小其固定尺寸意味着 wrap_content。
    【解决方案2】:

    虽然你在这里有更多的声誉,但我没有看到,相对布局方向,水平或垂直?您的图像是并排还是重叠的,意味着,每次触摸任何图像时,图像位置 onTouch 事件都会出现第二张图像。 对不起我的英语,如果你不明白的话..

    【讨论】:

    • 如果我在垂直位置使用 ImageView,问题是一样的。 Image2 始终处于其原始状态。
    猜你喜欢
    • 2013-09-23
    • 1970-01-01
    • 2013-01-26
    • 1970-01-01
    • 1970-01-01
    • 2019-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多