Is it possible to get real time coordinates of an ImageView while it is in Translate animation?

前端 未结 3 760
失恋的感觉
失恋的感觉 2021-01-11 14:18

I have an image of a bullet in an ImageView that does Translate animation.

I need to show real time coordinates to show how far it is from target in real time.

3条回答
  •  清歌不尽
    2021-01-11 14:34

    I had this idea: you can extend the standard TranslateAnimation and intercept every step of the animation by overriding the applyTransformation method.

    Take a look at this incomplete/untested snippet:

    private class ObservableTranslateAnimation extends TranslateAnimation{
    
        private float matrixValues[] = new float[9];
        private float actualDx;
        private float actualDy;
    
        public ObservableTranslateAnimation(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        // ... more constructors 
    
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            super.applyTransformation(interpolatedTime, t);
            //After that super is called, the matrix gets updated!
    
            //get the current matrix and extract what you need
            t.getMatrix().getValues(matrixValues);
            actualDx = matrixValues[Matrix.MTRANS_X];
            actualDy = matrixValues[Matrix.MTRANS_Y];
    
            /*
              Notify someone here (a listener?), or just read the values of actualDx, actualDy from outside
              You can also play around with the other Matrix values.
            */
        }
    }
    

提交回复
热议问题