Search code examples
androidimagebuttonmove

Android image moving on button click


bee to android programming and learning things.

i have the following code with me and what i need is to move only two selected images when button click happen first user will select two images then on button click they will move to left direction

here is my code

 <ImageView
            android:id="@+id/imLady"
            android:layout_width="160dp"
            android:layout_height="238dp"
            android:src="@drawable/lady"
            android:layout_marginTop="50dp"/>


     <ImageView
        android:id="@+id/imLady2"
        android:layout_width="160dp"
        android:layout_height="238dp"
        android:src="@drawable/lady"
        android:layout_marginTop="50dp"/>

      <ImageView

        android:id="@+id/imLady3"
        android:layout_width="160dp"
        android:layout_height="238dp"
        android:src="@drawable/lady"
        android:layout_marginTop="50dp"/>


      <Button
          android:id="@+id/button1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Move" />

looking forward for suggestions,solutions from u guys:) regards


Solution

  • you need to setAnimations to your ImageViews in onClick() function of Button

    ImageView v1,v2;
     Button b;
    TranslateAnimation ta1,ta2;
    ...
     public void onCreate(Bundle savedInstances)
     {
        super.onCreate(savedInstances);
        setContentView(R.layout.layoutname);
        ...
        initiate and refer button and imageviews
        ...
        ta1=new TranslateAnimation(fromx,tox,fromy,toy);
        ta1.setDuration(1000); //1 second
    
        ta2=new TranslateAnimation(fromx,tox,fromy,toy);
        ta2.setDuration(1000); //1 second
    
         b.setOnClickListener(new View.onClickListener(){
         @override
         public void onClick(View arg0)
         {
         v1.startAnimation(ta1);
         v2.startAnimation(ta2);
         }
         });
    
    
      }
    

    refer to TranslateAnimation Documentation here