我想在屏幕上移动一个图像,我可以这样做,但不正确。图像向下,很好,我希望它开始向另一个方向上升,一旦它移动到屏幕底部。
这是我尝试过的。在下面的代码中,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();
}
}发布于 2014-03-24 12:39:45
此代码有两个重叠的条件语句。但是,只有当第一个失败时,才会检查第二个。第二个条件是:
(j >= margenmaxy - height)这自动暗示(j < margenmaxy),因为margenmaxy和身高都是正的。当您检查这样的情况时:
if(j<margenmaxy) {
// Do downward animation.
} else if(j>=margenmaxy-height){
// Do upward animation.
}所发生的是,在图像向下移动的阶段,你所期望的会发生什么。但是,当您尝试向上移动图像时,(j < margenmaxy)条件再次得到满足,并且代码试图向下移动图像。当你放下它的时候,第一个条件就不再有效了。因为在循环中有这个构造,所以它会产生“跳跃”行为。
要解决这个问题,您需要更改逻辑。一个简单的方法是让一个布尔值保持动画的状态。此状态变量将根据图像是否已到达屏幕底部而更改。
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!作为一个额外的注意,因为你是移动球左右,你可能会最终击中这些边界以及。你真的应该对此进行更好的检查。第二个注意事项是,这可能不是在Android中实现移动图像的最佳方式。最后,一个更普遍的解决方案是使用Android 财产动画和您想要的内插器。如果您只想这样做一次,视图动画也可能足够。
https://stackoverflow.com/questions/22608643
复制相似问题