Android開發(fā)之圖形圖像與動畫(四)AnimationListener簡介
更新時間:2013年01月28日 11:54:33 作者:
就像Button控件有監(jiān)聽器一樣,動畫效果也有監(jiān)聽器,只需要實現(xiàn)AnimationListener就可以實現(xiàn)對動畫效果的監(jiān)聽,感興趣的朋友可以了解下啊,希望本文對你有所幫助
就像Button控件有監(jiān)聽器一樣,動畫效果也有監(jiān)聽器,只需要實現(xiàn)AnimationListener就可以實現(xiàn)對動畫效果的監(jiān)聽,其中需要重載三個函數(shù),就是下面的這幾個函數(shù):
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
}
}
其中第一個函數(shù)的意思是在動畫執(zhí)行完之后需要開發(fā)者做什么,第二個函數(shù)的意思是在動畫重復執(zhí)行的過程中應該做什么,第三個函數(shù)的意思是當動畫開始執(zhí)行時有什么動作發(fā)生。
下面我實現(xiàn)了一個例子,點擊刪除按鈕,圖片慢慢淡去,并最終刪除,當點擊添加按鈕時向viewGroup中添加一個imageview,實現(xiàn)的截圖如下:
具體的實現(xiàn)代碼如下:
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>
復制代碼 代碼如下:
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
}
}
其中第一個函數(shù)的意思是在動畫執(zhí)行完之后需要開發(fā)者做什么,第二個函數(shù)的意思是在動畫重復執(zhí)行的過程中應該做什么,第三個函數(shù)的意思是當動畫開始執(zhí)行時有什么動作發(fā)生。
下面我實現(xiàn)了一個例子,點擊刪除按鈕,圖片慢慢淡去,并最終刪除,當點擊添加按鈕時向viewGroup中添加一個imageview,實現(xiàn)的截圖如下:
具體的實現(xiàn)代碼如下:
復制代碼 代碼如下:
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開發(fā)之圖形圖像與動畫(一)Paint和Canvas類學習
- Android開發(fā)之圖形圖像與動畫(二)Animation實現(xiàn)圖像的漸變/縮放/位移/旋轉(zhuǎn)
- Android開發(fā)之圖形圖像與動畫(三)Animation效果的XML實現(xiàn)
- android中圖形圖像處理之drawable用法分析
- Android編程開發(fā)之在Canvas中利用Path繪制基本圖形(圓形,矩形,橢圓,三角形等)
- Android開發(fā) OpenGL ES繪制3D 圖形實例詳解
- Android開發(fā)之圖形圖像與動畫(五)LayoutAnimationController詳解
- Android編程之canvas繪制各種圖形(點,直線,弧,圓,橢圓,文字,矩形,多邊形,曲線,圓角矩形)
- Android自定義View實現(xiàn)shape圖形繪制
- android繪制幾何圖形的實例代碼
相關文章
Android之IphoneTreeView帶組指示器的ExpandableListView效果
在正在顯示的最上面的組的標簽位置添加一個和組視圖完全一樣的視圖,作為組標簽。這個標簽的位置要隨著列表的滑動不斷變化,以保持總是顯示在最上方,并且該消失的時候就消失2013-06-06
Android編程使WebView支持HTML5 Video全屏播放的解決方法
這篇文章主要介紹了Android編程使WebView支持HTML5 Video全屏播放的解決方法,較為詳細的分析了全屏播放所涉及的相關技巧,并給出了完整代碼下載地址供讀者參考,需要的朋友可以參考下2015-10-10
Android實現(xiàn)基于ZXing快速集成二維碼掃描功能
這篇文章主要為大家詳細介紹了Android二維碼掃描ZXing快速項目集成的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07
Android Path繪制貝塞爾曲線實現(xiàn)QQ拖拽泡泡
本文主要介紹Android Path繪制貝塞爾曲線,這里整理相關資料并運用貝塞爾曲線實現(xiàn)QQ拖拽泡泡的示例,有興趣的小伙伴可以參考下2016-09-09

