Tuesday 15 February 2011

Android Custom View Animation -



Android Custom View Animation -

hy, i'm trying animation of circle drawn on canvas. can pretty objectanimator, i'd start animation when view finishes loading or finishes drawing. if start animation in init(), animation property "ahead" of actual drawing, need start on callback when whole view set up. onmeasure() or onsizechanged() 2 called many times , if have nested layouts doesn't work properly. if utilize startdelay() works don't think accurate procedure.

here basic custom view class animation property changes radius of circle.

public class customview extends view { private static final string tag = customview.class.tostring(); public customview(final context context) { super(context); init(context); } public customview(final context context, final attributeset attrs) { super(context, attrs); init(context); } public customview(final context context, final attributeset attrs, final int defstyle) { super(context, attrs, defstyle); init(context); } private void init(final context context) { // outer circle paint mpaint = new paint(); // adds anti-aliasing drawed elements mpaint.setantialias(true); mpaint.setfilterbitmap(true); mpaint.setstrokewidth(1); mpaint.setstrokecap(paint.cap.square); mpaint.setstyle(paint.style.fill); final int animationtime = getresources().getinteger(animation_time_id); progressanimator = objectanimator.offloat(this, "animprogress", 0f, 0f); progressanimator.setduration(animationtime); log.d(tag, "init ended"); //startanimationcircle(50f); } @override public void ondraw(final canvas canvas) { super.ondraw(canvas); canvas.drawcircle(this.getwidth()/2, this.getheight()/2, animprogress, mpaint); } @override protected void onsizechanged (int w, int h, int oldw, int oldh) { //startanimationcircle(50f); } /** * onmeasure() called automatically right after phone call measure() */ @override protected void onmeasure(final int widthmeasurespec, final int heightmeasurespec) { super.onmeasure(widthmeasurespec, heightmeasurespec); //startanimationcircle(50f); } private paint mpaint; private static final int animation_time_id = android.r.integer.config_mediumanimtime; private float animprogress; private objectanimator progressanimator; public float getanimprogress() { homecoming animprogress; } public void setanimprogress(float animprogress) { this.animprogress = animprogress; this.invalidate(); } public void startanimationcircle(float size) { progressanimator.setfloatvalues(animprogress, size); //progressanimator.setstartdelay(2000); progressanimator.start(); } }

and xml also.

<com.your-package.customview android:layout_width="match_parent" android:layout_height="match_parent" />

android animation view custom-view

No comments:

Post a Comment