【问题标题】:How to move an image from left to right in android如何在android中将图像从左向右移动
【发布时间】:2020-08-11 16:51:49
【问题描述】:

我想使用 android 动画在模拟器上从左到右翻译图像。我是android动画的新手。我怎么能那样做?

谢谢。

【问题讨论】:

    标签: android animation


    【解决方案1】:
        ll = new LinearLayout(this);
        ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        ll.setOrientation(LinearLayout.VERTICAL);
    
        tv = new TextView(this);
        tv.setText("Animation");
    
        moveLefttoRight = new TranslateAnimation(0, 200, 0, 0);
        moveLefttoRight.setDuration(1000);
        moveLefttoRight.setFillAfter(true);
    
        button = new Button(this);
        button.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        button.setText("PressMe");
        button.setOnClickListener(new OnClickListener() {
    
            public void onClick(View v) {
                tv.startAnimation(moveLefttoRight);
            }
    
        });
    
        ll.addView(tv);
        ll.addView(button);
        setContentView(ll);
    

    是一种方法。

    【讨论】:

    • 嗨..谢谢你的回复。我试过你上面的例子。但它只显示“动画”这个词,没有任何动作。
    • @Sudhakar。抱歉错过了Animation.setDuration(1000)Animation.setFillAfter(true)
    • @Pete。道歉。我将从答案中删除链接。感谢您的提醒;)。
    • @techiServices 如何为两个文本从中间到相反方向设置动画?
    • @juned。使用正确的屏幕尺寸值创建两个动画,例如 centerLeft 和 centerRight。所以中心即开始将是屏幕宽度/ 2。左边缘为 0,右边缘为屏幕宽度。如果不清楚,您可以发布一个新问题吗?
    【解决方案2】:

    使用 Android TranslateAnimation 从左到右和从右到左移动图像

    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 
    

    you can find more details from here

    【讨论】:

    • 这是将图像移动到屏幕边界/边缘之外。它应该会反弹到我认为的边缘
    【解决方案3】:

    我参加聚会有点晚了,但值得在这里回答,

    案例 1:

    如果您的视图位于屏幕左侧,并且您想从左边缘移动到右边缘,请使用以下命令:

    imageView.animate()
                .translationX(((rootLayout.width - (imageView.width))).toFloat())
                .setInterpolator(AccelerateInterpolator()).duration = 1500
    

    案例 2: 如果您的视图位于屏幕的中心,并且您想从中心移动到右边缘,请使用:

    imageView.animate()
                .translationX(((rootLayout.width - (imageView.width)) / 2).toFloat())
                .setInterpolator(AccelerateInterpolator()).duration = 1500
    

    注意: rootLayout 是您的 XML 的根视图

    【讨论】:

      【解决方案4】:

      添加此代码 R.anim 文件夹

      <?xml version="1.0" encoding="utf-8"?>
      <set
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:interpolator="@android:anim/linear_interpolator"
      android:fillAfter="true">
      
      <translate
          android:fromXDelta="0%p"
          android:toXDelta="100%p"
          android:duration="800" />
      </set>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-02-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-10
        • 2016-03-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多