【问题标题】:Moving an image through a linearlayout通过线性布局移动图像
【发布时间】:2013-05-15 07:31:20
【问题描述】:

我正在开发一个 Android 2.2 应用程序。

我想将图像从屏幕左侧移动到屏幕右侧。

我该怎么做?我读到我必须将此图像添加到 ListView 或 GridView 才能设置此动画。

更新

我创建了以下文件:

anim/translate_right

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

anim/ship_layout_controller

<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
        android:delay="10%"
        android:animationOrder="reverse"
        android:animation="@anim/translate_right" />

布局/起始页

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <TextView 
        android:id="@+id/appNameTextView"
        android:text="@string/app_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="40px"/>
    <Button
        android:id="@+id/PlayButton"
        android:text="@string/play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="40px"/>
    <AbsoluteLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        xmlns:android="http://schemas.android.com/apk/res/android">
        <ImageView
            android:id="@+id/greekShip"
            android:persistentDrawingCache="animation|scrolling"
            android:layoutAnimation="@anim/ship_layout_controller"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/greekship"
            android:maxWidth="176px"
            android:maxHeight="87px"
            android:layout_x="-300px"/>
    </AbsoluteLayout>
</LinearLayout>

StartActivity.java

public class StartActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.startpage);
    }

    @Override
    protected void onResume() {
        super.onResume();
        ImageView ship = (ImageView)findViewById(R.id.greekShip);

        ship.startAnimation(AnimationUtils.loadAnimation(this, R.anim.translate_right));
    }
}

但它不起作用。

【问题讨论】:

  • 我听说我必须将此图像添加到 ListView 或 GridView 才能设置此动画。您误读了。嗯……你看过 SDK 中的动画示例了吗?
  • 移动意味着动画?如果是这样,您可以查看 Animation 类(一个简单的 Translate 动画应该可以做到)。如果您只想让它向右对齐,请将 Layout Gravity 设置为右侧。
  • @Cristian:我正在阅读 Apress 图书 Pro Android 2,它是这么说的。
  • @Zarah:是的,我想为从左到右的帆设置动画。
  • @VansFannel:好的,让我按顺序解释一下:你不需要 ListView 和 GridView 来做这个动画。所以,你看错了,或者这本书是错的(我认为这是不可行的)。

标签: android animation imageview android-2.2-froyo


【解决方案1】:

请查看Animation 类,特别是Tween Animation,更具体地说是Translate element。在您的项目中创建一个动画文件,然后将此动画应用于您的图像。例如,此动画会将一个对象从屏幕中心移动到右侧。

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

编辑:将此动画应用于 Button、TextView、ImageView 等。

ImageView imageView = (ImageView) findViewById(R.id.myImageView);
Animation exitAnimation = AnimationUtils.loadAnimation(this, R.anim.exit_animation);
imageView.startAnimation(exitAnimation);

【讨论】:

  • 我读到补间动画是针对视图内的元素而不是移动整个视图。
  • 你至少试过了吗?毕竟,图像视图。
  • 我想移动一个 ImageView,而不是 imageView 中的图像。
  • 补间动画 补间动画应用于视图,让您定义一系列位置、大小、旋转和不透明度的变化,以动画视图内容。
  • @SSHThis:你有没有尝试做一个项目>清理?检查您使用的 R.anim 引用的文件名是否正确。
【解决方案2】:

你必须使用翻译动画。例如,此动画会将图像从屏幕左侧移动到屏幕右侧。

AnimationActivity 是

    listener = new AnimationListener() {
        @Override 
        public void onAnimationStart(Animation animation) {}
        @Override 
        public void onAnimationRepeat(Animation animation) {}
        @Override
        public void onAnimationEnd(Animation animation) {
            System.out.println("End Animation!");
            //load_animations();
        }
    };

     }
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    if (v==button)
    {
        moveLefttoRight = new TranslateAnimation(0, 200, 0, 0);
        moveLefttoRight.setDuration(1000);
        moveLefttoRight.setFillAfter(true);
        my_image.startAnimation(moveLefttoRight);
    }
    }

  }

xml代码是

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<ImageView 
   android:id="@+id/diceid"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:background="@drawable/dice"/>

</LinearLayout>

动画文件是

  <?xml version="1.0" encoding="utf-8"?>
  <rotate
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:fromDegrees="0"
   android:toDegrees="360"
   android:pivotX="50%"
   android:pivotY="50%"
   android:duration="2000"
   android:repeatMode="reverse"
   android:repeatCount="infinite"
   android:startOffset="0"
  />

可能这就是您正在寻找的答案。

【讨论】:

  • 这太棒了!我想在布局中转换到它的位置并同时淡入,而不是旋转,怎么样?
【解决方案3】:

我会再次检查您的来源。也许它脱离了上下文。但最简单的方法是使用源自图像起始位置的翻译动画并将其翻译到屏幕的另一侧。完成之后,我通常会注册一个动画回调,以便在动画结束后更新图像的真实位置。希望对您有所帮助。

【讨论】:

    【解决方案4】:

    还是您正在寻找的Gallery

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多