invalidate()不会调用onDraw(),Android Studio

时间:2017-07-26 11:13:00

标签: android

我是Android新手,需要帮助。 在我的代码中,我使用onTouchEven()来移动ImageView,但invalidate()不会调用onDraw()。我正在寻找整个互联网上的解决方案,stil看不到我的错误。请帮忙。

map - 这是我想要拖动的ImageView。

public class MainActivity extends AppCompatActivity {
    TextView infoSpace;
    ImageView map;
    ImageButton updateButton;
    String currentDateTimeString;

    @Override
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState);
        setContentView(com.example.eleizo.firstapplication.R.layout.activity_main); 

        map = (ImageView) findViewById(R.id.map);

        map.setWillNotDraw(false);
        map.setOnTouchListener(new View.OnTouchListener(){
            @Override
            public boolean onTouch(View v, MotionEvent event){
                Context context = getApplicationContext();
                new CustomImageView(context).onTouchEvent(event);
                return true;
            }
        });

    }

    private class CustomImageView extends View {

    private float mLastTouchX;
    private float mLastTouchY;
    private float mPosX;
    private float mPosY;

    //the active pointer is one currently moving object
    private static final int INVALID_POINTER_ID = -1;
    private int mActivePointerId = INVALID_POINTER_ID;

        public CustomImageView(Context context) {
            super(context);
        }

        @Override
        public boolean onTouchEvent(MotionEvent ev) {
            final int action = ev.getActionMasked();


            switch (action & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_DOWN: {
                    Log.d("DEBUG","MotionEvent.ACTION_DOWN");
                    final float x = ev.getX();
                    final float y = ev.getY();
                    mLastTouchX = x;
                    mLastTouchX = y;
                    mActivePointerId = ev.getPointerId(0);
                    break;
                }

                case MotionEvent.ACTION_MOVE: {
                    Log.d("DEBUG","MotionEvent.ACTION_MOVE");
                    final int pointerIndex = ev.findPointerIndex(mActivePointerId);
                    final float x = ev.getX(pointerIndex);
                    final float y = ev.getY(pointerIndex);
                    final float dx = x - mLastTouchX;
                    final float dy = y - mLastTouchY;
                    mPosX += dx;
                    mPosY += dy;
                    this.setX(x+mPosX);
                    this.setY(y+mPosY);

                    invalidate();
                    Log.d("DEBUG",String.format("AFTER invalidate(): %f %f", mPosX,  mPosY));

                    mLastTouchX = x;
                    mLastTouchY = y;

                    break;
                }

                case MotionEvent.ACTION_UP: {
                    Log.d("DEBUG","MotionEvent.ACTION_UP");
                    mActivePointerId = INVALID_POINTER_ID;
                    break;
                }

                case MotionEvent.ACTION_CANCEL: {
                    Log.d("DEBUG","MotionEvent.ACTION_CANCEL");
                    mActivePointerId = INVALID_POINTER_ID;
                    break;
                }

                case MotionEvent.ACTION_POINTER_UP: {
                    Log.d("DEBUG","MotionEvent.ACTION_POINTER_UP");
                    final int pointerIndex = ev.getActionIndex();
                    final int pointerId = ev.getPointerId(pointerIndex);

                    if (pointerId == mActivePointerId) {
                        // This was our active pointer going up. Choose a new
                        // active pointer and adjust accordingly.
                        final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
                        mLastTouchX = ev.getX(newPointerIndex);
                        mLastTouchY = ev.getY(newPointerIndex);
                        mActivePointerId = ev.getPointerId(newPointerIndex);
                    }
                    break;
                }
            }
            return true;
        }

        @Override
        protected void onDraw (Canvas canvas){
            Log.d("DEBUG","OnDraw()");
            super.onDraw(canvas);
            canvas.save();
            canvas.translate(mPosX,mPosY);
            canvas.restore();

        }
    }

当我触摸地图时,我可以看到日志“AFTER invalidate():..” 但永远不会看到日志“OnDraw()”。

1 个答案:

答案 0 :(得分:0)

new CustomImageView(context).onTouchEvent(event);

您没有保存对CustomImageView对象的引用。您正在创建一个对象,自己调用onTouchEvent,但是您没有存储引用或者引用任何类调用onDraw。

也许制作map CustomImageView类型的对象,而不是ImageView