Android 我的乒乓球拍工作不正常

Android 我的乒乓球拍工作不正常,android,animation,timer,imageview,pong,Android,Animation,Timer,Imageview,Pong,我对android还是相当陌生的,目前我正在尝试创建pong 目前,我正在尝试动画两个图像,一个是乒乓球和第二个是桨。现在我有这两张图片,我已经把它们制作成动画。球的行为就像我希望它围绕x轴和y轴反弹一样,我让球拍在x轴上来回滑动,但出于某种原因,球拍的行为很奇怪 由于某种原因,桨叶可以在任何需要的时候停止和启动,使其具有波涛汹涌的动画效果。但每当我从代码中删除球动画时,拨片的动作和行为都与我预期的一样 如果有人对我做错了什么或忽略了什么有任何建议,我洗耳恭听,谢谢 package com.ex

我对android还是相当陌生的,目前我正在尝试创建pong

目前,我正在尝试动画两个图像,一个是乒乓球和第二个是桨。现在我有这两张图片,我已经把它们制作成动画。球的行为就像我希望它围绕x轴和y轴反弹一样,我让球拍在x轴上来回滑动,但出于某种原因,球拍的行为很奇怪

由于某种原因,桨叶可以在任何需要的时候停止和启动,使其具有波涛汹涌的动画效果。但每当我从代码中删除球动画时,拨片的动作和行为都与我预期的一样

如果有人对我做错了什么或忽略了什么有任何建议,我洗耳恭听,谢谢

package com.example.pongtest;

import java.util.Timer;
import java.util.TimerTask;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.app.Activity;

public class MainActivity extends Activity {

LinearLayout lay1;
ImageView ballImg, paddleImg;
TimerTask thisTimerTask;
Timer thisTimer;
LinearLayout.LayoutParams params;

float ballPosX=10; 
float ballPosY=10;
float paddlePosX=200;
float paddlePosY=10;
float ballOnLeftWall=0;
float ballOnRightWall=270;
float ballOnBottomWall=400;
float ballOnTopWall=0;
float paddleOnLeftWall=0;
float paddleOnRightWall=200;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    lay1 = new LinearLayout(this);
    ballImg = new ImageView(this);
    ballImg.setBackgroundResource(R.drawable.circlewdot);       
    setPosition(0,0,50,50);
    ballImg.setLayoutParams(params);

    paddleImg=new ImageView(this);
    paddleImg.setBackgroundResource(R.drawable.paddle);
    setPosition(0,0,50,10);
    paddleImg.setLayoutParams(params);

    lay1.addView(ballImg);
    lay1.addView(paddleImg);

    setContentView(lay1);

    thisTimerTask = new ThisTimerClass();
    thisTimer = new Timer(); 

    //setup frame rate of timer
    thisTimer.scheduleAtFixedRate(thisTimerTask, 2000, 16);
}

public void setPosition(float x, float y, float width, float height) {
params = new LinearLayout.LayoutParams(0,0);
params.topMargin = (int)y;
params.leftMargin = (int)x;
params.width = (int)width;
params.height = (int)height;
}

class ThisTimerClass extends TimerTask {
boolean directionOfBall_isRight = true;
boolean directionOfBall_isDown = true;
boolean directionOfPaddle_isLeft = true;

@Override
public void run() {
    // Will be called each time the timer is fired.
    MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {

        setPosition(ballPosX,ballPosY,50,50);
        ballImg.setLayoutParams(params);

        setPosition(paddlePosX,paddlePosY,50,10);
        paddleImg.setLayoutParams(params);

        //animates ball across the screen
        if (directionOfBall_isRight == true) 
        {ballPosX = (ballPosX + 1);}

        else if (directionOfBall_isRight == false) 
        {ballPosX = (ballPosX - 1);}

        if (directionOfBall_isDown == true) 
        {ballPosY = (ballPosY + 1);}

        else if (directionOfBall_isDown == false) 
        {ballPosY = (ballPosY - 1);}

        //redirects ball
        if (ballPosX > ballOnRightWall) 
        {directionOfBall_isRight = false;}

        else if (ballPosX < ballOnLeftWall) 
        {directionOfBall_isRight = true;} 

        if (ballPosY > ballOnBottomWall) 
        {directionOfBall_isDown = false;}

        else if (ballPosY < ballOnTopWall) 
        {directionOfBall_isDown = true;}

        //animates paddle across the screen
        if (directionOfPaddle_isLeft == true) 
        {paddlePosX = (paddlePosX - 1);}

        else if (directionOfPaddle_isLeft == false) 
        {paddlePosX = (paddlePosX + 1);}

        //redirects paddle
        if (paddlePosX < paddleOnLeftWall) 
        {directionOfPaddle_isLeft = false;}

        else if (paddlePosX > paddleOnRightWall) 
        {directionOfPaddle_isLeft = true;}
    }

});
}    
}
}

这可能是因为你在同一条线上控制球和桨。系统不能从一个线程同时做两件事