我在屏幕上的某个位置有两个视图(按钮)Button1和Button2,现在当我点击ImageView时有一个ImageView Button1在Button2上移动位置而Button2在Button1位置上移动,如下图所示
现在,当我再次在ImageView上单击相同的过程重复时,在其位置上交换两个按钮 我已经实现了以下代码,但它不合适
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:background="#000000" >
<include
android:id="@+id/notelist"
layout="@layout/first_view" />
<include
android:id="@+id/notelist"
layout="@layout/second_view" />
</RelativeLayout>
<Button
android:id="@+id/button1"
android:layout_width="120dp"
android:layout_height="35dp"
android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/relativeLayout1"
android:layout_marginBottom="73dp"
android:layout_marginLeft="15dp"
android:text="BUTTON 1" />
<Button
android:id="@+id/button2"
android:layout_width="120dp"
android:layout_height="35dp"
android:layout_alignBottom="@+id/relativeLayout1"
android:layout_alignLeft="@+id/button1"
android:text="BUTTON 2" />
</RelativeLayout>
first_view.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Layout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/ImageView01"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/switcher" >
</ImageView>
</RelativeLayout>
second_view.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Layout02"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" >
<ImageView
android:id="@+id/ImageView02"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/switcher" >
</ImageView>
</RelativeLayout>
</RelativeLayout>
请使用与截图中显示的图像相似的图像
MainActivity.java
package com.example.flipanimation;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class MainActivity extends Activity {
private ImageView image2;
private boolean isFirstImage = true;
private Button button1, button2;
private float btn1_Xpos, btn2_Xpos, btn1_Ypos, btn2_Ypos;
TranslateAnimation anim, reverseanim;
private RelativeLayout relativeLayout1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
image2 = (ImageView) findViewById(R.id.ImageView02);
image2.setVisibility(View.GONE);
relativeLayout1 = (RelativeLayout) findViewById(R.id.relativeLayout1);
relativeLayout1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (isFirstImage) {
isFirstImage = !isFirstImage;
btn1_Xpos = button1.getX();
btn1_Ypos = button1.getY();
btn2_Xpos = button2.getX();
btn2_Ypos = button2.getY();
anim = new TranslateAnimation(TranslateAnimation.ABSOLUTE,
btn2_Xpos / 4, TranslateAnimation.ABSOLUTE,
btn2_Ypos / 4);
anim.setFillAfter(true);
anim.setDuration(1000);
reverseanim = new TranslateAnimation(
TranslateAnimation.ABSOLUTE, -btn1_Xpos / 4,
TranslateAnimation.ABSOLUTE, -btn1_Ypos / 4);
reverseanim.setFillAfter(true);
reverseanim.setDuration(1000);
anim.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation arg0) {
button2.startAnimation(reverseanim);
}
@Override
public void onAnimationRepeat(Animation arg0) {
}
@Override
public void onAnimationEnd(Animation arg0) {
}
});
button1.startAnimation(anim);
} else {
isFirstImage = !isFirstImage;
btn1_Xpos = button1.getX();
btn1_Ypos = button1.getY();
btn2_Xpos = button2.getX();
btn2_Ypos = button2.getY();
anim = new TranslateAnimation(TranslateAnimation.ABSOLUTE,
-btn1_Xpos / 4, TranslateAnimation.ABSOLUTE,
-btn1_Ypos / 4);
anim.setFillAfter(true);
anim.setDuration(1000);
reverseanim = new TranslateAnimation(
TranslateAnimation.ABSOLUTE, btn2_Xpos / 4,
TranslateAnimation.ABSOLUTE, btn2_Ypos / 4);
reverseanim.setFillAfter(true);
reverseanim.setDuration(1000);
anim.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation arg0) {
button1.startAnimation(reverseanim);
}
@Override
public void onAnimationRepeat(Animation arg0) {
}
@Override
public void onAnimationEnd(Animation arg0) {
}
});
button2.startAnimation(anim);
}
}
});
}
}