2015年10月14日 星期三

制作自定義的View2-用view在Canvas畫動畫

In a View, the onDraw method use to draw the picture, So if we can call the onDraw  many time per second, it become 動畫. However ,we cant call the onDraw   directly , we need to use  invalidate() or postInvalidate();

invalidate() VS postInvalidate();

invalidate call at UI Thread
postInvalidate call at non UI thread

Morever, we need to create a new thread to call postInvalidate  to re Draw, in the onDraw method call invalidate  is not good.

example:
https://github.com/roy989898/tryMakeViewByMyself
the v2 logic view




remark:
Thread.sleep(30);

is important, to prevent re flash too fast, you cant see the change

code:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package pom.poly.com.trymakeviewbymyself.myView.v2;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import java.util.Random;

/**
 * Created by User on 14/10/2015.
 */
public class LogicView extends View {
    private Paint paint = new Paint();
    private int rx = 0;
    private MyThread myThread = null;
    private String showString = "show me";
    private RectF rectF = new RectF(0, 40, 600, 640);
    private float sweepAngle = 0;

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


    public LogicView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setTextSize(200);
        canvas.drawText(showString, rx, 300, paint);
        canvas.drawArc(rectF, 0, sweepAngle, true, paint);//why true?
        if (myThread == null) {
            myThread = new MyThread();
            myThread.start();
        }
        //invalidate(); //cal by ui thread
        //postInvalidate();
        //canvas.drawText("hi",rx,300,paint);
    }

    class MyThread extends Thread {

        @Override
        public void run() {
            Random random = new Random();
            super.run();

            while (true) {
                if (rx > getWidth()) {
                    rx = 0 - (int) paint.measureText(showString);
                }
                if (sweepAngle > 360) {
                    sweepAngle = 0;
                }

                paint.setARGB(255, random.nextInt(256), random.nextInt(256), random.nextInt(256));//important
                rx = rx + 20;
                sweepAngle += 2;
                postInvalidate();//call by non-UI thread
                try {
                    Thread.sleep(30);//important !!!
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
    }

}

in this view, we create a new thread, However, when we leave the app, the thread still keep running. we will sole this problem next charcter.