java - Android 中的后台移动

标签 java android

我正在 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,结帐 herehere了解更多信息

 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/

相关文章:

java - 默认按钮的方法

Android facebook SDK 执行错误 java.lang.NoClassDefFoundError

Java Applet 和 html 页面 - 嵌入多个 applet

java - 在Java中保存环境变量

java - 为什么我收到 java.lang.StringIndexOutOfBounds 异常?

android - 那里有 AppWidgetHost 教程吗?

android - 无法在模拟器中检测到软键盘的删除按钮

android - 将 float 操作按钮变形为相关操作

Java泛型类型删除类型转换

java - OverlayLayout 好像没有分层