Android:通过动画在触摸屏上旋转两个对象(圆)

Android:通过动画在触摸屏上旋转两个对象(圆),android,animation,rotation,draw,rotateanimation,Android,Animation,Rotation,Draw,Rotateanimation,基本上只想画两个圆(一个红色,一个蓝色),并使它们与旋转动画一起旋转。已经尝试了几天,但没有成功 还尝试将动画方法移到GameView类中,但也没有成功 目前它只是在onTouch上崩溃。 以下是我目前的代码: 游戏屏幕 public class GameScreen extends Activity { private GameView myGameView; private DrawThread drawThread ; @Override p

基本上只想画两个圆(一个红色,一个蓝色),并使它们与旋转动画一起旋转。已经尝试了几天,但没有成功

还尝试将动画方法移到GameView类中,但也没有成功

目前它只是在onTouch上崩溃。 以下是我目前的代码:

游戏屏幕

    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;
    }
}
游戏视图

    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);

    }

}
包装工

    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);
    }
}
拉丝

    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("setRunning@DrawThread", "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);
                }
            }
        }
    }
}
蓝色颜料

    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);
    }
}

非常感谢您的帮助。

在res中创建一个anim文件夹,创建一个flipping.xml,并在课堂上使用它

<set android:interpolator="@android:anim/linear_interpolator">
<rotate android:fromDegrees="0" 
android:toDegrees="360" 
android:pivotX="50%"
android:pivotY="50%" 
android:duration="500" 
android:startOffset="0"
android:repeatCount="1"
android:repeatMode="reverse"/></set>

我把它放在GameView里了吗?编译器怎么知道我指的是代码中的两个圆圈。看不到任何关联。在圆/图像的ontouch方法中的GameView类中使用它。在GameView类的rotate方法中复制了它,并出现三个错误:1)无法解析任务;2) 对于loadAnimator中的此项:在GameView中找到错误的第一个参数,但需要上下文。3) R.anim.flipping:类型为animator的例外资源。
AnimatorSet set;
set = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.anim.flipping);
    set.setTarget(img_logo);
    set.start();

    Timer timer = new Timer();
    timer.schedule(task, 2000);