【问题标题】:Move and Double click for an ImageView移动并双击 ImageView
【发布时间】:2013-08-31 09:08:49
【问题描述】:

我有这个问题:

我有一个ImageView,我将它设置为onTouchListener,我可以移动它。 问题是我想设置相同的 imageview 来监听 DoubleClick 事件。

你有什么例子可以帮助我解决这个问题吗?

我需要移动并双击同一个图像视图!

【问题讨论】:

    标签: android move touch-event ontouchlistener double-click


    【解决方案1】:
    public class Touch_Screen  extends Activity implements OnTouchListener {
    private ImageView mImageView;
    
    protected void onCreate(Bundle savedInstanceState) {
            setContentView(R.layout.touch_test);
            super.onCreate(savedInstanceState);
    
                mRrootLayout = (ViewGroup) findViewById(R.id.root);
                mImageView = (ImageView) mRrootLayout.findViewById(R.id.imageView);
                mImageView.setVisibility(View.INVISIBLE);
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(150, 150);
                mImageView.setLayoutParams(layoutParams);
    
                mImageView.setOnTouchListener(this);
    }
    
    @Override
        public boolean onTouch(View view, MotionEvent event) {
            try {
                Thread.sleep(50);
            } catch (Exception e) {
                // TODO: handle exception
            }
            final int X = (int) event.getRawX();
            final int Y = (int) event.getRawY();
            switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
                _xDelta = X - lParams.leftMargin;
                _yDelta = Y - lParams.topMargin;
                break;
            case MotionEvent.ACTION_UP:
    
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                break;
            case MotionEvent.ACTION_POINTER_UP:
                break;
            case MotionEvent.ACTION_MOVE:
    
                RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view
                        .getLayoutParams();
                layoutParams.leftMargin = X - _xDelta;
                layoutParams.topMargin = Y - _yDelta;
                layoutParams.rightMargin = -250;
                layoutParams.bottomMargin = -250;
                view.setLayoutParams(layoutParams);
                break;
            }
            mRrootLayout.invalidate();
            return true;
        }
    
    }
    

    【讨论】:

    • 感谢非常好的答案!
    猜你喜欢
    • 1970-01-01
    • 2012-05-03
    • 2019-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-07
    • 2012-04-14
    相关资源
    最近更新 更多