我正在 android studio 中制作一个项目。我试图在运行应用程序时通过线程移动背景图像,但是当我运行应用程序时,它会给出一条消息“不幸停止”。如何通过线程或其他方式移动背景图像 使用安卓工作室?有人请帮帮我吗?
//Xml:
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/bg2"
android:scaleType="fitXY"
android:id="@+id/image_id"
/>
//java:
image=(ImageView)findViewById(R.id.image_id); // image is an ImageView type object
Thread an=new Thread(){
@Override
public void run() {
super.run();
for(;x<=200;)
{
x++;
image.setX(x);
image.setY(0);
System.out.println("Value of x: "+x);
try
{
sleep(1000);
}catch(Exception e)
{
System.out.println("Exception: "+e);
}
}
}
};an.start();
最佳答案
您可以在您的线程中使用 TranslateAnimation,结帐 here和 here了解更多信息
ImageView img_animation = (ImageView) findViewById(R.id.img_animation);
TranslateAnimation animation = new TranslateAnimation(0.0f, 400.0f,
0.0f, 0.0f); // new TranslateAnimation(xFrom,xTo, yFrom,yTo)
animation.setDuration(5000); // animation duration
animation.setRepeatCount(5); // animation repeat count
animation.setRepeatMode(2); // repeat animation (left to right, right to left )
//animation.setFillAfter(true);
img_animation.startAnimation(animation); // start animation
关于java - Android 中的后台移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27134216/