translate the imageview(object like button) original(where placed)place to another specified position on screen in android programmatically

StackOverflow https://stackoverflow.com/questions/21781070

Вопрос

I am trying to translate my image to another specified place..i tried here in code given.but, its going wrong way translated..

Please anyone help me to come out this issue..

package com.example.numbercount1;

import com.nineoldandroids.animation.ObjectAnimator;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class TestingAnimation extends Activity implements OnClickListener {


Button btn1 ,btn2 ;
float fromXposition ,fromYPosition , toXPosition , toYPosition ;
TranslateAnimation transAnimation ;
RelativeLayout mainScreen ;
ImageView imageOne , imageTwo , imageThree ;
float sourceX , sourceY , destinationX , destinationY ;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.testing_activity_main);


     mainScreen = (RelativeLayout) findViewById(R.id.mainscreen);

     imageOne = (ImageView) findViewById(R.id.imageView1);
     imageTwo = (ImageView) findViewById(R.id.imageView2);
     imageThree = (ImageView) findViewById(R.id.imageView3);

     imageOne.setOnClickListener(this);
     imageTwo.setOnClickListener(this);
     imageThree.setOnClickListener(this);

     readLocation("onCreate");


}

private void readLocation(String str) {
    Log.v("hari", "str:"+str);

      DisplayMetrics displayMetrics = new DisplayMetrics();
      getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

      int offsetX = displayMetrics.widthPixels - mainScreen.getMeasuredWidth();
      int offsetY = displayMetrics.heightPixels - mainScreen.getMeasuredHeight();


     int[] locationInWindow = new int[2];
     imageTwo.getLocationInWindow(locationInWindow);
      int[] locationOnScreen = new int[2];
      imageTwo.getLocationOnScreen(locationOnScreen);

      sourceX = locationOnScreen[0];
      sourceY = locationOnScreen[1];

Log.v("hari", "getLocationInWindow:--locationInWindow:"
 +locationInWindow[0]+", locationInWindow:"+locationInWindow[1]); 
 Log.v("hari", "getLocationOnScreen:--locationInWindow:"
 +locationOnScreen[0]+", locationInWindow:"+locationOnScreen[1]);

      Log.v("hari", "readLocation:--offsetX:"+offsetX+", offsetY:"+offsetY); 

      Log.v("hari","---------------------------------------------");

          int[] locationInWindowSecond = new int[2];
         imageThree.getLocationInWindow(locationInWindowSecond);
          int[] locationOnScreenSecond = new int[2];
          imageThree.getLocationOnScreen(locationOnScreenSecond);

     Log.v("hari","getLocationInWindow:--locationInWindowSecond:"
  +locationInWindowSecond[0]+", locationInWindowSecond:"+locationInWindowSecond[1]); 
 Log.v("hari","getLocationOnScreen:--locationOnScreenSecond:"
 +locationOnScreenSecond[0]+", locationOnScreenSecond:"+locationOnScreenSecond[1]);

Log.v("hari", "readLocation:--offsetX:"+offsetX+", offsetY:"+offsetY); 

          destinationX = locationOnScreenSecond[0];
          destinationY = locationOnScreenSecond[1];



}

@Override
 public void onWindowFocusChanged(boolean hasFocus) {
  // TODO Auto-generated method stub
  super.onWindowFocusChanged(hasFocus);

  readLocation("onWindowFocusChanged");
 }

@SuppressLint("NewApi")
@Override
public void onClick(View v) {

    switch (v.getId()) {

    case R.id.imageView2 :

        Log.v("hari", "onclick image button2");

        fromXposition = v.getX() ;
        fromYPosition = v.getY() ;

Log.v("hari", "fromXposition:"+fromXposition+"--fromYPosition:"+fromYPosition);

        toXPosition = imageThree.getX() ;
        toYPosition = imageThree.getY() ;

Log.v("hari", "toXPosition:"+toXPosition+"--toYPosition:"+toYPosition);

//transAnimation= new TranslateAnimation(sourceX, sourceY,
 destinationX, destinationY);
transAnimation= new TranslateAnimation(sourceX, destinationX,sourceY,destinationY);
         transAnimation.setDuration(3000);
         v.startAnimation(transAnimation);

   ObjectAnimator transAnimationNew= ObjectAnimator.ofFloat(v, "moving",
 destinationX, destinationY);
            transAnimationNew.setDuration(500);
            transAnimationNew.start();

        break;

    default:
        break;
    }


}
 }

----------------------------------------------------------------
  testing_activity_main.xml.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:id="@+id/mainscreen"
tools:context=".MainActivity" >

<RelativeLayout
    android:id="@+id/relative_subContainer"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:background="@drawable/curvedborder_new" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="184dp"
        android:layout_toLeftOf="@+id/imageView2"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/imageView2"
        android:layout_alignTop="@+id/imageView2"
        android:layout_marginRight="139dp"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignTop="@+id/imageView1"
        android:layout_marginLeft="416dp"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>

</RelativeLayout>


 Thanks Advance
Это было полезно?

Решение

You want to translate imageTwo to imageThree, am I right?

If so, the parameters you pass to TranslateAnimation's constructor are wrong.

Here are the four parameters:

float fromXDelta  ==> starting xValue RELATIVE to the view 
-> ZERO in your case // imageTwo starts from where it is

float toXDelta    ==> final xValue RELATIVE to the view
-> (destinationX - sourceX) in your case // imageTwo travels this much in X

float fromYDelta  ==> starting yValue RELATIVE to the view 
-> ZERO in your case // imageTwo starts from where it is

float toYDelta    ==> final yValue RELATIVE to the view
-> (destinationY - sourceY) in your case // imageTwo travels this much in Y

Modified case for R.id.imageView2:

case R.id.imageView2 :

    transAnimation= new TranslateAnimation(0f, 
                                          (destinationX - sourceX),
                                           0f,
                                          (destinationY - sourceY));
     transAnimation.setDuration(3000);
     v.startAnimation(transAnimation);

    break;

If you want imageTwo to 'stay' after the animation, attach an AnimationListener to transAnimation and change the LayoutParams for imageTwo accordingly in onAnimationEnd.

One more thing: Look at the comments to this(Link) answer. Are you sure you're getting valid values for positions? A safe thing to do would be to call readLocation(String str) from onClick(View):

@Override
public void onClick(View v) {
    readLocation("onClick");

    switch(v.getId()) {
        ....
        ....
    }
}

And, locationOnScreen(int[]) is enough for your purpose. locationInWindow(int[]) is not required.

Другие советы

1st I suggest that you use NineOldDroid for view animations as its easy to use and you get Rid of the respective points of touch for translation.

e.g for Translation

AnimatorSet set = new AnimatorSet();
                set.playTogether(
                        ObjectAnimator.ofFloat(dragView, "x", holderLoc[0]),
                        ObjectAnimator.ofFloat(dragView, "y", holderLoc[1]));
                set.setDuration(500).start();
                set.addListener(new AnimatorListener() {

                    @Override
                    public void onAnimationStart(Animator animation) {

                    }

                    @Override
                    public void onAnimationRepeat(Animator animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {

                    }

                    @Override
                    public void onAnimationCancel(Animator animation) {


                    }
                }); 

Secondly you said that you got the coordinate's right so i didn't checked that but you are getting the View's Co-ordinates in the Window and when you move it to that location but variables are set accordingly to parent View. So do 1 thing from there

1) either make the Views Direct subviews of the Relative Layout

2) Calculate the right values as per the parents.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top