View TouchListener中的Android Bitmap

时间:2016-11-04 08:38:23

标签: android view ontouchlistener

我在View中有一个位图。当我使用onTouchEvent(MotionEvent event)时,它会控制View touch。

但我想移动或触摸位图。当我触摸位图时,视图正在移动。但Bitmap并没有移动。当我触摸位图时,我想移动位图。不查看

    public class TestView extends View {
    public TestView(Context context) {
        super(context);

        Bitmap target = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.image), 150, 150, false);

        @Override
        public boolean onTouchEvent (MotionEvent event){

            float x = event.getX();
            float y = event.getY();
            int action = event.getAction();

            if (action == MotionEvent.ACTION_DOWN) {
                if (x_t <= x && x < x_t + target.getWidth() &&
                        y_t <= y && y < y_t + target.getHeight()) {

                    View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(this);

                    viewGroup.getChildAt(1).startDrag(null, //data to be dragged
                            shadowBuilder, //drag shadow
                            this, //local data about the drag and drop operation
                            0   //no needed flags
                    );
                }
            }
            return true;
        }
    }
}

0 个答案:

没有答案