android Animation監(jiān)聽(tīng)器AnimationListener的使用方法)
AnimationListener聽(tīng)名字就知道是對(duì)Animation設(shè)置監(jiān)聽(tīng)器,說(shuō)簡(jiǎn)單點(diǎn)就是在Animation動(dòng)畫(huà)效果開(kāi)始執(zhí)行前,執(zhí)行完畢和重復(fù)執(zhí)行時(shí)可以觸發(fā)監(jiān)聽(tīng)器,從而執(zhí)行對(duì)應(yīng)的函數(shù)。
開(kāi)發(fā)環(huán)境為android4.1.
AnimaitonListener的使用方法主要是在Animation上設(shè)置一個(gè)監(jiān)聽(tīng)器,即采用Animation的方法成員setAnimationListener().其參數(shù)就是監(jiān)聽(tīng)器的函數(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è)控件,感覺(jué)就是Layout控件一樣,本次實(shí)驗(yàn)的圖片控件ImageView里面的圖片的增加和刪除就是采用的ViewGrop中的addView()和removeView()方法。這2種方法里面?zhèn)魅氲膮?shù)就是ImageView.
另外,Mars老師資料中在增加圖片監(jiān)聽(tīng)器函數(shù)中,重新定義了一個(gè)ImageView,重新把這個(gè)ImageView加入到ViewGroup中,這樣會(huì)導(dǎo)致一個(gè)問(wèn)題,那就是當(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比較好,否則加入的圖片用后面的方法視覺(jué)上是刪不掉的
//這里是采用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é):通過(guò)本次實(shí)驗(yàn),可以了解到AnimationListener的基本使用方法。
作者:tornadomeet
- Android編程自定義View時(shí)添加自己的監(jiān)聽(tīng)器示例
- Android實(shí)現(xiàn)檢測(cè)手機(jī)搖晃的監(jiān)聽(tīng)器
- Android中ScrollView實(shí)現(xiàn)滑動(dòng)距離監(jiān)聽(tīng)器的方法
- Android編程之監(jiān)聽(tīng)器用法實(shí)例分析
- Android編程之監(jiān)聽(tīng)器的實(shí)現(xiàn)方法
- Android實(shí)現(xiàn)靜態(tài)廣播監(jiān)聽(tīng)器的方法
- Android控件系列之Button以及Android監(jiān)聽(tīng)器使用介紹
- android監(jiān)聽(tīng)器實(shí)例代碼
相關(guān)文章
Android編程實(shí)現(xiàn)通過(guò)反射獲取資源Id的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)通過(guò)反射獲取資源Id的方法,結(jié)合實(shí)例形式分析了Android反射機(jī)制操作資源的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-01-01
Android APP開(kāi)發(fā)KML軌跡導(dǎo)出教程示例
這篇文章主要為大家介紹了Android APP開(kāi)發(fā)KML軌跡導(dǎo)出教程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
Android開(kāi)發(fā)人臉識(shí)別登錄功能
這篇文章主要介紹了Android開(kāi)發(fā)人臉識(shí)別登錄功能,這個(gè)很多公司都在使用,非常流行,今天小編給大家從頭到尾做一個(gè)案例分享到腳本之家平臺(tái),需要的朋友參考下吧2019-11-11
Android用戶輸入自動(dòng)提示控件AutoCompleteTextView使用方法
這篇文章主要為大家詳細(xì)介紹了Android用戶輸入自動(dòng)提示控件AutoCompleteTextView的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
關(guān)于Android實(shí)現(xiàn)簡(jiǎn)單的微信朋友圈分享功能
這篇文章主要介紹了關(guān)于Android實(shí)現(xiàn)簡(jiǎn)單的微信朋友圈分享功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下2017-02-02
Android簡(jiǎn)易電話撥號(hào)器實(shí)例詳解
這篇文章主要為大家詳細(xì)介紹了Android簡(jiǎn)易電話撥號(hào)器實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07

