需要post
的Runnable
再次执行并再次在主线程中不睡觉。类似的东西:
GradientDrawable gd;
Handler uiHandler;
private View view;
private void start(){
// create the drawable
gd = new GradientDrawable(Orientation.TL_BR, green);
gd.setGradientType(GradientDrawable.RADIAL_GRADIENT);
gd.setGradientCenter(0.5f, 1.25f);
gd.setCornerRadius(0f);
// put it on the view
view = findViewById(R.id.ms_layout);
view.setBackgroundDrawable(gd);
// get a handler for the main thread looper and run
uiHandler = new Handler(Looper.getMainLooper());
uiHandler.post(myRun);
}
private Runnable myRun = new Runnable(){
int x = 0;
@SuppressWarnings("deprecation")
@Override
public void run(){
for(int x = 0; x < 400; x++){
// do the change
gd.setGradientRadius((float) 400 - x);
// ask system to re-draw it
gd.invalidateSelf();
// update n re-post loop
x++;
if(x < 400)
uiHandler.post(myRun);
}
}
};
ps.1:这一切都没有测试完成,你必须自己调整细节。
ps.2:您还可以使用postDelayed
这样uiHandler.postDelayed(myRun, 20);
编辑:
的情况下,你想要做的事更模块化,你可以创建自己的绘制对象,从GradientDrawable
和使用的延伸setLevel(int)
开始动画。我做这一切的时候,是这样的:
public class MyCustomDrawable extends GradientDrawable{
// put the same constructors GradientDrawable uses.
private int x;
protected boolean onLevelChange (int level){
x = 0;
invalidateSelf(); // this will make the thing call draw
}
public void draw (Canvas canvas){
// adjust what to draw
setGradientRadius((float) 400 - x);
// let the GradientDrawable do its normal drawing thing
super.draw(canvas);
// and then check if it needs another update
x++;
if(x < getLevel())
invalidateSelf();
}
}
把这个在您的视图的背景,并调用你想要做动画gd.setLevel(400);
每次。
这种方法的好处是,你可以随意调整它例如,基于时间,如果你愿意的话,4级为4秒钟,然后你在onLevelChange
让你做数学long animationEndTime = System.currentTimeMilis() + (level * 1000)
和draw
期间调用invalidateSelf ()if(System.currentTimeMilis() < animationEndTime)
“我猜它什么时候睡觉整个应用程序暂停,然后继续找出它需要再次停止。” - 不,这是因为您没有将主应用程序线程的控制权返回给框架,因此无法绘制您的更改。 – CommonsWare