Android 如何将ValueAnimation放入线程?

Android 如何将ValueAnimation放入线程?,android,multithreading,animation,Android,Multithreading,Animation,我定义了一个简单的textview,它上面有一个进度条的动画。它工作得很好,但当我将其中几个放入活动时,调试器说“跳过了**帧!应用程序可能在其主线程上做了太多工作”。 那么,如果我可以把动画放到一个线程中,那么如何? 谢谢,以下是我的文本视图 同时,我在onAnimationEnd回调中发送消息的方式是否正确 public class MyTextView extends androidx.appcompat.widget.AppCompatTextView { private Pai

我定义了一个简单的textview,它上面有一个进度条的动画。它工作得很好,但当我将其中几个放入活动时,调试器说“跳过了**帧!应用程序可能在其主线程上做了太多工作”。 那么,如果我可以把动画放到一个线程中,那么如何? 谢谢,以下是我的文本视图

同时,我在onAnimationEnd回调中发送消息的方式是否正确

public class MyTextView extends androidx.appcompat.widget.AppCompatTextView {
    private Paint mPaint = new Paint();
    private PathMeasure mPathMeasure = new PathMeasure();
    private Path mPath = new Path();
    private int mWidth, mHeight;
    private Handler mHandler;
    float[] pos = new float[2];

    @Override
    protected void onDraw(Canvas canvas) {
        mHeight = getMeasuredHeight();
        mWidth = getMeasuredWidth();
        int left = getPaddingLeft();
        int top = getPaddingTop();

        mPath.reset();

        mPaint.setColor(Color.BLACK);
        mPaint.setStyle(Paint.Style.STROKE);
        mPath.moveTo(left, top);
        mPath.lineTo(left + mWidth, top);
        mPathMeasure.setPath(mPath, false);        

        mPaint.setColor(Color.RED);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(10);
        canvas.drawLine(left, top, left + pos[0], top, mPaint);
        super.onDraw(canvas);
    }

    public void startMove(int nDuration, int nWidth) {
        this.measure(0,0);
        int width = getMeasuredWidth();
        ValueAnimator Anim = ValueAnimator.ofFloat(0, width /*mPathMeasure.getLength()*/);
        Anim.setDuration(nDuration);
        Anim.setRepeatCount(ValueAnimator.INFINITE);
        Anim.setRepeatMode(ValueAnimator.RESTART);
        Anim.setInterpolator(new DecelerateInterpolator());

        Anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float distance = (float) animation.getAnimatedValue();

                mPathMeasure.getPosTan(distance, pos, null);
                //postInvalidate();
                invalidate();
            }
        });

        Anim.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                Handler handler = MyApp.getMyApplication().getgHandler();
                Message msg = new Message();
                msg.what = 0x100 + mID;
                handler.sendMessage(msg);
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        Anim.start();
    }
}