在图像上双击

时间:2018-08-24 23:13:03

标签: java android onclick gesture

我有一个仅添加1张图像的“活动”,其中添加了“平移/缩放/旋转”系统。 我需要在图像上双击以向代码添加另一个功能,但是我做不到。 我尝试使用onClick方法将setOnClickListener添加到我的图像中,尽管没有任何代码错误,但是什么也没有发生。

public class LastActivity extends AppCompatActivity
{
ImageView my_View;
float scalediff;
private static final int NONE = 0;
private static final int DRAG = 1;
private static final int ZOOM = 2;
private int mode = NONE;
private float oldDist = 1f;
private float d = 0f;
private float newRot = 0f;

@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_last);

    my_View = findViewById(R.id.my_View);

    final Bundle mBundle = getIntent().getExtras();

    if (mBundle != null)
    {
        my_View.setImageResource(mBundle.getInt("fullImg"));
    }

    init();

    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int sWidth = size.x;
    int sHeight = size.y;

    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(800, 800);

    my_View.setLayoutParams(layoutParams);

    my_View.setOnTouchListener(new View.OnTouchListener() {
        RelativeLayout.LayoutParams parms;
        int startwidth;
        int startheight;
        float dx = 0, dy = 0, x = 0, y = 0;
        float angle = 0;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            final ImageView view = (ImageView) v;

            ((BitmapDrawable) view.getDrawable()).setAntiAlias(true);
            switch (event.getAction() & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_DOWN:

                    parms = (RelativeLayout.LayoutParams) view.getLayoutParams();
                    startwidth = parms.width;
                    startheight = parms.height;
                    dx = event.getRawX() - parms.leftMargin;
                    dy = event.getRawY() - parms.topMargin;
                    mode = DRAG;
                    break;

                case MotionEvent.ACTION_POINTER_DOWN:

                    oldDist = spacing(event);
                    if (oldDist > 10f) {
                        mode = ZOOM;
                    }

                    d = rotation(event);

                    break;
                case MotionEvent.ACTION_UP:

                    break;

                case MotionEvent.ACTION_POINTER_UP:

                    mode = NONE;

                    break;
                case MotionEvent.ACTION_MOVE:

                    if (mode == DRAG) {

                        x = event.getRawX();
                        y = event.getRawY();

                        parms.leftMargin = (int) (x - dx);
                        parms.topMargin = (int) (y - dy);

                        parms.rightMargin = 0;
                        parms.bottomMargin = 0;
                        parms.rightMargin = parms.leftMargin + (5 * parms.width);
                        parms.bottomMargin = parms.topMargin + (10 * parms.height);

                        view.setLayoutParams(parms);

                    } else if (mode == ZOOM) {

                        if (event.getPointerCount() == 2) {

                            newRot = rotation(event);
                            angle = newRot - d;

                            x = event.getRawX();
                            y = event.getRawY();

                            float newDist = spacing(event);
                            if (newDist > 10f) {
                                float scale = newDist / oldDist * view.getScaleX();
                                if (scale > 0.6) {
                                    scalediff = scale;
                                    view.setScaleX(scale);
                                    view.setScaleY(scale);

                                }
                            }

                            view.animate().rotationBy(angle).setDuration(0).setInterpolator(new LinearInterpolator()).start();

                            x = event.getRawX();
                            y = event.getRawY();

                            parms.leftMargin = (int) ((x - dx) + scalediff);
                            parms.topMargin = (int) ((y - dy) + scalediff);

                            parms.rightMargin = 0;
                            parms.bottomMargin = 0;
                            parms.rightMargin = parms.leftMargin + (5 * parms.width);
                            parms.bottomMargin = parms.topMargin + (10 * parms.height);

                            view.setLayoutParams(parms);
                        }
                    }
                    break;
            }
            return true;
        }
    });

}

private void init()
{
    my_View = findViewById(R.id.my_View);
}

private float spacing(MotionEvent event)
{
    float x = event.getX(0) - event.getX(1);
    float y = event.getY(0) - event.getY(1);
    return (float) Math.sqrt(x * x + y * y);
}

private float rotation(MotionEvent event)
{
    double delta_x = (event.getX(0) - event.getX(1));
    double delta_y = (event.getY(0) - event.getY(1));
    double radians = Math.atan2(delta_y, delta_x);
    return (float) Math.toDegrees(radians);
}
}

如何在不破坏原始代码的情况下对图像进行双击?

这是onClick方法

int c = 0;

my_View.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
    c++;
    Handler handler = new Handler();
    Runnable run = new Runnable()
    {
        @Override
        public void run()
        {
            c = 0;
        }
    };

    handler.postDelayed(run,350);
    if (c == 2)
    {
        // event code
    }
}
});

2 个答案:

答案 0 :(得分:1)

您可以实现适应this answer的手势检测器:

GestureDetector gestureDetector = new GestureDetector(context, new GestureListener());
}

// skipping measure calculation and drawing

// delegate the event to the gesture detector

@Override
public boolean onTouchEvent(MotionEvent e) {
    return gestureDetector.onTouchEvent(e);
}

//since you already have the previous bookean for your original code to work, try changing "return true" (of your on touch event) to "return gestureDetector.onTouchEvent(e)"

private class GestureListener extends GestureDetector.SimpleOnGestureListener {

    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }
    // event when double tap occurs
    @Override
    public boolean onDoubleTap(MotionEvent e) {
        float x = e.getX();
        float y = e.getY();

        //double tapped

        return true;
    }
}

如果ggeasture检测器在获取事件时遇到问题,并且原始触摸侦听器没有反应,则只需在geaturelistener中获取所有其他事件(将代码复制到适当的事件中(tou将需要添加到onDown和onDoubleClicked中) ,为此,您可能需要将“扩展简单的手势侦听器”更改为扩展手势侦听器)

答案 1 :(得分:0)

对我来说,更好和可行的解决方案是将this post birdman 答案添加到我的代码中。...