效果图,略骚,不要笑!!!
我们使用的是这么一张图片,图片是静态的,我们通过旋转动画去实现加载的效果.
首先我们看下,一个加载窗口需要些什么.一个ImageVIew和一个TextView,那么我们就有了下面的布局文件.load_layout_xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dialog_view"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minHeight="60dp"
android:minWidth="180dp"
android:gravity="center"
android:padding="10dp"
android:background="@drawable/loading_bg">
<ImageView
android:id="@+id/progressIV"
android:layout_width="64dp"
android:layout_height="64dp"
android:src="@drawable/progress"
/>
<TextView
android:id="@+id/tipTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textColor="#eeeeee"
android:text="正在加载中,请稍候..." />
</LinearLayout>
其中loading_bg是我们的背景图,这里我们就使用自定义的shape,在drawable目录下添加loading_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="10dp" />
<gradient
android:centerColor="#FF4081"
android:endColor="#FF4081"
android:startColor="#FF4081" />
</shape>
下面开始我们的主代码编写.
package com.example.august.customtheme;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomProgressDialog extends Dialog {
/**
* 从我们的布局文件loading_layout加载的View
*/
private View mContentView;
/**
* 我们的两个小空控件,显示图片和显示文字
*/
private ImageView mImageView;
private TextView mTextView;
/**
* 我们只去覆写指定主题的方法,并且我们就只有这么一个构造器
* @param context
* @param themeResId
* @param LayoutID
* @param imageViewID
* @param textViewID
*/
public CustomProgressDialog(Context context, int themeResId, int LayoutID, int imageViewID, int textViewID) {
super(context, themeResId);
mContentView = LayoutInflater.from(context).inflate(LayoutID, null);
mImageView = (ImageView) mContentView.findViewById(imageViewID);
/**
* 为图片设置一个旋转动画
*/
RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setInterpolator(new AccelerateInterpolator());
rotateAnimation.setDuration(1500);
rotateAnimation.setRepeatCount(-1);
mImageView.startAnimation(rotateAnimation);
mTextView = (TextView) mContentView.findViewById(textViewID);
setContentView(mContentView);
}
}
这里我们构造器写那么多参数,只是为了耦性降低一点.可以自己指定其他布局和在后续增加更多方法去实例化这个Dialog.
下面我们就可以去使用了,调用代码如下
CustomProgressDialog mProgressDialog = new CustomProgressDialog(MainActivity.this
, R.style.loading_dialog, R.layout.load_layout,
R.id.progressIV, R.id.tipTV);
mProgressDialog.show();
我们再去给他公布一下方法,去设置那个文本信息和图片,于是乎我们给CustomProgressDialog添加以下方法
public void setMessage(String text) {
if (mTextView != null) {
mTextView.setText(text);
}
}
public void setProgressBitmap(Bitmap bitmap) {
if (mImageView != null) {
mImageView.setImageBitmap(bitmap);
}
}
最后,如果你需要大量使用特定布局对应的ProgressDialog,那么我们还可以去把layout写死,于是又诞生了MyProgressDialog
public class MyProgressDialog {
public static CustomProgressDialog get(Context context) {
return new CustomProgressDialog(context, R.style.CustomDialog, R.layout.loading_layout, R.id.progressIV,
R.id.tipTV);
}
}
下面我们调用的代码就变得简单了
CustomProgressDialog mProgressDialog = MyProgressDialog.get(MainActivity.this);
mProgressDialog.show();
CustomProgressDialog整体代码:
package com.example.august.customtheme;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomProgressDialog extends Dialog {
/**
* 从我们的布局文件loading_layout加载的View
*/
private View mContentView;
/**
* 我们的两个小空控件,显示图片和显示文字
*/
private ImageView mImageView;
private TextView mTextView;
/**
* 我们只去覆写指定主题的方法,并且我们就只有这么一个构造器
* @param context
* @param themeResId
* @param LayoutID
* @param imageViewID
* @param textViewID
*/
public CustomProgressDialog(Context context, int themeResId, int LayoutID, int imageViewID, int textViewID) {
super(context, themeResId);
mContentView = LayoutInflater.from(context).inflate(LayoutID, null);
mImageView = (ImageView) mContentView.findViewById(imageViewID);
/**
* 为图片设置一个旋转动画
*/
RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setInterpolator(new AccelerateInterpolator());
rotateAnimation.setDuration(1500);
rotateAnimation.setRepeatCount(-1);
mImageView.startAnimation(rotateAnimation);
mTextView = (TextView) mContentView.findViewById(textViewID);
setContentView(mContentView);
}
public void setMessage(String text) {
if (mTextView != null) {
mTextView.setText(text);
}
}
public void setProgressBitmap(Bitmap bitmap) {
if (mImageView != null) {
mImageView.setImageBitmap(bitmap);
}
}
}