通过的LinearLayout移动图片图片、LinearLayout

2023-09-05 04:36:23 作者:一个很2的姑娘。

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

I'm developing an Android 2.2 application.

欲从屏幕的左侧的图像移动到屏幕右侧的

I want to move an image from left side of the screen to the right side of the screen.

我怎么能这样做?我读过,我有这个图像添加到ListView或一个GridView设置这个动画。

How can I do that? I've read that I have to add this image to a ListView or to a GridView to setup this animation.

更新 我创建下列文件: 动画/ translate_right

UPDATE I've created the following files: 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>

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

不过,这是行不通的。

But it doesn't work.

推荐答案

请看一看在的动画类,特别是吐温动画,更具体的翻译元素。在项目中创建一个动画文件,然后将此动画图像。例如,该动画将移动对象从屏幕中心到右侧。

Please take a look at the Animation class, specifically the Tween Animation, more specifically the Translate element. Create an animation file in your project then apply this animation to your image. For example, this animation would move an object from the center of the screen to the right side.

<?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>

编辑:要将此动画一个按钮,一个TextView,一个ImageView的,等等

To apply this animation to a Button, a TextView, an ImageView, etc.

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