Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6685447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:03:31+00:00 2026-05-26T05:03:31+00:00

i have two image views which translates on click. the animation works properly for

  • 0

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);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T05:03:32+00:00Added an answer on May 26, 2026 at 5:03 am

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a linearlayout in which i have one image view, one textview ,
I have a View in my MVC3 application which displays one of two pretty
I have two methods, one that I use to convert an image to a
I have a NavigationBar app with two views: a parent and a sub view.
I'm trying to create an animation to transition between two views, both of which
If I have two images which are both the left side view of a
In my view controller I have two UIScrollViews . One is to display latest
I have a activity where I have two imageViews. In onTouch I recognize which
I have a layout that contains two ImageViews. I want one of them to
I have two models, Image and Tag . Each Image object can have more

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.