2

我有两个在点击时翻译的图像视图。动画适用于一个视图,但对于第二个图像视图,我的动画不是根据提供的坐标。

当我单击顶部图像视图 (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);
    }
}
4

1 回答 1

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

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

抱歉这么久才回复您,我丢失了您的帖子,由于某种原因无法再次找到它。很高兴您之前发表了评论!

于 2011-10-07T06:54:14.653 回答