0
我正在嘗試創建一個圓形動畫,但屏幕並未自行刷新,而是在不同位置繪製了許多圈,而不是繪製運動。我想以這種方式使用它,而不是使用invalidate()
方法,因爲我想將它用於實時應用程序。在android中繪製動畫圈
下面是代碼:
public class activity_layout_animation extends SurfaceView implements Runnable {
Thread thread;
boolean CanDraw = false;
Paint red_fill;
int circle_x, circle_y;
Canvas canvas;
SurfaceHolder surfaceHolder;
public activity_layout_animation(Context context){
super(context);
surfaceHolder = getHolder();
circle_x= 130;
circle_y=130;
}
@Override
public void run(){
preparePaint();
while(CanDraw){
if (!surfaceHolder.getSurface().isValid()){
continue;
}
canvas = surfaceHolder.lockCanvas();
motionCircle(10);
canvas.drawCircle(circle_x,circle_y,50, red_fill);
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
public void pause(){
CanDraw = false;
while(true) {
try {
thread.join();
break;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
thread = null;
}
public void resume(){
CanDraw = true;
thread = new Thread(this);
thread.start();
}
private void preparePaint(){
red_fill = new Paint();
red_fill.setColor(Color.RED);
red_fill.setStyle(Paint.Style.FILL);
}
private void motionCircle (int speed){
if ((circle_y == 130) && (circle_x < 650)){
circle_x = circle_x + speed;
}
if ((circle_y < 650) && (circle_x ==650)){
circle_y = circle_y + speed;
}
if ((circle_y == 650) && (circle_x > 130)){
circle_x = circle_x - speed;
}
if ((circle_y > 130) && (circle_x== 130)){
circle_y = circle_y - speed;
}
}
}
謝謝!