您的位置:首页 > 移动开发 > Android开发

Android自定义View示例(四)—带有动画的Dialog

2014-02-04 12:38 639 查看
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如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dip"
/>

</RelativeLayout>


loading.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" >

<TextView
android:id="@+id/loadingTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="loading..." />

<ImageView
android:id="@+id/loadingImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/loadingTextView"
android:src="@drawable/ic_launcher"
/>

</RelativeLayout>


loadinganimfirst.xml如下:

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android" >
<rotate
android:fromDegrees="90"
android:toDegrees="-90"
android:pivotX="50%"
android:pivotY="50%"
android:duration="4000"
android:repeatCount="infinite"
android:repeatMode="reverse"
/>
</set>


loadinganimsecond.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<animation-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/photo1" android:duration="500" />
<item android:drawable="@drawable/photo2" android:duration="500" />
<item android:drawable="@drawable/photo3" android:duration="500" />
</animation-list>



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: