我想在屏幕上移动图像,我能够做到这一点,但不是很正确。图像向下移动很好,我希望它一旦移动到屏幕底部就开始向另一个方向向上移动。
这是我尝试过的。在下面的代码中,margenMaXX
是屏幕的宽度,margenmaxy
是屏幕的高度
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
Thread myThread = new Thread(new UpdateThread());
myThread.start();
public class UpdateThread implements Runnable {
@Override
public void run() {
//... code to manipulate position
while (i<margenMaxX){
if(j<margenmaxy) {
try {
runOnUiThread(new Runnable() {
@Override
public void run() {
/*mDrawable.setBounds(i, j ,i+ width, i+ height);
mDrawable.draw(cc);
invalidate();*/
}
});
Thread.sleep(200);
i=i+10;
j=j+10;
} catch (InterruptedException e) {
e.printStackTrace();
}
}else if(j>=margenmaxy-height){
try {
runOnUiThread(new Runnable() {
@Override
public void run() {
/*mDrawable.setBounds(i, j ,i+ width, i+ height);
mDrawable.draw(cc);
invalidate();*/
}
});
Thread.sleep(200);
i=i-10;
j=j-10;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
public class AnimatedView extends ImageView {
public AnimatedView(Context context) {
super(context);
// TODO Auto-generated constructor stub
mDrawable = new ShapeDrawable(new OvalShape());
mDrawable.getPaint().setColor(0xffffAC23);
}
protected void onDraw(final Canvas cc) {
final Context context = null;
mDrawable.setBounds(i, j ,i+ width, i+ height);
mDrawable.draw(cc);
invalidate();
}
}
更新1:
使用此代码,球在击中地面后向上并向另一侧移动。现在,我希望球在击中右边界时回来。我为此做了编码,但它没有回来。我的最终目标是开发一种球必须来自左或右的比赛。它必须撞到地面并朝相反的方向行进,撞到墙上然后回来。只要游戏正在进行,球就必须完成这项工作。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
Thread myThread = new Thread(new UpdateThread());
myThread.start();
public class UpdateThread implements Runnable {
boolean mMoveDown=false;
boolean mMoveUp = false;
@Override
public void run() {
while(!mMoveUp) {
// Move the image down and right.
try {
runOnUiThread(new Runnable() {
@Override
public void run() {
}
});
Thread.sleep(200);
i=i+10;
j=j+10;
// Try posting a runnable to the UI thread to update the view.
} catch (InterruptedException e) {
e.printStackTrace();
}if(j >= margenmaxy)
{
// Change to moving up phase.
mMoveUp = true;
}
}
while(mMoveUp){
try {
runOnUiThread(new Runnable() {
@Override
public void run() {
}
});
Thread.sleep(200);
i=i + 10;
j=j - 10;
} catch (InterruptedException e) {
e.printStackTrace();
} if(i >= margenMaxX)
{
// Change to moving up phase.
mMoveDown = true;
}
}while(mMoveDown){
try {
runOnUiThread(new Runnable() {
@Override
public void run() {
}
});
Thread.sleep(200);
i=i - 10;
j=j + 10;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class AnimatedView extends ImageView {
public AnimatedView(Context context) {
super(context);
// TODO Auto-generated constructor stub
mDrawable = new ShapeDrawable(new OvalShape());
mDrawable.getPaint().setColor(0xffffAC23);
}
protected void onDraw(final Canvas cc) {
final Context context = null;
mDrawable.setBounds(i, j ,i+ width, j+ height);
mDrawable.draw(cc);
invalidate();
}
}
答案 0 :(得分:0)
此代码有两个重叠的条件语句。但是,仅在第一个失败时才检查第二个。第二个条件是:
(j >= margenmaxy - height)
这自动暗示 (j&lt; margenmaxy) 因为margenmaxy和身高都是正面的。当你检查这样的条件时:
if(j<margenmaxy) {
// Do downward animation.
} else if(j>=margenmaxy-height){
// Do upward animation.
}
您所期望的是图像向下移动的阶段会发生什么。但是一旦你尝试向上移动图像,再次满足(j 要解决此问题,您需要更改逻辑。一种简单的方法是使用布尔值来保持动画的状态。此状态变量根据图像是否已触及屏幕底部而改变。 作为补充说明,由于你左右移动球,你最终也可能会击中这些边界。你真的应该实现更好的检查。作为第二个注释,这可能不是在Android中完成动态图像的最佳方式。最后,更通用的解决方案是将Android Property Animation与您想要的插补器一起使用。如果您只想这样做一次,View Animation也可以满足要求。public class UpdateThread implements Runnable {
// Variable to store the animation state.
boolean mMoveUp = false;
@Override
public void run() {
//... code to manipulate position
while (i<margenMaxX) {
// IF the animation is in the moving down phase
if(!mMoveUp) {
// Move the image down and right.
i += 10;
j += 10;
// Try posting a runnable to the UI thread to update the view.
try {
runOnUiThread(new Runnable() {
@Override
public void run() {
/*mDrawable.setBounds(i, j ,i+ width, i+ height);
mDrawable.draw(cc);
invalidate();*/
}
});
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// We're moving upwards.
else {
// Move up and left
i -= 10;
j -= 10;
try {
runOnUiThread(new Runnable() {
@Override
public void run() {
/*mDrawable.setBounds(i, j ,i+ width, i+ height);
mDrawable.draw(cc);
invalidate();*/
}
});
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
} // Catch!
} // Else!
// Check to see if we've hit the bottom of the screen
if(j >= margenmaxy - height)
{
// Change to moving up phase.
mMoveUp = true;
}
// Note, you can use an altered form of this to switch back and forth
// between moving up and down.
} // while(i < margenmaxx)!
} // run()!
} // UpdateThread!