0

根据我首先单击的动画,第二个动画将始终触发第一个动画。我不知道我做错了什么。

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>
4

2 回答 2

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();
        }

    });

AnimationsetAnimation()对象在调用或时附加到它们的视图startAnimation()。通过启动附加到同一个Animation实例的两个视图,动画有可能在运行时影响两个视图的绘制。视图仅在无效时才关注动画的当前状态,因此这并不一定意味着它们将始终一起动画。但是,如果您的代码ImageView在运行时遇到两个实例都无效的情况Animation,它们都会响应翻译。

于 2013-01-30T20:11:47.477 回答
0

尝试使用 >>new View.OnClickListener()代替new OnClickListener()

于 2013-01-30T20:12:32.233 回答