我正在尝试将动画添加到我的线性布局中。以下是我的代码: -
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/mylinearlayout"
android:weightSum="1">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/imageView1"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:scaleType="centerCrop"
android:layout_weight="0.5"
android:src="@mipmap/ic_launcher"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/darker_gray"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/offerImage"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:layout_weight="0.5"
android:layout_gravity="center_horizontal"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/startAnimation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/offerImage"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:layout_weight="0.5"
android:layout_gravity="center_horizontal"
android:text="Start"
/>
</LinearLayout>
</LinearLayout>
在我的活动的onCreate方法中,我喜欢
@Override
protected void onCreate(Bundle savedInstanceState) {
.
.
.
LinearLayout ll_mainLayout = (LinearLayout)findViewById(R.id.mylinearlayout);
TextView tv_data = (TextView) findViewById(R.id.textView1);
Button btn_startAnimation = (Button)findViewById(R.id.startAnimation);
tv_data.setText("Before Animation");
btn_StartAnimation.setOnClickListner(new View.OnClickListener(){
@Override
public void OnClick(View v)
{
Animation animation = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.move);
ll_mainLayout.setAnimation(animation);
ll_mainLayout.setLayoutAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
showData();
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
}
});
}
public void showData()
{
tv_data.setText("After Animation");
}
}
我可以看到动画发生但动画结束后tv_data textview不会改为“After Animation”;
请帮忙
答案 0 :(得分:1)
在Animation对象上设置AnimationListener:
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationEnd (Animation animation) {
showData();
}
@Override
public void onAnimationRepeat (Animation animation) {}
@Override
public void onAnimationStart (Animation animation) {}
});
另外,如果您希望立即开始,请startAnimation()
使用setAnimation()
代替ll_mainLayout
。