通过线性布局移动图像

时间:2011-02-20 16:51:46

标签: android animation imageview android-2.2-froyo

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

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

我该怎么做?我已经读过,我必须将此图像添加到ListView或GridView以设置此动画。

更新

我创建了以下文件:

动画/ 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>

动画/ 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));
    }
}

但它不起作用。

4 个答案:

答案 0 :(得分:3)

请查看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);

答案 1 :(得分:3)

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

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"
  />

可能是您正在搜索的答案。

答案 2 :(得分:0)

我会再次查看您的来源。也许它脱离了背景。但最简单的方法是使用源自Image起始位置的Translate动画,并将其转换为屏幕的另一侧。完成之后,我通常会注册一个动画回调,这样我就可以在动画结束后更新图像的真实位置。希望有所帮助。

答案 3 :(得分:0)

或者您正在寻找Gallery