9

I'm trying to have an animation drawing stroke rectangle:

Rounded Rectangle Stroke Animation

Any kickstart for this issue will be helpful. Using Views,Canvas anything.

Thanks

12
  • <corners> Creates rounded corners for the shape. Applies only when the shape is a rectangle. developer.android.com/guide/topics/resources/… May 7, 2013 at 8:22
  • thanks for your help I draw the rounded corner shape already in few ways. The main problem is to make the shape drawing with animation filling the stroke like a "snake"... May 7, 2013 at 8:31
  • Hi Sam, the link to youtube "this" is broken... May 10, 2013 at 10:36
  • @TheButcher, here you go.
    – Sam R.
    May 10, 2013 at 13:07
  • 1
    @TheButcher, I deleted the source code since I saw u got ur answer from @Oren and I already have answered you down there. I took the first approach. I created a stroke animation with Adobe AE and then exported it as PNG sequence. Created an animation-list XML and bind it to a View. That's it.
    – Sam R.
    May 12, 2013 at 12:39

2 Answers 2

4

Ok, here's is something to get you started, it is not the complete solution but from here you can complete your task.

What I'm doing is dynamically updating my mask according to the progress. I just drew a line, but in your case you need to draw four lines that will make a masked rectangle according to the progress. Here's the code let me know if that helps:

public class DrawView extends View implements Runnable {

Bitmap mProgressBitmap;
Bitmap mMaskProgressBitmap;
Bitmap mResultBitmap;

Canvas mTempCanvas;
Canvas mMaskCanvas;

Paint mPaint;

Paint mWhitePaint;

Handler mHandler = new Handler();

float mProgress = 0;

static final long FRAME_TIME = 50;

public DrawView(Context context, AttributeSet attrs) {
    super(context, attrs);

    InputStream resource = getResources().openRawResource(R.drawable.timer);
    mProgressBitmap = BitmapFactory.decodeStream(resource);

    mMaskProgressBitmap = Bitmap.createBitmap(mProgressBitmap.getWidth(), mProgressBitmap.getHeight(), Bitmap.Config.ARGB_8888);
    mMaskCanvas = new Canvas(mMaskProgressBitmap);
    mMaskCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);

    mResultBitmap = Bitmap.createBitmap(mProgressBitmap.getWidth(), mProgressBitmap.getHeight(), Bitmap.Config.ARGB_8888);

    mTempCanvas = new Canvas(mResultBitmap);

    mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    mPaint.setDither(true);

    mWhitePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mWhitePaint.setColor(Color.WHITE);
    mWhitePaint.setStrokeWidth(50);

    mHandler.postDelayed(this, FRAME_TIME);
}

@Override
public void onDraw(Canvas canvas) {

    mTempCanvas.drawBitmap(mMaskProgressBitmap, 0, 0, null);
    mTempCanvas.drawBitmap(mProgressBitmap, 0, 0, mPaint);

    canvas.drawBitmap(mResultBitmap, 0, 0, null);
}

@Override
public void run() {

    mProgress += 0.01f;

    mMaskCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
    mMaskCanvas.drawLine(0, 0, (float)mProgressBitmap.getWidth() * mProgress, 0, mWhitePaint);

    this.invalidate();

    mHandler.postDelayed(this, FRAME_TIME);
}

}
1
  • how to use this view ?
    – Arth Tilva
    Aug 31, 2018 at 5:17
1

I think you can achieve something very basic with AnimationDrawable and create a frame-by-frame animation. Obviously, the more frame you use the smoother it becomes.

<!-- Animation frames are wheel0.png -- wheel5.png files inside the
 res/drawable/ folder -->
 <animation-list android:id="@+id/selected" android:oneshot="false">
    <item android:drawable="@drawable/wheel0" android:duration="50" />
    <item android:drawable="@drawable/wheel1" android:duration="50" />
    ...
 </animation-list>

Other option would be to use ClipDrawable to mask your rectangle and animate the mask. There is a tutorial to Customize Your Progress Bar which uses the same logic for your purpose. I hope so.

Look at this sample. It's flash but it uses the same clipping technique to achieve a stroking effect.

If I'm not mistaken, Android Canvas has a few clip methods to mask your graphics: clipPath, clipRect and clipRegion. But I'm not sure if it can be animated or not. Check them out.

3
  • Hi Sam I took all your suggestions, but every suggestion has a issue. 1) The first has bad smoothing and non customable option. 2) The second one with Customize Progress Bar display the rectangle edge in parallel, and not doing it like a fill snake that growing. 3)I'm trying not using flash in my app right now. 4)This is the last issue I'm going to check I hope I will find a way to make an animation to the clipRect. thanks any way. May 7, 2013 at 21:31
  • @TheButcher, Honestly I haven't done something like that but I'm experiencing something similar for the time being. I'll let you know if I find anything that actually works. By the way, I just mentioned the flash one just to give you the idea behind stroke effect. Of course it's a terrible idea to use flash on Android :)
    – Sam R.
    May 7, 2013 at 21:39
  • This is A really difficult issue, I think I spend almost a week on this feature but I didn't find a solution for this. Any help? @SamRad, the issue with the mask is a good kick start but I sill have the problem of drawing filled paths to be the mask.. :( May 8, 2013 at 9:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.