android - How to start animation one by one using onAnimationend()? -
i making animation consist of 7 circles.
i used interpolator , draw circles on canvas, animation quite okay. want each of circles starts 1 one (one after another)/
i developed library project , following java file.
import android.animation.animator; import android.animation.animatorlisteneradapter; import android.animation.objectanimator; import android.animation.valueanimator; import android.content.context; import android.content.res.typedarray; import android.graphics.canvas; import android.graphics.paint; import android.graphics.drawable.shapedrawable; import android.graphics.drawable.shapes.ovalshape; import android.util.attributeset; import android.view.view; import android.view.animation.acceleratedecelerateinterpolator; import android.view.animation.anticipateovershootinterpolator; import android.util.log; import android.view.animation.linearinterpolator; import teamdapsr.loaders.lib.utils.measureutils; /** * created devesh on 08-jul-15. */ public class cubicbezierrotate extends view{ private int i=0; private int radius = 0, centerx, centery; private shapedrawable circleone, circletwo, circlethree, circlesmall; protected paint paint[]; protected valueanimator anim1, anim2, anim3; public int getradius(){ return radius; } public void setradius(int radius){ this.radius = radius; } public cubicbezierrotate(context context, attributeset attrs) { super(context, attrs); typedarray = context.gettheme().obtainstyledattributes( attrs, r.styleable.cubicbezierrotate, 0, 0 ); try { radius = a.getint(r.styleable.cubicbezierrotate_radius, 0); } catch (exception e) { e.printstacktrace(); } { a.recycle(); } init(); } protected void init() { circleone = new shapedrawable(new ovalshape()); circletwo = new shapedrawable(new ovalshape()); circlethree = new shapedrawable(new ovalshape()); circleone.getpaint().setcolor(0x99ff0000); circletwo.getpaint().setcolor(0x9900ff00); circlethree.getpaint().setcolor(0x990000ff); objectanimator animator = objectanimator.ofint(this, "radius", 0, 100); animator.setduration(5000); animator.setinterpolator(new anticipateovershootinterpolator()); /* thread thread = new thread() { @override public void run() { try { while(true) { sleep(50); updatebounds(); } } catch (interruptedexception e) { e.printstacktrace(); } } };*/ paint = new paint[3]; paint[0] = new paint(paint.anti_alias_flag); paint[1] = new paint(paint.anti_alias_flag); paint[2] = new paint(paint.anti_alias_flag); paint[0].setstyle(paint.style.fill); paint[1].setstyle(paint.style.fill); paint[2].setstyle(paint.style.fill); paint[0].setcolor(0x99999999); paint[1].setcolor(0x99999999); paint[2].setcolor(0x99999999); } protected void setupanimations() { anim1 = valueanimator.ofint(0, (int)(0.10*getmeasuredheight())); anim2 = valueanimator.ofint(0, (int)(0.10*getmeasuredheight())); anim3 = valueanimator.ofint(0, (int)(0.10*getmeasuredheight())); anim1.setduration(1500); anim2.setduration(1500); anim3.setduration(1500); anim1.setinterpolator(new acceleratedecelerateinterpolator()); anim2.setinterpolator(new linearinterpolator()); anim3.setinterpolator(new anticipateovershootinterpolator()); anim1.setrepeatmode(valueanimator.reverse); anim2.setrepeatmode(valueanimator.reverse); anim3.setrepeatmode(valueanimator.reverse); anim1.setrepeatcount(valueanimator.infinite); anim2.setrepeatcount(valueanimator.infinite); anim3.setrepeatcount(valueanimator.infinite); // anim2.addupdatelistener(new valueanimator.animatorupdatelistener() { // @override // public void onanimationupdate(valueanimator animation) { // int animprogress = (integer) animation.getanimatedvalue(); // } // }); // // anim1.addlistener(new animatorlisteneradapter() { // @override // public void onanimationend(animator animation) { // super.onanimationend(animation); // } // }); anim1.start(); anim2.start(); anim3.start(); } protected void updatebounds(int radius) { /* circleone.setbounds(radius, radius, radius, radius); circletwo.setbounds(radius, radius, radius, radius); circlethree.setbounds(radius, radius, radius, radius); */ invalidate(); //requestlayout(); } @override public void ondraw(canvas canvas) { log.i("radius", "radius1 = " + (int) (anim1.getanimatedvalue())); log.i("radius", "radius2 = " + (int) (anim2.getanimatedvalue())); log.i("radius", "radius3 = " + (int) (anim3.getanimatedvalue())); canvas.drawcircle(50, getmeasuredheight()/2, (int)(anim1.getanimatedvalue()), paint[0]); canvas.drawcircle(90, getmeasuredheight()/2, (int)(anim2.getanimatedvalue()), paint[1]); canvas.drawcircle(130, getmeasuredheight()/2, (int)(anim3.getanimatedvalue()), paint[2]); canvas.drawcircle(170, getmeasuredheight()/2, (int)(anim2.getanimatedvalue()), paint[2]); canvas.drawcircle(210, getmeasuredheight()/2, (int)(anim2.getanimatedvalue()), paint[2]); canvas.drawcircle(250, getmeasuredheight()/2, (int)(anim2.getanimatedvalue()), paint[2]); canvas.drawcircle(290, getmeasuredheight()/2, (int)(anim2.getanimatedvalue()), paint[2]); // while (i<7) // { // switch (i) // { // case 0 : // canvas.drawcircle(50, getmeasuredheight()/2, (int)(anim2.getanimatedvalue()), paint[0]); // canvas.drawcircle(90, getmeasuredheight()/2, (int)(anim2.getanimatedvalue()), paint[1]); // canvas.drawcircle(130, getmeasuredheight()/2, (int)(anim2.getanimatedvalue()), paint[2]); // canvas.drawcircle(170, getmeasuredheight()/2, (int)(anim2.getanimatedvalue()), paint[2]); // try { // thread.sleep(1000); // } catch (interruptedexception e) { // e.printstacktrace(); // } // i++; // break; // case 1 : // // canvas.drawcircle(210, getmeasuredheight()/2, (int)(anim2.getanimatedvalue()), paint[2]); // canvas.drawcircle(250, getmeasuredheight()/2, (int)(anim2.getanimatedvalue()), paint[2]); // canvas.drawcircle(290, getmeasuredheight()/2, (int)(anim2.getanimatedvalue()), paint[2]); // try { // thread.sleep(50); // } catch (interruptedexception e) { // e.printstacktrace(); // } // break; // // } // } try { thread.sleep(50); } catch (interruptedexception e) { e.printstacktrace(); } updatebounds(0); } @override protected void onsizechanged(int w, int h, int oldw, int oldh) { super.onsizechanged(w, h, oldw, oldh); } @override public void onmeasure(int widthmeasurespec, int heightmeasurespec) { // width measurement int widthsize = measureutils.getmeasurement(widthmeasurespec, getdesiredwidth()); // height measurement int heightsize = measureutils.getmeasurement(heightmeasurespec, getdesiredheight()); centerx = (int)(0.5*measurespec.getsize(widthmeasurespec)); centery = (int)(0.5*measurespec.getsize(heightmeasurespec)); //must call store measurements setmeasureddimension(widthsize + 10, heightsize + 10); setupanimations(); } private int getdesiredwidth() { // to-do calculate width child components. return 2*radius+1000; } private int getdesiredheight() { // to-do calculate height chile components. return 2*radius+100; } }
please me out. want start each of circle animation 1 one (the next circle starts when previous 1 end). , whole cycle "1 2 3 4 5 6 7" end "7 6 5 4 3 2 1"
why not using animation-list
?? .
step 1 : define images every step of animation
step 2 : create drawable xml file should contain (example of animating 7 circles ):
<animation-list android:id="@+id/progresscircles" android:oneshot="false" xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/progress_circle1" android:duration="150" /> <item android:drawable="@drawable/progress_circle2" android:duration="150" /> <item android:drawable="@drawable/progress_circle3" android:duration="150" /> <item android:drawable="@drawable/progress_circle4" android:duration="150" /> <item android:drawable="@drawable/progress_circle5" android:duration="150" /> <item android:drawable="@drawable/progress_circle6" android:duration="150" /> <item android:drawable="@drawable/progress_circle7" android:duration="150" /> </animation-list>
step 3 : create imageview in layout file , set progresscircles
android:src
of image :
<imageview android:id="@+id/progress_bar" android:layout_alignparentright="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/progresscircles" />
step 4 (optionnal) : retrieve the reference image , animationdrawable in order track progress example etc :
imageview imgprogress = (imageview)findviewbyid(r.id.progress_bar); if (imgprogress != null) { imgprogress.setvisibility(view.visible); animationdrawable animation = (animationdrawable)progress.getdrawable(); animation.setcallback(progress); animation.setvisible(true, true); }
Comments
Post a Comment