Android动画片在Android 2.2中翻译

时间:2011-09-12 10:49:34

标签: android animation translate-animation

我有两个图像视图,可以转换为单击。 动画适用于一个视图,但对于第二个图像视图,我的动画不符合提供的坐标。

当我点击顶部图像视图(img1)时,它会向底部图像视图(img2)正确动画。但是当我单击底部图像视图时,它会从某处向下动画并仅向图像视图2初始位置移动。虽然预期的行为是,它应该从其位置动画到顶部图像视图(img1)的初始位置。

我的xml是

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

    <ImageView  
        android:layout_width="100dp" 
        android:layout_height="100dp" 
        android:src="@drawable/letter_f"
        android:layout_alignParentTop="true"
        android:id="@+id/imgview1"
        android:background="@drawable/chart"/>

    <ImageView android:layout_height="100dp" 
        android:layout_width="100dp"
        android:id="@+id/imgview2" 
        android:src="@drawable/letter_g" 
        android:background="@drawable/chart" 
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"/>

</RelativeLayout>

我的java类文件是

    public class AnimationDemo extends Activity implements OnClickListener
{   
    private ImageView img1;
    private ImageView img2;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        img1 = (ImageView)findViewById(R.id.imgview1);
        img2 = (ImageView)findViewById(R.id.imgview2);        
        img1.setOnClickListener(this);
        img2.setOnClickListener(this);
     }

    @Override
    public void onClick(View arg0) 
    {
        int x1,y1; // Coordinates of first image view
        int x2,y2; //Coordinates of second image view

        ImageView img = (ImageView)arg0;
        x1 = img1.getLeft();
        y1 = img1.getTop();

        x2 = img2.getLeft();
        y2 = img2.getTop();

        TranslateAnimation slide;
        if(arg0 == img1)
        {
            //translate from img view 1 to img view 2
            slide = new TranslateAnimation(Animation.ABSOLUTE,x1,Animation.ABSOLUTE, x2,Animation.ABSOLUTE, y1,Animation.ABSOLUTE,y2 );
        }
        else
        {
            // translate from img view 2 to img view 1
            slide = new TranslateAnimation(Animation.ABSOLUTE,x2,Animation.ABSOLUTE, x1,Animation.ABSOLUTE, y2,Animation.ABSOLUTE,y1);
        }
        slide.setDuration(1000);   
        slide.setFillAfter(true); 
        img.startAnimation(slide);
    }
}

1 个答案:

答案 0 :(得分:4)

您的问题归因于位置。我相信当动画以绝对像素移动时,它与自身相关。所以在你的第二个动画中,你实质上将它从x2 = 220移动到x1 = 0,并且y2 = 419到y1 = 0。所以它从(currentX + 220,currentY + 419)转移到(currentX + 0,currentY +0)= =

要解决这个实例,只需否定并切换第二张幻灯片声明的值,如下所示:

TranslateAnimation slide;
        if(arg0 == img1)
        {
            //translate from img view 1 to img view 2
            slide = new TranslateAnimation(Animation.ABSOLUTE,x1,Animation.ABSOLUTE, x2,Animation.ABSOLUTE, y1,Animation.ABSOLUTE,y2 );
        }
        else
        {
            // translate from img view 2 to img view 1
//            slide = new TranslateAnimation(Animation.ABSOLUTE,x2,Animation.ABSOLUTE, x1,Animation.ABSOLUTE,y2,Animation.ABSOLUTE,y1);
            slide = new TranslateAnimation(Animation.ABSOLUTE,0,Animation.ABSOLUTE, (-x2),Animation.ABSOLUTE,0,Animation.ABSOLUTE, (-y2));
        }

这只会发生,因为你的左上角精灵在0,0但是。你必须认真地重新思考你是如何移动精灵的。请记住,TranslateAnimation相对于当前位置移动它们,基本上将精灵原始位置设置为(0,0)。

可能是错的,但希望它有所帮助。它对我有用......

很抱歉,花了这么长时间才回复你,我丢失了你的帖子,因某些原因无法再找到它。很高兴您之前评论过!