作者:pfshi | 来源:互联网 | 2024-09-26 15:14
MainActivity如下:packagecc.testview1;importandroid.os.Bundle;importandroid.app.Activity
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如下:
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
> android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:layout_centerHorizOntal="true"
android:layout_marginTop="30dip"
/>
loading.xml如下:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" >
android:id="@+id/loadingTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="loading..." />
android:id="@+id/loadingImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/loadingTextView"
android:src="@drawable/ic_launcher"
/>
loadinganimfirst.xml如下:
android:fromDegrees="90"
android:toDegrees="-90"
android:pivotX="50%"
android:pivotY="50%"
android:duration="4000"
android:repeatCount="infinite"
android:repeatMode="reverse"
/>
loadinganimsecond.xml如下:
xmlns:android="http://schemas.android.com/apk/res/android"
android:Oneshot="false">