android Tween动画之透明的Dialog(数据加载)

来源:互联网 发布:mac移动硬盘装win10 编辑:程序博客网 时间:2024/05/16 16:49

转载前请注明链接:http://blog.csdn.net/daiwei714/article/details/44059833


相信大家无论是开发什么样的APP都会用到对话框(Dialog),尤其在发送网络请求的时候,为了让用户体验界面的友好性,都会提示一个对话框让客户去等待(其实客户最讨厌这个,转啊转)。


下面为大家提供一个基于Tween动画的透明的用于数据加载的Dialog,先上图:



好了,差不多加载中的时候就是这个样子,下面上代码。

1.CustomProgressBar.java

/** * 自定义进度条 */public class CustomProgressBar extends Dialog{private Context context ;private String progressText ;public CustomProgressBar(Context context) {super(context , R.style.dialog_theme) ;this.context = context ;}public CustomProgressBar(Context context, String progressText) {super(context,R.style.dialog_theme) ;this.context = context ;this.progressText = progressText ;}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.custom_progressbar) ;TextView title =  (TextView) findViewById(R.id.custom_imageview_progress_title);title.setText(progressText==null?"加载数据中,请稍后...":progressText) ;}/** * @see android.app.Dialog#show() */@Overridepublic void show() {try{if(!isShowing()){super.show();}}catch (Exception e){e.printStackTrace();}ImageView im =  (ImageView) findViewById(R.id.custom_imageview_progress_bar);im.startAnimation(AnimationUtils.loadAnimation(context, R.anim.round_loading));}} 

2.Theme的设置 :style.xml (dialog_theme)

    <style name="dialog_theme">        <item name="android:windowFrame">@null</item>        <item name="android:windowContentOverlay">@null</item>        <item name="android:windowIsFloating">true</item>        <item name="android:windowIsTranslucent">true</item>        <item name="android:windowNoTitle">true</item>        <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>        <item name="android:windowBackground">@color/transparent</item>        <!-- 背景透明 -->        <!-- 激活背景变暗遮罩 -->        <item name="android:backgroundDimEnabled">true</item>        <item name="android:backgroundDimAmount">0.6</item>    </style>

3.Anim的设置:round_loading.xml

<?xml version="1.0" encoding="utf-8"?><rotate xmlns:android="http://schemas.android.com/apk/res/android"    android:duration="1000"    android:fromDegrees="0"    android:interpolator="@android:anim/linear_interpolator"    android:pivotX="50%"    android:pivotY="50%"    android:repeatCount="infinite"    android:toDegrees="+360" />

4.布局文件:custom_progressbar.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:background="@drawable/loading_bg"    android:padding="15dip"    android:gravity="center"    android:orientation="vertical" >    <ImageView        android:layout_width="wrap_content"    android:layout_height="wrap_content"        android:id="@+id/custom_imageview_progress_bar"        android:src="@drawable/load" />    <TextView         android:layout_width="wrap_content"    android:layout_height="wrap_content"        android:layout_marginTop="5dip"        android:id="@+id/custom_imageview_progress_title"        android:textColor="@android:color/white"        android:textSize="12sp"        /></LinearLayout>

下面给大家贴上load的图片:


到这里就可以在activity中调用了:

/* * loading */public void createProgressBar() {this.createProgressBar(null);}public void createProgressBar(String text) {if (progressBar == null) {if (text == null) {progressBar = new CustomProgressBar(this);} else {progressBar = new CustomProgressBar(this, text);}progressBar.setCancelable(true);}if (!progressBar.isShowing()) {progressBar.show();}}public void disMissProgress() {if (progressBar != null && progressBar.isShowing()) {progressBar.dismiss();}}

这里扩展了可以自己设置提示文字,也可以使用默认文字。

谢谢大家的支持,不足之处请多多指出~


0 0