Android图形图像与动画之AnimationListener简介的代码

如下资料是关于Android图形图像与动画之AnimationListener简介的内容。

private class MyListenr implements AnimationListener{

@Override
public void onAnimationEnd(Animation arg0) {

}

@Override
public void onAnimationRepeat(Animation arg0) {

}

@Override
public void onAnimationStart(Animation arg0) {

}

}





其中第一个函数的意思是在动画执行完之后需要开发者做什么,第二个函数的意思是在动画重复执行的过程中应该做什么,第三个函数的意思是当动画开始执行时有什么动作发生。具体的实现代码如下:



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) {
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);
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) {
viewGroup.removeView(imageView);
Log.d("BruceZhang", "Animation End!");
}

@Override
public void onAnimationRepeat(Animation arg0) {
Log.d("BruceZhang", "Animation Repeat!");
}

@Override
public void onAnimationStart(Animation arg0) {
Log.d("BruceZhang", "Animation Start!");
}

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

}





此实例的布局文件如下,注意,需要在根标签下给出viewGroup的id:


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>





猜你喜欢

转载自www.cnblogs.com/uigrls/p/10297957.html