我在 RelativeLayout 下创建了带有动画的 Button,我想再次刷新 Button 并重新启动动画..但我做不到。我的声音是
布局:
<RelativeLayout
android:id="@+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/top"
android:gravity="center" >
<Button
android:contentDescription="Animation"
android:id="@+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_gravity="center"
android:background="@drawable/ic_ballon"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:textSize="35sp"
android:text="8"
android:paddingBottom="35dp"/>
<Button
android:contentDescription="Animation"
android:id="@+id/image2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_toRightOf="@+id/image1"
android:layout_gravity="center"
android:background="@drawable/ic_ballon_1"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:textSize="35sp"
android:text="8"
android:paddingBottom="35dp"/>
</RelativeLayout>
动画片:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/bounce_interpolator">
<translate
android:fromYDelta="-100%p"
android:toYDelta="0"
android:duration="3000"/>
</set>
活动:
//Animation
final Animation animBounce = AnimationUtils.loadAnimation(this, R.anim.bounce);
final Animation animBounce1 = AnimationUtils.loadAnimation(this, R.anim.bounce1);
//Random Number
Random randomGenerator1 = new Random();
Random randomGenerator2 = new Random();
final int randomInt1 = randomGenerator1.nextInt(20);
final int randomInt2 = randomGenerator2.nextInt(20);
Button number1 = (Button) findViewById(R.id.image1);
number1.setText(Integer.toString(randomInt1));
Button number2 = (Button) findViewById(R.id.image2);
number2.setText(Integer.toString(randomInt2));
number1.startAnimation(animBounce);
number2.startAnimation(animBounce1);
number1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
RelativeLayout hidden = (RelativeLayout) findViewById(R.id.btn);
hidden.invalidate();
number1.refreshDrawableState();
number2.refreshDrawableState();
}
});
number2.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
RelativeLayout hidden = (RelativeLayout) findViewById(R.id.btn);
hidden.invalidate();
number1.refreshDrawableState();
number2.refreshDrawableState();
}
});
所以,我想刷新 Button 以显示动画,并在 onClick 时再次显示。我使用下面的代码,但它对我不起作用:
RelativeLayout hidden = (RelativeLayout) findViewById(R.id.btn);
hidden.invalidate();
number1.refreshDrawableState();
number2.refreshDrawableState();
如何通过onClick刷新按钮并重新启动动画?