Moving an image through a linearlayout

你说的曾经没有我的故事 提交于 2020-01-11 12:03:09

问题


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.

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.

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>

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

layout/startpage

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


回答1:


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>

EDIT: 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);



回答2:


you have to use Translate Animation. For example, this animation would move an image from Left side of the screen to the right side of the screen.

The AnimationActivity is

    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 code is

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

and anim file is

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

may be this is the answer what you are searching.




回答3:


I would check your source again. Maybe it's out of context. But the easiest way to do this is to use a Translate animation that originates from the Image's starting place and translates it to the other side of the screen. After that is done I usually register an animation callback so that I can update the true position of the image after the animation is over. hope that helps.




回答4:


Or is it the Gallery that you are looking for?



来源:https://stackoverflow.com/questions/5058449/moving-an-image-through-a-linearlayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!