如何使用矩阵限制拖放中的屏幕边界?

时间:2013-11-25 06:32:18

标签: android matrix

//我正在使用矩阵来拖放图像。当向上移动图像时,它正越过屏幕边界,我如何在屏幕边界移动图像来重写图像?     如何在屏幕边界移动图像。请提前帮助我。

     public class MultiTouch extends Activity implements OnTouchListener {

            // these matrices will be used to move and zoom image
            private Matrix matrix = new Matrix();
            private Matrix savedMatrix = new Matrix();
            // we can be in one of these 3 states

            private static final int DRAG = 1;


            // remember some things for zooming
            private PointF start = new PointF();
            public boolean first = true;

            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                ImageView view = (ImageView) findViewById(R.id.imageView);
                view.setOnTouchListener(this);
            }

            public boolean onTouch(View v, MotionEvent event) {
                // handle touch events here
                ImageView view = (ImageView) v;
                switch (event.getAction() & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_DOWN:
                    if (first) {
                        first = false;
                        savedMatrix.set(matrix);
                        start.set(event.getX(), event.getY());

                    }

                    else {
                        savedMatrix.set(matrix);
                        start.set(event.getX(), event.getY());
                    }

                    break;

                case MotionEvent.ACTION_UP:

                case MotionEvent.ACTION_MOVE:


                        float dx = event.getX() - start.x;
                        float dy = event.getY() - start.y;

                        if (dy > 0) {

                            matrix.set(savedMatrix);
                            matrix.postTranslate(view.getLeft(), dy);
                            view.setImageMatrix(matrix);
                        }

                        if (dy < 0) {

                            matrix.set(savedMatrix);
                            matrix.postTranslate(view.getLeft(), dy);
                            view.setImageMatrix(matrix);
                        }


                    break;
                }

                return true;
            }

        }

    my xml is:
    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:scaleType="matrix"
            android:src="@drawable/dd" />

    </FrameLayout>

0 个答案:

没有答案