在按钮上单击动画必须被触发

时间:2014-08-16 09:11:24

标签: android

我有这个按钮已经链接到一些字符串数组。我需要的是,当点击相同的按钮时,必须播放动画。当我从第一次活动到第二次活动时,我有这个动画:

overridePendingTransition(R.anim.animation , R.anim.animation2);

每次点击按钮我想要相同的动画,我该怎么做?

这是我的其余代码:

public class MyActivity2 extends Activity {
    private String[] colors;
    private String[] values;
    private TextView tv;
    private RelativeLayout rl;
    int index = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_activity2);
    Button n = (Button) findViewById(R.id.button);
    tv = (TextView) findViewById(R.id.textView);
    rl = (RelativeLayout) findViewById(R.id.layout_view);

    Typeface typeface = Typeface.createFromAsset(getAssets(), "BebasNeue Bold.ttf");
    n.setTypeface(typeface);
    Typeface face = Typeface.createFromAsset(getAssets(),"OSP-DIN.ttf");
    tv.setTypeface(face);

    values = getResources().getStringArray(R.array.things_array);
    colors = getResources().getStringArray(R.array.colorcode_array);

    n.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Random RAND = new Random();
            int position = RAND.nextInt(colors.length);
            int position2 = (index++);
            String nextValue = colors[position];
            String textValue = values[position2];
            overridePendingTransition(R.anim.animation , R.anim.animation2);
            tv.setText(textValue);
            rl.setBackgroundColor(Color.parseColor(nextValue));


            }
        });
    }

@Override
public void onBackPressed() {
    super.onBackPressed();
    overridePendingTransition(R.anim.animation4, R.anim.animation3);
    }
}

和我的活动的.xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MyActivity2"
android:background="#ffdb4b5e">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Next"
    android:id="@+id/button"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:textColor="#ffffffff"
    android:textSize="72sp"
    android:background="#00000000"
    />

<TextView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/thing1"
    android:id="@+id/textView"
    android:textColor="#ffffffff"
    android:textSize="36sp"
    android:gravity="center"
    android:layout_above="@+id/button"
    />


</RelativeLayout>

anim.xml

<?xml version="1.0" encoding="utf-8" ?> 
<translate xmlns:android="http://schemas.android.com/apk/res/android"
 android:fromXDelta="100%p" android:toXDelta="0" android:duration="500" /> 

anim2.xml

<?xml version="1.0" encoding="utf-8" ?> 
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0" android:toXDelta="-50%p" android:duration="500" /> 

1 个答案:

答案 0 :(得分:1)

    n.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Some code
            v.startAnimation(AnimationUtils.loadAnimation(MyActivity2.this, R.anim.animation));
        }
    });