Android開(kāi)發(fā)之圖形圖像與動(dòng)畫(huà)(四)AnimationListener簡(jiǎn)介
更新時(shí)間:2013年01月28日 11:54:33 作者:
就像Button控件有監(jiān)聽(tīng)器一樣,動(dòng)畫(huà)效果也有監(jiān)聽(tīng)器,只需要實(shí)現(xiàn)AnimationListener就可以實(shí)現(xiàn)對(duì)動(dòng)畫(huà)效果的監(jiān)聽(tīng),感興趣的朋友可以了解下啊,希望本文對(duì)你有所幫助
就像Button控件有監(jiān)聽(tīng)器一樣,動(dòng)畫(huà)效果也有監(jiān)聽(tīng)器,只需要實(shí)現(xiàn)AnimationListener就可以實(shí)現(xiàn)對(duì)動(dòng)畫(huà)效果的監(jiān)聽(tīng),其中需要重載三個(gè)函數(shù),就是下面的這幾個(gè)函數(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
}
}
其中第一個(gè)函數(shù)的意思是在動(dòng)畫(huà)執(zhí)行完之后需要開(kāi)發(fā)者做什么,第二個(gè)函數(shù)的意思是在動(dòng)畫(huà)重復(fù)執(zhí)行的過(guò)程中應(yīng)該做什么,第三個(gè)函數(shù)的意思是當(dāng)動(dòng)畫(huà)開(kāi)始執(zhí)行時(shí)有什么動(dòng)作發(fā)生。
下面我實(shí)現(xiàn)了一個(gè)例子,點(diǎn)擊刪除按鈕,圖片慢慢淡去,并最終刪除,當(dāng)點(diǎn)擊添加按鈕時(shí)向viewGroup中添加一個(gè)imageview,實(shí)現(xiàn)的截圖如下:
具體的實(shí)現(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;
}
}
此實(shí)例的布局文件如下,注意,需要在根標(biāo)簽下給出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>
復(fù)制代碼 代碼如下:
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
}
}
其中第一個(gè)函數(shù)的意思是在動(dòng)畫(huà)執(zhí)行完之后需要開(kāi)發(fā)者做什么,第二個(gè)函數(shù)的意思是在動(dòng)畫(huà)重復(fù)執(zhí)行的過(guò)程中應(yīng)該做什么,第三個(gè)函數(shù)的意思是當(dāng)動(dòng)畫(huà)開(kāi)始執(zhí)行時(shí)有什么動(dòng)作發(fā)生。
下面我實(shí)現(xiàn)了一個(gè)例子,點(diǎn)擊刪除按鈕,圖片慢慢淡去,并最終刪除,當(dāng)點(diǎn)擊添加按鈕時(shí)向viewGroup中添加一個(gè)imageview,實(shí)現(xiàn)的截圖如下:

具體的實(shí)現(xiàn)代碼如下:
復(fù)制代碼 代碼如下:
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;
}
}
此實(shí)例的布局文件如下,注意,需要在根標(biāo)簽下給出viewGroup的id:
復(fù)制代碼 代碼如下:
<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開(kāi)發(fā)之圖形圖像與動(dòng)畫(huà)(一)Paint和Canvas類(lèi)學(xué)習(xí)
- Android開(kāi)發(fā)之圖形圖像與動(dòng)畫(huà)(二)Animation實(shí)現(xiàn)圖像的漸變/縮放/位移/旋轉(zhuǎn)
- Android開(kāi)發(fā)之圖形圖像與動(dòng)畫(huà)(三)Animation效果的XML實(shí)現(xiàn)
- android中圖形圖像處理之drawable用法分析
- Android編程開(kāi)發(fā)之在Canvas中利用Path繪制基本圖形(圓形,矩形,橢圓,三角形等)
- Android開(kāi)發(fā) OpenGL ES繪制3D 圖形實(shí)例詳解
- Android開(kāi)發(fā)之圖形圖像與動(dòng)畫(huà)(五)LayoutAnimationController詳解
- Android編程之canvas繪制各種圖形(點(diǎn),直線,弧,圓,橢圓,文字,矩形,多邊形,曲線,圓角矩形)
- Android自定義View實(shí)現(xiàn)shape圖形繪制
- android繪制幾何圖形的實(shí)例代碼
相關(guān)文章
Android實(shí)現(xiàn)顏色漸變動(dòng)畫(huà)效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)顏色漸變動(dòng)畫(huà)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05Android自定義View實(shí)現(xiàn)多邊形統(tǒng)計(jì)圖示例代碼
這篇文章主要給大家介紹了關(guān)于Android自定義View如何實(shí)現(xiàn)多邊形統(tǒng)計(jì)圖的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-01-01android?studio后臺(tái)服務(wù)使用詳解
這篇文章主要為大家詳細(xì)介紹了android?studio后臺(tái)服務(wù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08Android實(shí)現(xiàn)打開(kāi)手機(jī)淘寶并自動(dòng)識(shí)別淘寶口令彈出商品信息功能
最近項(xiàng)目經(jīng)理給我們安排一個(gè)活兒,基于Android開(kāi)發(fā)實(shí)現(xiàn)打開(kāi)手機(jī)淘寶,并自動(dòng)識(shí)別淘口令,彈出商品信息,今天小編就抽空給大家分享下這個(gè)需求是怎么實(shí)現(xiàn)的,需要的朋友參考下吧2017-11-11Android中多行文本末尾添加圖片排版問(wèn)題的解決方法
這篇文章主要給大家介紹了關(guān)于Android中多行文本末尾添加圖片排版問(wèn)題的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07Android用RecyclerView實(shí)現(xiàn)圖標(biāo)拖拽排序以及增刪管理
這篇文章主要介紹了Android用RecyclerView實(shí)現(xiàn)圖標(biāo)拖拽排序以及增刪管理的方法,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下2021-03-03