android action_up有时候效果不好?

时间:2013-11-28 10:26:01

标签: android multi-touch

在我的应用程序中,通过在Android触摸事件中使用矩阵拖放图像,在此MotionEvent.ACTION_DOWN& MotionEvent.ACTION_MOVE工作正常,但MotionEvent.ACTION_UP无法正常工作。某些时候某些工作无效,如何解决我的问题。

public class MainActivity extends Activity {

    // mainLayout is the child of the HorizontalScrollView ...
    private LinearLayout mainLayout;

    // this is an array that holds the IDs of the drawables ...
    private int[] images = { R.drawable.gg, R.drawable.gg, R.drawable.gg,
            R.drawable.gg, R.drawable.gg, R.drawable.gg };

    ImageButton btn1, btn2, btn3;
    float dy; // postTranslate Y distance
    float matrixX = 0; // X coordinate of matrix inside the ImageView
    float matrixY = 0; // Y coordinate of matrix inside the ImageView
    float[] matrixValues = new float[9];
    float height = 0;
    HorizontalScrollView hsv;
    boolean hTouch = false;

    /** Called when the activity is first created. */
    @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        setContentView(R.layout.activity_main);

        mainLayout = (LinearLayout) findViewById(R.id.linearLayout);
        for (int i = 0; i < images.length; i++) {
            hsv = (HorizontalScrollView) findViewById(R.id.hsv);
            final Matrix matrix = new Matrix();
            final Matrix savedMatrix = new Matrix();
            final PointF startPoint = new PointF();
            matrixValues = new float[9];
            View cell = getLayoutInflater().inflate(R.layout.image, null);

            final ImageView img = (ImageView) cell.findViewById(R.id.imageView);
            img.setImageResource(images[i]);
            btn1 = (ImageButton) cell.findViewById(R.id.button1);
            btn2 = (ImageButton) cell.findViewById(R.id.button2);
            btn3 = (ImageButton) cell.findViewById(R.id.button3);

            img.setOnTouchListener(new View.OnTouchListener() {

                @SuppressWarnings("deprecation")
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    ImageView view = (ImageView) v;
                    switch (event.getAction() ) {
                    case MotionEvent.ACTION_DOWN:

                        System.out.println("action down");
                        savedMatrix.set(matrix);
                        startPoint.set(view.getLeft(), event.getY());
                        break;

                    case MotionEvent.ACTION_UP:



                            matrix.set(savedMatrix);
                            matrix.getValues(matrixValues);



                            matrixX = matrixValues[2];
                            matrixY = matrixValues[5];
                            height = matrixValues[4]
                                    * (((ImageView) view).getDrawable()
                                            .getIntrinsicHeight());
                            dy = event.getY() - startPoint.y;
                            System.out.println("matrixY: " + matrixY);

                            if (matrixY + dy < 0) {
                                System.out.println("1st if checking");
                                dy = -matrixY;
                            }
                            if (matrixY + dy + height > view.getHeight()
                                    + btn1.getBottom()) {
                                System.out.println("2nd if checking");
                                dy = (view.getHeight() + btn1.getBottom())
                                        - matrixY - (height);

                            }



                            float bottom = (btn1.getBottom()+2);

                            if (dy > 0.01 && dy < (bottom)) {
                                System.out.println("1st if");
                                dy = btn1.getBottom() ;


                            }


                            if (dy < -0.01 && dy > (-bottom)) {
                                System.out.println("2nd if");
                                dy = -btn1.getBottom();

                            }



                            matrix.postTranslate(0, dy);
                            view.setImageMatrix(matrix);
                            view.invalidate();



                        break;

                    case MotionEvent.ACTION_MOVE:
                        System.out.println("");

                        matrix.set(savedMatrix);
                        matrix.getValues(matrixValues);
                        matrixX = matrixValues[2];
                        matrixY = matrixValues[5];
                        height = matrixValues[4]
                                * (((ImageView) view).getDrawable()
                                        .getIntrinsicHeight());
                        dy = event.getY() - startPoint.y;
                        if (matrixY + dy < 0) {

                            dy = -matrixY;
                        }
                        if (matrixY + dy + height > view.getHeight()
                                + btn1.getBottom()) {
                            dy = (view.getHeight() + btn1.getBottom())
                                    - matrixY - (height);

                        }


                        matrix.postTranslate(0, dy);
                        view.setImageMatrix(matrix);

                        break;

                    }
                    return true;

                }

            });
            mainLayout.addView(cell);

        }



    }
}

1 个答案:

答案 0 :(得分:0)

它不像那样

MotionEvent.ACTION_CANCEL是另一个事件,当您从视图中拖动并在视图外拖动结束时会被触发。它不会在那个时候调用MotionEvent.ACTION_UP。如果操作在触摸响应视图中开始和结束,则会调用UP