应用加载过程中的loading动画:
LoadingView .java
/***
* loading 动画的实现
* @author
*
*/
public class LoadingView extends RelativeLayout {
private LayoutInflater mLayoutInflater;
private RelativeLayout mRLayout;
private ImageView mDot01Iv, mDot02Iv, mDot03Iv, mDot04Iv, mDot05Iv;
private Handler mHandler = null;
Context mContext;
public LoadingView(Context context) {
super(context);
mContext = context;
}
public LoadingView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
mLayoutInflater = LayoutInflater.from(context);
mRLayout = (RelativeLayout) mLayoutInflater.inflate(
R.layout.view_loading,
LoadingView.this, true);
mDot01Iv = (ImageView) mRLayout
.findViewById(R.id.view_loading_first_iv);
mDot02Iv = (ImageView) mRLayout.findViewById(R.id.view_loading_sec_iv);
mDot03Iv = (ImageView) mRLayout
.findViewById(R.id.view_loading_third_iv);
mDot04Iv = (ImageView) mRLayout.findViewById(R.id.view_loading_four_iv);
mDot05Iv = (ImageView) mRLayout.findViewById(R.id.view_loading_five_iv);
}
/***
* 显示loading动画
*/
public void show() {
playAnimation();
}
/***
* 取消loading动画
*/
public void dismiss() {
Thread.interrupted();
}
// 不断发送消息,切换图片
private void playAnimation() {
new Thread() {
@Override
public void run() {
while (true) {
for (int i = 5; i > 0; i--) {
mHandler.sendEmptyMessage(i);
try {
sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}.start();
// handler接受消息进行变化
mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what)
{
case 1:
mDot01Iv.setImageDrawable(mContext.getResources()
.getDrawable(R.drawable.alpha_bg));
mDot02Iv.setImageDrawable(mContext.getResources()
.getDrawable(R.drawable.icon));
mDot03Iv.setImageDrawable(mContext.getResources()
.getDrawable(R.drawable.icon));
mDot04Iv.setImageDrawable(mContext.getResources()
.getDrawable(R.drawable.icon));
mDot05Iv.setImageDrawable(mContext.getResources()
.getDrawable(R.drawable.icon));
break;
case 2:
mDot01Iv.setImageDrawable(mContext.getResources()
.getDrawable(R.drawable.icon));
mDot02Iv.setImageDrawable(mContext.getResources()
.getDrawable(R.drawable.alpha_bg));
mDot03Iv.setImageDrawable(mContext.getResources()
.getDrawable(R.drawable.icon));
mDot04Iv.setImageDrawable(mContext.getResources()
.getDrawable(R.drawable.icon));
mDot05Iv.setImageDrawable(mContext.getResources()
.getDrawable(R.drawable.icon));
break;
case 3:
mDot01Iv.setImageDrawable(mContext.getResources()
.getDrawable(R.drawable.icon));
mDot02Iv.setImageDrawable(mContext.getResources()
.getDrawable(R.drawable.icon));
mDot03Iv.setImageDrawable(mContext.getResources()
.getDrawable(R.drawable.alpha_bg));
mDot04Iv.setImageDrawable(mContext.getResources()
.getDrawable(R.drawable.icon));
mDot05Iv.setImageDrawable(mContext.getResources()
.getDrawable(R.drawable.icon));
break;
case 4:
mDot01Iv.setImageDrawable(mContext.getResources()
.getDrawable(R.drawable.icon));
mDot02Iv.setImageDrawable(mContext.getResources()
.getDrawable(R.drawable.icon));
mDot03Iv.setImageDrawable(mContext.getResources()
.getDrawable(R.drawable.icon));
mDot04Iv.setImageDrawable(mContext.getResources()
.getDrawable(R.drawable.alpha_bg));
mDot05Iv.setImageDrawable(mContext.getResources()
.getDrawable(R.drawable.icon));
break;
case 5:
mDot01Iv.setImageDrawable(mContext.getResources()
.getDrawable(R.drawable.icon));
mDot02Iv.setImageDrawable(mContext.getResources()
.getDrawable(R.drawable.icon));
mDot03Iv.setImageDrawable(mContext.getResources()
.getDrawable(R.drawable.icon));
mDot04Iv.setImageDrawable(mContext.getResources()
.getDrawable(R.drawable.icon));
mDot05Iv.setImageDrawable(mContext.getResources()
.getDrawable(R.drawable.alpha_bg));
break;
}
}
};
}
}
view_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:layout_gravity="center"
android:background="#000000"
>
<ImageView
android:id="@+id/view_loading_first_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon"
/>
<ImageView
android:id="@+id/view_loading_sec_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon"
android:layout_toRightOf="@+id/view_loading_first_iv"
/>
<ImageView
android:id="@+id/view_loading_third_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon"
android:layout_toRightOf="@+id/view_loading_sec_iv"
/>
<ImageView
android:id="@+id/view_loading_four_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon"
android:layout_toRightOf="@+id/view_loading_third_iv"
/>
<ImageView
android:id="@+id/view_loading_five_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon"
android:layout_toRightOf="@+id/view_loading_four_iv"
/>
</RelativeLayout>