//总布局中:
<zz.mymobilesafe.view.CustomClipLoading
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
//Layout布局中:custom_loading
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/iv_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/loading_bg"
android:paddingLeft="3dp"
android:paddingTop="3dp"
android:scaleType="centerInside"
android:src="@drawable/clip_loading" />
</pre><pre name="code" class="java">//drawable文件夹下:clip_loading<pre name="code" class="java"><?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
android:clipOrientation="vertical"
android:drawable="@drawable/loading_progress"
android:gravity="bottom" >
</clip>
//定义了一个类,代码如下:
<pre name="code" class="java">public class CustomClipLoading extends FrameLayout {
private static final int MAX_PROGRESS = 10000;
private ClipDrawable mClipDrawable;
private int mProgress = 0;
private boolean running;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
// 如果消息是本程序发送的
if (msg.what == 0x123) {
mClipDrawable.setLevel(mProgress);
}
}
};
public CustomClipLoading(Context context) {
this(context, null, 0);
}
public CustomClipLoading(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomClipLoading(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
private void init(Context context) {
View view = LayoutInflater.from(context).inflate(R.layout.custom_loading, null);
addView(view);
ImageView imageView = (ImageView) findViewById(R.id.iv_progress);
mClipDrawable = (ClipDrawable) imageView.getDrawable();
Thread s = new Thread(r);
s.start();
}
public void stop() {
mProgress = 0;
running = false;
}
Runnable r = new Runnable() {
@Override
public void run() {
running = true;
while (running) {
handler.sendEmptyMessage(0x123);
if (mProgress > MAX_PROGRESS) {
mProgress = 0;
}
mProgress += 100;
try {
Thread.sleep(18);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
}