Android实现创意LoadingView动画效果
程序员文章站
2024-02-22 22:48:36
android上的热火锅煮萝卜蔬菜的loading动画效果。 这是一个锅煮萝卜的loading动画,效果仿照自之前ios上看到的一个效果,觉得挺有意思,就移植过来了,在此完...
android上的热火锅煮萝卜蔬菜的loading动画效果。 这是一个锅煮萝卜的loading动画,效果仿照自之前ios上看到的一个效果,觉得挺有意思,就移植过来了,在此完成了dialog的样式,方便使用者作为loadingview去使用。
关键性代码:
package yellow5a5.demo.boilingloadingview.view; import android.animation.animator; import android.animation.animatorlisteneradapter; import android.animation.valueanimator; import android.content.context; import android.graphics.drawable.clipdrawable; import android.os.handler; import android.os.message; import android.util.attributeset; import android.util.typedvalue; import android.view.layoutinflater; import android.view.view; import android.view.animation.animation; import android.view.animation.animationutils; import android.widget.imageview; import android.widget.relativelayout; import java.util.timer; import java.util.timertask; import yellow5a5.demo.boilingloadingview.r; /** * created by weiwu on 16/1/2. */ public class boilingpanview extends relativelayout { private view mview; private clipdrawable mwaterdrawable; private waterview mwaterview; private flameview mflameview; private view mpea1; private view mpea2; private imageview mpotato; private imageview mcarrot; private imageview mcoverview; private animation mleftinanim; private animation mrightinanim; private boolean isrightrotate = true; private valueanimator mcoveranim; private boilinganimlistener mboilinganimlistener; public interface boilinganimlistener { //初始动画结束监听 void onfirstanimend(); } public void setboilinganimlistener(boilinganimlistener l) { this.mboilinganimlistener = l; } private handler mhandle = new handler(new handler.callback() { @override public boolean handlemessage(message msg) { if (msg.what == 0x0000) { mwaterdrawable.setlevel(mwaterdrawable.getlevel() + 800); } return false; } }); public boilingpanview(context context) { this(context, null); } public boilingpanview(context context, attributeset attrs) { this(context, attrs, 0); } public boilingpanview(context context, attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); mview = layoutinflater.from(context).inflate(r.layout.boiling_pan, this, true); initview(); initstartanim(); initcoveranim(); } private void initview() { mwaterview = (waterview) mview.findviewbyid(r.id.img_water); mflameview = (flameview) mview.findviewbyid(r.id.flame); mcoverview = (imageview) mview.findviewbyid(r.id.img_cover); mpea1 = mview.findviewbyid(r.id.img_pea1); mpea2 = mview.findviewbyid(r.id.img_pea2); mpotato = (imageview) mview.findviewbyid(r.id.img_potato); mcarrot = (imageview) mview.findviewbyid(r.id.img_carrot); mwaterdrawable = (clipdrawable) mwaterview.getdrawable(); } private void initstartanim() { mleftinanim = animationutils.loadanimation(getcontext(), r.anim.left_in_anim); mrightinanim = animationutils.loadanimation(getcontext(), r.anim.right_in_anim); } /* 抖动的盖子 */ private void initcoveranim() { mcoveranim = valueanimator.offloat(0f, 1f, 0f).setduration(800); mcoveranim.setrepeatmode(animation.reverse); mcoveranim.setrepeatcount(-1); mcoveranim.addupdatelistener(new valueanimator.animatorupdatelistener() { @override public void onanimationupdate(valueanimator animation) { float value = (float) animation.getanimatedvalue(); if (isrightrotate) { mcoverview.setrotation(value * 5); } else { mcoverview.setrotation(-value * 5); } mcoverview.settranslationy(-value * typedvalue.applydimension(typedvalue.complex_unit_dip, 5, getresources().getdisplaymetrics())); } }); mcoveranim.addlistener(new animatorlisteneradapter() { @override public void onanimationrepeat(animator animation) { super.onanimationrepeat(animation); isrightrotate = !isrightrotate; } }); } /* 开始启动的动画 */ public void beginfirstinanim() { mpea1.setvisibility(visible); mpea2.setvisibility(visible); mpotato.setvisibility(visible); mcarrot.setvisibility(visible); mcoverview.setvisibility(visible); mpea1.startanimation(mleftinanim); mpea2.startanimation(mleftinanim); mpotato.startanimation(mleftinanim); mcarrot.startanimation(mrightinanim); mcoverview.startanimation(mrightinanim); mrightinanim.setanimationlistener(new animation.animationlistener() { @override public void onanimationstart(animation animation) { } @override public void onanimationend(animation animation) { if (mboilinganimlistener != null) { //这里是为了给外部留有操作的空间 mboilinganimlistener.onfirstanimend(); } else { beginboilinganim(); } } @override public void onanimationrepeat(animation animation) { } }); } /* 开始加水燃火动画 */ public void beginboilinganim() { final timer timer = new timer(); timer.schedule(new timertask() { @override public void run() { mhandle.sendemptymessage(0x0000); if (mwaterdrawable.getlevel() >= 10000) { timer.cancel(); } } }, 0, 50); mflameview.startflaming(); mcoveranim.start(); } /* 重置动画 */ public void resetanim() { mwaterdrawable.setlevel(0); mwaterview.resetbubbleanim(); mflameview.stopflaming(); mpea1.setvisibility(invisible); mpea2.setvisibility(invisible); mpotato.setvisibility(invisible); mcarrot.setvisibility(invisible); mcoverview.setvisibility(invisible); } }
希望本文所述对大家学习android软件编程有所帮助。