0

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
PearsonArtPhoto
  • 38,970
  • 17
  • 111
  • 142
harikrishnan
  • 1,985
  • 4
  • 32
  • 63
  • why dont you draw Bitmaps directly? – pskink Feb 14 '14 at 14:42
  • i just want to move the object is enough for me.i have taken object x,y coordinates.this x,y position is correct only. but, i cant move object on specified place.its moving wrong place.. – harikrishnan Feb 15 '14 at 17:47
  • my problem is two place maximum. one for getting object position using readLocation method and specifying the position on transAnimation= new TranslateAnimation(sourceX, destinationX,sourceY,destinationY); – harikrishnan Feb 15 '14 at 17:50
  • here i made mistake. can anybody found means, please tell me.. – harikrishnan Feb 15 '14 at 17:51
  • It would help if you could be more specific as to what you are trying to do, and what went wrong. Please try to explain both of these. – PearsonArtPhoto Feb 17 '14 at 12:52
  • Take your image over surfaceview and on surface view you can easily move image wherever you want – Aamirkhan Feb 17 '14 at 12:54
  • nothing Pearson. i just want to move the image or button from one place to another place(specified position). that's it.. – harikrishnan Feb 17 '14 at 13:13
  • here I used translate animation. its working.but, object moving wrong place. – harikrishnan Feb 17 '14 at 13:14

2 Answers2

0

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.

Quamber Ali
  • 2,170
  • 25
  • 46
0

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.

Community
  • 1
  • 1
Vikram
  • 51,313
  • 11
  • 93
  • 122
  • but, need one more help.. after translate the object from one place to another place(specified position), its keep animation state(using transAnimation.setFillAfter(true)).. at that time, when i click the button, onclick listener is not working..when i click imagetwo place, that place only, animation started previous same.. – harikrishnan Feb 19 '14 at 07:21
  • when i start click imagetwo, its translate to imagethree place.fine.. again, when i click imagetwo, imagetwo need to go to existing started place..can u please guide me.. – harikrishnan Feb 19 '14 at 07:23
  • @harikrishnan This would require some extra work, as we need to keep track of where imageTwo currently is. Also, `setFillAfter(true)` only sets the visual position. The `ImageView` stays where it was before the animation - this is the behavior you are witnessing now. For your purpose, you need to change the `LayoutParams` for imageTwo on animation end. I have created a sample project based on your code that does this. You can look at, and use it from [here](http://pastebin.com/HptUCQqx). – Vikram Feb 19 '14 at 18:17
  • thank you so much vikram.I done successfully.but one thing pls,again imagetwo is came from existing position. when i click, onclick listener, its not working. what i want means, repeaddely i need to do animation from source to destinatio,and destination to starting position like that continuously. all the time, i need to work onclick listener for that imagetwo button. – harikrishnan Feb 20 '14 at 14:04
  • Hi Vikram,please help me to solve this issue.i will give more points for this also.. – harikrishnan Feb 24 '14 at 05:46
  • @harikrishnan Sorry Harikrishan, I was busy with school work. Please explain the issue one more time as I am not able to understand what your desired output is. If you can, join me in chat: [Link](http://chat.stackoverflow.com/rooms/48223/translate-the-imageview). – Vikram Feb 24 '14 at 06:18