Android 动画 在 android 2.2 中翻译

发布于 2024-12-03 21:02:04 字数 2615 浏览 0 评论 0原文

我有两个图像视图,点击即可翻译。 动画对于一个视图正常工作,但对于第二个图像视图,我的动画不根据提供的坐标。

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

i have two image views which translates on click.
the animation works properly for one view but for second image view , my animation is not according to coordinates provided.

when i click top image view (img1) it animates properly toward bottom image view (img2) . But when i click the bottom image view, it animates from somewhere down and move towards image view 2 initial position only. though the expected behaviour is, it should animate from its position to top image view (img1) initial position.

My xml is

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

and my java class file is

    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);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苏佲洛 2024-12-10 21:02:04

您的麻烦是由于您的位置造成的。我相信当动画以绝对像素移动时,它是相对于自身的。因此,在第二个动画中,您实质上是将其从 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)。

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

抱歉,花了这么长时间才回复您,我丢失了您的帖子,并且由于某种原因无法再次找到它。很高兴您早些时候发表评论!

Your troubles are due to your locations. I believe when animations are moved with absolute pixels it is relative to itself. So on your second animation you were in essence moving it from x2=220 to x1=0, and y2=419 to y1=0. So it was moving from (currentX+220, currentY+419) to (currentX +0, currentY +0) which = itself

To solve this instance simply negate and switch the values of the second slide declaration like so:

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));
        }

This only happens because your top left sprite is at 0,0 though. You have to seriously rethink how you're moving your sprites around. Just remember, the TranslateAnimation moves them relative to their current positions, basically setting the sprites original location to (0,0).

Could be wrong, but hope it helps. It worked for me...

Sorry it took so long to get back to you, I lost your post and couldn't find it again for some reason. Glad you had commented earlier!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文