简体   繁体   English

android action_up有时无法正常工作?

[英]android action_up is not working perfectly some times?

In my app drag drop the image by using matrix in android touch events,in this MotionEvent.ACTION_DOWN & MotionEvent.ACTION_MOVE is working perfectly,but MotionEvent.ACTION_UP is not working.actionup is working some times some time it is not working, how to solve my problem. 在我的应用程序中,通过在android touch事件中使用矩阵拖放图像,在此MotionEvent.ACTION_DOWNMotionEvent.ACTION_MOVE正常工作,但MotionEvent.ACTION_UP无法工作.actionup有时工作不正常,如何工作解决我的问题。

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

        }



    }
}

Its not like that 不像那样

MotionEvent.ACTION_CANCEL is another event which gets fired when you start dragging from yor view and drag ends outside the view. MotionEvent.ACTION_CANCEL是另一个事件,当您开始从视图中拖动并在视图外拖动结束时会触发该事件。 It wont call MotionEvent.ACTION_UP on that time. 那时它不会调用MotionEvent.ACTION_UP UP gets called if action starts and end within your touch responsive view. 如果动作在触摸响应视图中开始并结束,则会调用UP

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM