ANDROID_MARS学习笔记_S02_012_ANIMATION_利用AnimationListener在动画结束时删除或添加组件

一、代码

1.xml
(1)activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layoutId"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:id="@+id/addButtonId" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_alignParentBottom="true"
android:text="添加图片" /> <Button android:id="@+id/removeButtonId" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_above="@id/addButtonId"
android:text="删除图片" /> <ImageView android:id="@+id/imageViewId"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_centerInParent="true" android:layout_marginTop="100dip"
android:src="@drawable/a1" />
</RelativeLayout>

2.java
(1)MainActivity.java

 package com.animationlistener;

 import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.Button;
import android.widget.ImageView; public class MainActivity extends Activity { private Button removeButton = null;
private Button addButton = null;
private ImageView imageView = null;
private ViewGroup viewGroup = null; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); removeButton = (Button)findViewById(R.id.removeButtonId);
removeButton.setOnClickListener(new RemoveButtonListener());
addButton = (Button)findViewById(R.id.addButtonId);
addButton.setOnClickListener(new AddButtonListener()); imageView = (ImageView)findViewById(R.id.imageViewId);
viewGroup = (ViewGroup) findViewById(R.id.layoutId);
} class AddButtonListener implements OnClickListener {
@Override
public void onClick(View v) {
Animation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(1000);
animation.setStartOffset(500);
ImageView imageViewAdd = new ImageView(MainActivity.this);
imageViewAdd.setImageResource(R.drawable.a1);
viewGroup.addView(imageViewAdd, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
imageViewAdd.startAnimation(animation);
}
} class RemoveButtonListener implements OnClickListener {
@Override
public void onClick(View v) {
Animation animation = new AlphaAnimation(1.0f, 0.0f);
animation.setDuration(1000);
animation.setStartOffset(500);
animation.setAnimationListener(new RemoveAnimationListener());
imageView.startAnimation(animation);
}
} class RemoveAnimationListener implements AnimationListener { @Override
public void onAnimationStart(Animation animation) {
System.out.println("Start");
} @Override
public void onAnimationEnd(Animation animation) {
System.out.println("End");
viewGroup.removeView(imageView);
} @Override
public void onAnimationRepeat(Animation animation) {
System.out.println("Repeat");
}
}
}
上一篇:TFS二次开发07——锁定(Lock)和解锁(UnLock)


下一篇:[TFS]TFS强制删除离职人员签出锁定项的方法