Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android自定義View—帶有動畫的Dialog

Android自定義View—帶有動畫的Dialog

編輯:關於Android編程

MainActivity如下:

package cc.testview1;

import android.os.Bundle;
import android.app.Activity;
/**
 * Demo描述:
 * 自定義Dialog,在Dialog中有動畫(旋轉動畫或者幀動畫)效果
 */
public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		//第一種-->rotate動畫
		LoadingDialogFirst loadingDialogFirst=new LoadingDialogFirst(this,R.style.dialog);
		loadingDialogFirst.show();
		
		//第二種-->frame動畫
		//LoadingDialogSecond loadingDialogSecond=new LoadingDialogSecond(this,R.style.dialog);
		//loadingDialogSecond.show();
		
	}
}


LoadingDialogFirst如下:

package cc.testview1;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

public class LoadingDialogFirst extends Dialog {
    private ImageView  mLoadingImageView;
    private Animation mLoadingAnimation;
	public LoadingDialogFirst(Context context, boolean cancelable,OnCancelListener cancelListener) {
		super(context, cancelable, cancelListener);
	}

	public LoadingDialogFirst(Context context, int theme) {
		super(context, theme);
	}

	public LoadingDialogFirst(Context context) {
		super(context);
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		View loadingView=LayoutInflater.from(getContext()).inflate(R.layout.loading, null);
		mLoadingImageView=(ImageView) loadingView.findViewById(R.id.loadingImageView);
		setContentView(loadingView);
	}
	
	@Override
	public void show() {
		super.show();
		mLoadingAnimation=AnimationUtils.loadAnimation(getContext(), R.anim.loadinganimfirst);
		mLoadingImageView.startAnimation(mLoadingAnimation);
	}
	@Override
	public void dismiss() {
		super.dismiss();
		mLoadingAnimation.cancel();
	}

}

LoadingDialogSecond如下:

package cc.testview1;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

public class LoadingDialogSecond extends Dialog {
    private ImageView  mLoadingImageView;
    private AnimationDrawable mLoadingAnimationDrawable;
	public LoadingDialogSecond(Context context, boolean cancelable,OnCancelListener cancelListener) {
		super(context, cancelable, cancelListener);
	}

	public LoadingDialogSecond(Context context, int theme) {
		super(context, theme);
	}

	public LoadingDialogSecond(Context context) {
		super(context);
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		View loadingView=LayoutInflater.from(getContext()).inflate(R.layout.loading, null);
		mLoadingImageView=(ImageView) loadingView.findViewById(R.id.loadingImageView);
		mLoadingImageView.setImageResource(R.anim.loadinganimsecond);
		setContentView(loadingView);
	}
	
	@Override
	public void show() {
		super.show();
		//注意將動畫的啟動放置在Handler中.否則只可看到第一張圖片
		new Handler(){}.postDelayed(new Runnable() {
			@Override
			public void run() {
				mLoadingAnimationDrawable =(AnimationDrawable) mLoadingImageView.getDrawable();
				mLoadingAnimationDrawable.start();
			}
		}, 10);
	}
	@Override
	public void dismiss() {
		super.dismiss();
		//結束幀動畫
		mLoadingAnimationDrawable =(AnimationDrawable) mLoadingImageView.getDrawable();
	    mLoadingAnimationDrawable.stop();
	}

}


main.xml如下:


    
    
     


loading.xml如下:




    

    


loadinganimfirst.xml如下:




    


loadinganimsecond.xml如下:



    
    
    



  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved