将imageview(像对象一样的对象)原始(放置的位置)转换到android中的另一个指定位置

时间:2014-02-14 13:46:58

标签: android android-animation android-3.0-honeycomb translate-animation

我正在尝试将我的图片翻译到另一个指定的地方..我在这里尝试了代码。但是,它的翻译方式错了..

请有人帮我解决这个问题..

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

2 个答案:

答案 0 :(得分:1)

第一,我建议你使用NineOldDroid来查看动画,因为它易于使用,你可以摆脱各自的翻译点。

例如翻译

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) {


                    }
                }); 

其次你说你的坐标是正确的,所以我没有检查过,但是你在窗口中获得了View的坐标,当你将它移动到那个位置时,变量相应地设置为父视图。所以从那里做一件事

1)或者制作相对布局的视图直接子视图

2)根据父母计算正确的值。

答案 1 :(得分:0)

您想将imageTwo翻译为imageThree,我是对的吗?

如果是这样,传递给TranslateAnimation's构造函数的参数是错误的。

以下是四个参数:

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

R.id.imageView2的修改后的案例:

case R.id.imageView2 :

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

    break;

如果您希望imageTwo在动画后“留下”,请将AnimationListener附加到transAnimation并相应地更改LayoutParams imageTwo onAnimationEnd readLocation(String str) 1}}。

还有一件事:查看对此(Link)答案的评论。你确定你获得了有效的头寸价值吗?安全的做法是从onClick(View)致电@Override public void onClick(View v) { readLocation("onClick"); switch(v.getId()) { .... .... } }

locationOnScreen(int[])

并且,locationInWindow(int[])足以满足您的目的。 <{1}}不是必需的。