android Animation監(jiān)聽器AnimationListener的使用方法)
AnimationListener聽名字就知道是對Animation設(shè)置監(jiān)聽器,說簡單點就是在Animation動畫效果開始執(zhí)行前,執(zhí)行完畢和重復(fù)執(zhí)行時可以觸發(fā)監(jiān)聽器,從而執(zhí)行對應(yīng)的函數(shù)。
開發(fā)環(huán)境為android4.1.
AnimaitonListener的使用方法主要是在Animation上設(shè)置一個監(jiān)聽器,即采用Animation的方法成員setAnimationListener().其參數(shù)就是監(jiān)聽器的函數(shù)。
現(xiàn)在來說說本次實驗的功能,主要有2個按鈕,一個是增加圖片的按鈕,一個是刪除圖片的按鈕,還有一個ImageView的控件,用來顯示圖片的。當(dāng)增加圖片的按鈕按下時,圖片會以無到全尺寸的尺寸大小變化出現(xiàn),而刪除按鈕按下時,圖片會從全尺寸到0尺寸逐漸退出,最后刪除掉。
程序界面如下:
這里值得一提的是ViewGroup這個控件,感覺就是Layout控件一樣,本次實驗的圖片控件ImageView里面的圖片的增加和刪除就是采用的ViewGrop中的addView()和removeView()方法。這2種方法里面?zhèn)魅氲膮?shù)就是ImageView.
另外,Mars老師資料中在增加圖片監(jiān)聽器函數(shù)中,重新定義了一個ImageView,重新把這個ImageView加入到ViewGroup中,這樣會導(dǎo)致一個問題,那就是當(dāng)我們把圖片刪除后且又重新加載后就刪除不掉了,因為我們在刪除的時候刪的是布局文件中的ImageView,但是增加按鈕增加的是另外一個ImageView,所以我們雖然刪除掉了布局文件中的ImageView,但是屏幕上還是會顯示圖片的。因此解決的方法就是在增加按鈕函數(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,否則報錯
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));
//啟動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é):通過本次實驗,可以了解到AnimationListener的基本使用方法。
作者:tornadomeet
相關(guān)文章
Android編程實現(xiàn)通過反射獲取資源Id的方法
這篇文章主要介紹了Android編程實現(xiàn)通過反射獲取資源Id的方法,結(jié)合實例形式分析了Android反射機制操作資源的相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2018-01-01
Android APP開發(fā)KML軌跡導(dǎo)出教程示例
這篇文章主要為大家介紹了Android APP開發(fā)KML軌跡導(dǎo)出教程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
Android用戶輸入自動提示控件AutoCompleteTextView使用方法
這篇文章主要為大家詳細(xì)介紹了Android用戶輸入自動提示控件AutoCompleteTextView的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08
關(guān)于Android實現(xiàn)簡單的微信朋友圈分享功能
這篇文章主要介紹了關(guān)于Android實現(xiàn)簡單的微信朋友圈分享功能,非常不錯,具有參考借鑒價值,需要的的朋友參考下2017-02-02

