android Animation監(jiān)聽器AnimationListener的使用方法)
AnimationListener聽名字就知道是對(duì)Animation設(shè)置監(jiān)聽器,說(shuō)簡(jiǎn)單點(diǎn)就是在Animation動(dòng)畫效果開始執(zhí)行前,執(zhí)行完畢和重復(fù)執(zhí)行時(shí)可以觸發(fā)監(jiān)聽器,從而執(zhí)行對(duì)應(yīng)的函數(shù)。
開發(fā)環(huán)境為android4.1.
AnimaitonListener的使用方法主要是在Animation上設(shè)置一個(gè)監(jiān)聽器,即采用Animation的方法成員setAnimationListener().其參數(shù)就是監(jiān)聽器的函數(shù)。
現(xiàn)在來(lái)說(shuō)說(shuō)本次實(shí)驗(yàn)的功能,主要有2個(gè)按鈕,一個(gè)是增加圖片的按鈕,一個(gè)是刪除圖片的按鈕,還有一個(gè)ImageView的控件,用來(lái)顯示圖片的。當(dāng)增加圖片的按鈕按下時(shí),圖片會(huì)以無(wú)到全尺寸的尺寸大小變化出現(xiàn),而刪除按鈕按下時(shí),圖片會(huì)從全尺寸到0尺寸逐漸退出,最后刪除掉。
程序界面如下:
這里值得一提的是ViewGroup這個(gè)控件,感覺就是Layout控件一樣,本次實(shí)驗(yàn)的圖片控件ImageView里面的圖片的增加和刪除就是采用的ViewGrop中的addView()和removeView()方法。這2種方法里面?zhèn)魅氲膮?shù)就是ImageView.
另外,Mars老師資料中在增加圖片監(jiān)聽器函數(shù)中,重新定義了一個(gè)ImageView,重新把這個(gè)ImageView加入到ViewGroup中,這樣會(huì)導(dǎo)致一個(gè)問題,那就是當(dāng)我們把圖片刪除后且又重新加載后就刪除不掉了,因?yàn)槲覀冊(cè)趧h除的時(shí)候刪的是布局文件中的ImageView,但是增加按鈕增加的是另外一個(gè)ImageView,所以我們雖然刪除掉了布局文件中的ImageView,但是屏幕上還是會(huì)顯示圖片的。因此解決的方法就是在增加按鈕函數(shù)中直接使用布局文件中的ImageView,這樣程序中可以一直增加圖片和刪除圖片,且在屏幕中還能看到效果。
程序主要代碼如下:
MainActivity.java:
package com.example.anim_5;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.ScaleAnimation;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
private Button add = null;
private Button delete = null;
private ViewGroup viewgroup = null;
private ImageView imageview = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
add = (Button)findViewById(R.id.add);
delete = (Button)findViewById(R.id.delete);
imageview = (ImageView)findViewById(R.id.image);
viewgroup = (ViewGroup)findViewById(R.id.main_layout);
add.setOnClickListener(new AddOnClickListener());
delete.setOnClickListener(new DeleteOnClickListener());
}
private class AddOnClickListener implements OnClickListener{
public void onClick(View v) {
// TODO Auto-generated method stub
//數(shù)字后面必須全部加f,否則報(bào)錯(cuò)
ScaleAnimation animation = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
animation.setStartOffset(500);
animation.setDuration(1000);
// ImageView image_add = new ImageView(MainActivity.this);
// image_add.setImageResource(R.drawable.london_olympic);
// viewgroup.addView(image_add, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
// image_add.setAnimation(animation);
//還是直接用布局文件中的ImageView比較好,否則加入的圖片用后面的方法視覺上是刪不掉的
//這里是采用setImageResource方法加載圖片到ImageView控件上的。
imageview.setImageResource(R.drawable.london_olympic);
//用ViewGroup將ImageView加載到activity中
viewgroup.addView(imageview, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
//啟動(dòng)ImageView的Animation
imageview.startAnimation(animation);
}
}
private class DeleteOnClickListener implements OnClickListener{
public void onClick(View v) {
// TODO Auto-generated method stub
ScaleAnimation animation = new ScaleAnimation(1.0f, 0.0f, 1.0f, 0.0f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
animation.setStartOffset(500);
animation.setDuration(1000);
//設(shè)置AnimationListener
animation.setAnimationListener(new DeleteAnimationListener());
imageview.startAnimation(animation);
}
}
private class DeleteAnimationListener implements AnimationListener{
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
viewgroup.removeView(imageview);
}
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/delete"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Delete Image"
/>
<Button
android:id="@+id/add"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/delete"
android:text="Add Image" />
<ImageView
android:id= "@+id/image"
android:layout_width="wrap_content"
android:layout_height="310dip"
android:src="@drawable/london_olympic"
/>
</RelativeLayout>
總結(jié):通過本次實(shí)驗(yàn),可以了解到AnimationListener的基本使用方法。
作者:tornadomeet
- Android編程自定義View時(shí)添加自己的監(jiān)聽器示例
- Android實(shí)現(xiàn)檢測(cè)手機(jī)搖晃的監(jiān)聽器
- Android中ScrollView實(shí)現(xiàn)滑動(dòng)距離監(jiān)聽器的方法
- Android編程之監(jiān)聽器用法實(shí)例分析
- Android編程之監(jiān)聽器的實(shí)現(xiàn)方法
- Android實(shí)現(xiàn)靜態(tài)廣播監(jiān)聽器的方法
- Android控件系列之Button以及Android監(jiān)聽器使用介紹
- android監(jiān)聽器實(shí)例代碼
相關(guān)文章
Android 自定義View結(jié)合自定義TabLayout實(shí)現(xiàn)頂部標(biāo)簽滑動(dòng)效果
小編最近在做app的項(xiàng)目,需要用到tablayout實(shí)現(xiàn)頂部的滑動(dòng)效果,文中代碼用到了自定義item,代碼也很簡(jiǎn)單,感興趣的朋友跟隨腳本之家小編一起看看吧2018-07-07Android實(shí)現(xiàn)光點(diǎn)模糊漸變的自旋轉(zhuǎn)圓環(huán)特效
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)光點(diǎn)模糊漸變的自旋轉(zhuǎn)圓環(huán)特效,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06Android App中ListView仿QQ實(shí)現(xiàn)滑動(dòng)刪除效果的要點(diǎn)解析
這篇文章主要介紹了Android App中ListView仿QQ實(shí)現(xiàn)滑動(dòng)刪除效果的要點(diǎn)解析,重點(diǎn)是要判斷手勢(shì)按下的位置坐標(biāo),需要的朋友可以參考下2016-04-04Android中Serializable和Parcelable序列化對(duì)象詳解
這篇文章主要介紹了Android中Serializable和Parcelable序列化對(duì)象的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-02-02android 中ProgressDialog實(shí)現(xiàn)全屏效果的示例
本篇文章主要介紹了android 中ProgressDialog實(shí)現(xiàn)全屏效果的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2017-11-11安裝時(shí)加入外部數(shù)據(jù)庫(kù)示例(android外部數(shù)據(jù)庫(kù))
這篇文章主要介紹了android打包安裝時(shí)加入外部數(shù)據(jù)庫(kù)的示例,需要的朋友可以參考下2014-03-03Android Studio自動(dòng)排版的兩種實(shí)現(xiàn)方式
這篇文章主要介紹了Android Studio自動(dòng)排版的兩種實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-03-03Kotlin文件讀寫與SharedPreferences存儲(chǔ)功能實(shí)現(xiàn)方法
SharedPreferences是安卓平臺(tái)上一個(gè)輕量級(jí)的存儲(chǔ)類,用來(lái)保存應(yīng)用的一些常用配置,比如Activity狀態(tài),Activity暫停時(shí),將此activity的狀態(tài)保存到SharedPereferences中;當(dāng)Activity重載,系統(tǒng)回調(diào)方法onSaveInstanceState時(shí),再?gòu)腟haredPreferences中將值取出2022-12-12Android中選項(xiàng)菜單(OptionMenu)的創(chuàng)建方法
這篇文章主要介紹了Android中選項(xiàng)菜單(OptionMenu)的創(chuàng)建方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-01-01Android自定義View實(shí)現(xiàn)多圖片選擇控件
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)多圖片選擇控件,具有一定的實(shí)用性,感興趣的小伙伴們可以參考一下2016-08-08