2
基本上只需繪製兩個圓圈(一個紅色,一個藍色),並使它們與旋轉動畫一起旋轉。已經嘗試了幾天,但沒有取得任何成功。Android:用動畫旋轉兩個物體(圓圈)onTouch
也試圖將動畫方法移到GameView類中,但它在那裏也不起作用。
目前它只是onTouch崩潰。 這裏是到目前爲止我的代碼:
GameScreen
public class GameScreen extends Activity {
private GameView myGameView;
private DrawThread drawThread ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_screen);
myGameView = (GameView) (findViewById(R.id.gameView1));
myGameView.setZOrderOnTop(true);
myGameView.getHolder().setFormat(PixelFormat.TRANSLUCENT);
}
public boolean onTouchEvent(MotionEvent e) {
int eventaction = e.getAction();
switch (eventaction) {
case MotionEvent.ACTION_DOWN:
System.out.println("Clicked");
myGameView.rotate(myGameView);
myGameView.invalidate();
break;
}
return true;
}
}
GameView
public class GameView extends SurfaceView implements
SurfaceHolder.Callback {
private BleenCatcher blueCatcher;
private BleenCatcher redCatcher;
private DrawThread drawThread;
private int colorBlue = -16776961;
private int colorRed = -65536;
public GameView(Context context) {
super(context);
initialize();
}
public GameView(Context context, AttributeSet attrs) {
super(context, attrs);
initialize();
}
public GameView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initialize();
}
private void initialize() {
getHolder().addCallback(this);
drawThread = new DrawThread(getHolder(), this);
setFocusable(true);
}
public void surfaceCreated(SurfaceHolder holder) {
blueCatcher = new BleenCatcher(540, 850, colorBlue);
redCatcher = new BleenCatcher(540, 950, colorRed);
drawThread.setRunning(true);
drawThread.start();
}
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int
arg3) {
// TODO Auto-generated method stub
}
public void surfaceDestroyed(SurfaceHolder arg0) {
boolean retry = true;
drawThread.setRunning(false);
while (retry) {
try {
drawThread.join();
retry = false;
} catch (InterruptedException e) { }
}
}
public void onDraw(Canvas canvas) {
redCatcher.onDraw(canvas);
blueCatcher.onDraw(canvas);
}
public void rotate(GameView game){
redCatcher.changePosition(game);
blueCatcher.changePosition(game);
}
}
BleenCatcher
public class BleenCatcher {
int x, y, rad = 50;
private Animation anima;
private boolean rotate = false;
private GameView myGameView;
BleenPaint catcherPaint;
public BleenCatcher(int x, int y, int color) {
this.x = x;
this.y = y;
catcherPaint = new BleenPaint(1, Paint.Cap.SQUARE, Paint.Style.FILL,
color);
}
public void onDraw(Canvas canvas) {
if(rotate){
canvas.drawCircle(x, y, rad, catcherPaint);
createAnimation(canvas);
rotate = false;
}else{
canvas.drawCircle(x, y, rad, catcherPaint);
}
}
public void changePosition(GameView game){
rotate = true;
myGameView = game;
}
protected void createAnimation(Canvas canvas) {
anima = new RotateAnimation(0, 180, 540 , 850);
anima.setRepeatMode(Animation.RESTART);
anima.setRepeatCount(Animation.INFINITE);
anima.setDuration(1000L);
myGameView.startAnimation(anima);
}
}
DrawThread
public class DrawThread extends Thread {
private SurfaceHolder surfaceHolder;
private GameView surfaceView;
private boolean run = false;
public DrawThread(SurfaceHolder surfaceHolder, GameView surfaceView) {
this.surfaceHolder = surfaceHolder;
this.surfaceView = surfaceView;
run = false;
}
public void setRunning(boolean run) {
Log.d("[email protected]", "Run status is " + run);
this.run = run;
}
@Override
public void run() {
Canvas canvas = null;
while (run) {
try {
canvas = surfaceHolder.lockCanvas(null);
synchronized (surfaceHolder) {
surfaceView.onDraw(canvas);
}
} finally {
if (canvas != null) {
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
}
}
BleenPaint
public class BleenPaint extends Paint {
public BleenPaint(int strokeWidth, Paint.Cap cap, Paint.Style style,
int color) {
setStrokeWidth(strokeWidth);
setAntiAlias(true);
setStrokeCap(cap);
setStyle(style);
setColor(color);
}
}
任何幫助表示讚賞。
我把它放在GameView中嗎?而編譯器如何知道我的意思是那兩個代碼的圓圈。看不到任何關聯。 – user3361146
在你的GameView Class中使用圓/圖像的ontouch方法。 –
在GameView類的rotate方法中複製它,並得到三個錯誤: 1)無法解析任務; 2)對於loadAnimator中的這個:錯誤的第一個參數發現GameView但需要上下文。 3)R.anim.flipping:類型動畫的例外資源。 – user3361146