这个动画效果的实现主要依靠的是ClipDrawable类.下面是ClipDrawable类的简介:
A Drawable that clips another Drawable based on this Drawable's current level value. You can control how much the child Drawable gets clipped in width and height based on the level, as well as a gravity to control where it is placed in its overall container. Most often used to implement things like progress bars, by increasing the drawable's level with setLevel()
.
Note: The drawable is clipped completely and not visible when the level is 0 and fully revealed when the level is 10,000.
It can be defined in an XML file with the <clip>
element. For more information, see the guide to Drawable Resources.
下面是clip的xml文件
<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
android:clipOrientation="vertical"
android:drawable="@drawable/taskmanager_circle_full"
android:gravity="bottom" >
</clip>
<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"
tools:context=".MainActivity" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/img_background"
android:src="@drawable/taskmanager_background"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/img_vary"
android:src="@drawable/vary"
/>
</RelativeLayout>
public class MainActivity extends Activity {
private final static int MAX = 10000;
private int current = 0;
private final static int INCREMENT = 100;
protected static final int START = 1;
boolean isFinished = true;;
private ImageView img;
private Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case START:
img.setImageResource(R.drawable.vary);
ClipDrawable clip = (ClipDrawable) img.getDrawable();
clip.setLevel(current);
if(current == MAX){
if(timer != null){
timer.cancel();
}
if(task != null){
task.cancel();
}
}
isFinished = true;
break;
default:
break;
}
};
};
private Timer timer;
private TimerTask task;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = (ImageView) findViewById(R.id.img_vary);
getAnima();
}
private void getAnima(){
timer = new Timer();
task = new TimerTask() {
@Override
public void run() {
while(isFinished){
isFinished = false;
Message msg = new Message();
msg.what = START;
msg.obj = current;
System.out.println("current = "+current);
current += 100;
handler.sendMessage(msg);
}
}
};
timer.schedule(task, 0, 1);
}
}