欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

android Animation監(jiān)聽器AnimationListener的使用方法)

 更新時(shí)間:2013年11月18日 15:43:12   作者:  
AnimaitonListener的使用方法主要是在Animation上設(shè)置一個(gè)監(jiān)聽器,下面通過一個(gè)實(shí)例說(shuō)明它的使用方法

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:

復(fù)制代碼 代碼如下:

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:
復(fù)制代碼 代碼如下:

<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

相關(guān)文章

最新評(píng)論