复制代码 代码如下:
private class MyListenr implements AnimationListener{
@OverrIDe
public voID onAnimationEnd(Animation arg0) {
// Todo auto-generated method stub
}
@OverrIDe
public voID onAnimationRepeat(Animation arg0) {
// Todo auto-generated method stub
}
@OverrIDe
public voID onAnimationStart(Animation arg0) {
// Todo auto-generated method stub
}
}
其中第一个函数的意思是在动画执行完之后需要开发者做什么,第二个函数的意思是在动画重复执行的过程中应该做什么,第三个函数的意思是当动画开始执行时有什么动作发生。
下面我实现了一个例子,点击删除按钮,图片慢慢淡去,并最终删除,当点击添加按钮时向vIEwGroup中添加一个imagevIEw,实现的截图如下:
复制代码 代码如下:
public class MainActivity extends Activity {
private button button;
private button button2;
private ImageVIEw imageVIEw;
private VIEwGroup vIEwGroup;
@OverrIDe
protected voID onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentVIEw(R.layout.activity_main);
button=(button)findVIEwByID(R.ID.button_add);
button2=(button)findVIEwByID(R.ID.button_delete);
imageVIEw=(ImageVIEw)findVIEwByID(R.ID.imageVIEw1);
vIEwGroup=(VIEwGroup)findVIEwByID(R.ID.vIEwGroup);
button.setonClickListener(new Mybutton());
button2.setonClickListener(new Mybutton());
}
private class Mybutton implements OnClickListener{
@OverrIDe
public voID onClick(VIEw v) {
// Todo auto-generated method stub
switch (v.getID()) {
case R.ID.button_add:
Add();
break;
case R.ID.button_delete:
Delete();
break;
default:
break;
}
}
}
public voID Add() {
AlphaAnimation AlphaAnimation=new AlphaAnimation(0.0f,1.0f);
AlphaAnimation.setDuration(2000);
AlphaAnimation.setStartOffset(500);
ImageVIEw imageVIEwAdd=new ImageVIEw(MainActivity.this);
imageVIEwAdd.setimageResource(R.drawable.ic_launcher);
vIEwGroup.addVIEw(imageVIEwAdd);
// vIEwGroup.addVIEw(imageVIEwAdd,new LayoutParams(
// LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
imageVIEwAdd.startAnimation(AlphaAnimation);
}
public voID Delete() {
AlphaAnimation AlphaAnimation=new AlphaAnimation(1.0f,0.0f);
AlphaAnimation.setDuration(2000);
AlphaAnimation.setStartOffset(500);
imageVIEw.startAnimation(AlphaAnimation);
AlphaAnimation.setAnimationListener(new MyListenr());
}
private class MyListenr implements AnimationListener{
@OverrIDe
public voID onAnimationEnd(Animation arg0) {
// Todo auto-generated method stub
vIEwGroup.removeVIEw(imageVIEw);
Log.d("BruceZhang","Animation End!");
}
@OverrIDe
public voID onAnimationRepeat(Animation arg0) {
// Todo auto-generated method stub
Log.d("BruceZhang","Animation Repeat!");
}
@OverrIDe
public voID onAnimationStart(Animation arg0) {
// Todo auto-generated method stub
Log.d("BruceZhang","Animation Start!");
}
}
@OverrIDe
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main,menu);
return true;
}
}
此实例的布局文件如下,注意,需要在根标签下给出vIEwGroup的ID:
复制代码 代码如下:
<absoluteLayout 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"
tools:context=".MainActivity"
androID:ID="@+ID/vIEwGroup"
>
<button
androID:ID="@+ID/button_add"
androID:layout_wIDth="match_parent"
androID:layout_height="wrap_content"
androID:layout_x="0dp"
androID:layout_y="367dp"
androID:text="添加图片" />
<button
androID:ID="@+ID/button_delete"
androID:layout_wIDth="match_parent"
androID:layout_height="wrap_content"
androID:layout_x="0dp"
androID:layout_y="410dp"
androID:text="删除图片" />
<ImageVIEw
androID:ID="@+ID/imageVIEw1"
androID:layout_wIDth="wrap_content"
androID:layout_height="wrap_content"
androID:layout_x="153dp"
androID:layout_y="155dp"
androID:src="@drawable/ic_launcher" />
</absoluteLayout>
总结
以上是内存溢出为你收集整理的Android开发之图形图像与动画(四)AnimationListener简介全部内容,希望文章能够帮你解决Android开发之图形图像与动画(四)AnimationListener简介所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)