根据我首先点击的动画,第二个动画将始终设置第一个动画。我不知道我做错了什么。
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class Test extends Activity {
ImageView img_left;
ImageView img_right;
Button left;
Button right;
TranslateAnimation moveup;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
moveup = new TranslateAnimation(0, 0, 0, -900);
moveup.setDuration(2000);
moveup.setFillAfter(true);
left = (Button) findViewById(R.id.left);
right = (Button) findViewById(R.id.right);
img_left = (ImageView) findViewById(R.id.image_left);
img_right = (ImageView) findViewById(R.id.image_right);
right.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
img_right.startAnimation(moveup);
Toast.makeText(getApplicationContext(), "right", Toast.LENGTH_LONG).show();
}
});
left.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
img_left.startAnimation(moveup);
Toast.makeText(getApplicationContext(), "left", Toast.LENGTH_LONG).show();
}
});
}
}
布局:
<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:background="#ffffff"
tools:context=".First" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" >
<Button
android:id="@+id/left"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#0000EE"
android:text="" />
<Button
android:id="@+id/right"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#0000EE"
android:text="" />
</LinearLayout>
<ImageView
android:id="@+id/image_left"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:src="@drawable/cue1" />
<ImageView
android:id="@+id/image_right"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:src="@drawable/cue2" />
</RelativeLayout>
答案 0 :(得分:2)
创建两个具有相同属性的Animation
个对象,一个用于左侧,另一个用于右侧,或者在启动之前在另一个视图上调用setAnimation(null)
。换句话说:
right.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
img_left.setAnimation(null);
img_right.startAnimation(moveup);
Toast.makeText(getApplicationContext(), "right", Toast.LENGTH_LONG).show();
}
});
left.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
img_right.setAnimation(null);
img_left.startAnimation(moveup);
Toast.makeText(getApplicationContext(), "left", Toast.LENGTH_LONG).show();
}
});
调用Animation
或setAnimation()
时, startAnimation()
个对象会附加到其视图中。通过启动附加到同一Animation
实例的两个视图,动画可以在运行时影响两个视图的绘制。视图仅在动画无效时关注动画的当前状态,因此这并不一定意味着它们将始终一起动画。但是,如果您的代码遇到ImageView
个实例在Animation
运行时无效的情况,则它们都会响应转换。
答案 1 :(得分:0)
尝试使用&gt;&gt; new View.OnClickListener()
取代new OnClickListener()