Android中用canvas如何绘制动态图?请大神速回,在线等!!

public class MyView extends View {
private Paint paint;
private int roundColor, roundProgressColor;
private float roundWidth;
private int style;
private int process = 1;
private Canvas mcanvas;
private RectF oval;

public MyView(Context context) {
    this(context, null);
}

public MyView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);

    paint = new Paint();
    TypedArray mtypeArray = context.obtainStyledAttributes(attrs, R.styleable.RoundProgressBar);
    roundColor = mtypeArray.getColor(R.styleable.RoundProgressBar_roundColor, Color.RED);
    roundWidth = mtypeArray.getDimension(R.styleable.RoundProgressBar_roundWidth, 15);
    roundProgressColor = mtypeArray.getColor(R.styleable.RoundProgressBar_roundProgressColor, Color.GREEN);
    style = mtypeArray.getInt(R.styleable.RoundProgressBar_style, 0);

    mtypeArray.recycle();

}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    int center = getWidth() / 2;
    int radius = (int) (center - roundWidth / 2);

//
// paint.setColor(roundColor);
// paint.setStyle(Paint.Style.STROKE);
// paint.setStrokeWidth(roundWidth);
// paint.setAntiAlias(true);
// PathEffect effects = new DashPathEffect(new float[]{10,10,10,10},1);
// paint.setPathEffect(effects);
// canvas.drawCircle(center, center, radius, paint);

    paint.setStrokeWidth(roundWidth);
    paint.setColor(roundProgressColor);
    oval = new RectF(center - radius, center - radius, center + radius, center + radius);
    paint.setStyle(Paint.Style.STROKE);
    PathEffect effects = new DashPathEffect(new float[]{10, 10, 10, 10}, 1);
    paint.setAntiAlias(true);
    paint.setPathEffect(effects);
    canvas.drawArc(oval, 120, 30*process%300, false, paint);
    mcanvas = canvas;
    Log.i("pan", "meiyoujinlai");
    Log.i("pan", canvas.toString() + "meiyou");
    new Thread() {
        @Override
        public void run() {
            super.run();
            Message msg = new Message();
            msg.what = 1;

            while (true) {
                try {

                    mHandler.sendMessage(msg);
                    sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }


        }
    }.start();

}


private Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        if (msg.what == 1) {
            Log.i("pan", "xianchengzaifa!!");
            Log.i("pan", mcanvas + "is value");

            mcanvas.drawArc(oval, 120, process % 300, false, paint);
            process += 20;

        }
    }
};

}
上面是我的代码,下面是我的效果图图片说明。这是我想实现的动态效果:图片说明

1个回答

首先尽可能不要onDraw方法里实例对象,你可能在MyView构造方法里创建后一个线程,在线程了计算弧度大小,然后调用postInvalidate();重绘

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问