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

Kara
  • 6,115
  • 16
  • 50
  • 57
The Butcher
  • 235
  • 2
  • 9
  • Creates rounded corners for the shape. Applies only when the shape is a rectangle. http://developer.android.com/guide/topics/resources/drawable-resource.html#corners-element – Android Developer May 07 '13 at 08: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"... – The Butcher May 07 '13 at 08:31
  • Hi Sam, the link to youtube "this" is broken... – The Butcher May 10 '13 at 10:36
  • @TheButcher, [here](http://youtu.be/8CHZ7gL5fdA) you go. – Sam R. May 10 '13 at 13:07
  • Ok, I saw the video it looks like this is what I was looking for. How does it work, source code, something? Do you know if there is an option to use drawable for texture? – The Butcher May 12 '13 at 11:03
  • 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 '13 at 12:39
  • @TheButcher, for the color or texture, as long as you can create such animation with AE it doesn't really matter what image you use. It's not OpenGL ES that you bother with texturing. – Sam R. May 12 '13 at 12:41
  • @SamRad where is the movie? – udidu May 12 '13 at 21:10
  • @udidu, I deleted it. Do you want it? – Sam R. May 12 '13 at 21:19
  • @SamRad Yes please.. can you upload it again? – udidu May 13 '13 at 08:02
  • @udidu, [Stroke](http://youtu.be/E0j_sz7A7Eg) – Sam R. May 13 '13 at 17:22
  • @TheButcher, just Google `After Effect Stroke`. Nothing really special cuz AE has a bundled effect called `Stroke`. You just need to learn how to work with it. – Sam R. May 13 '13 at 21:28

2 Answers2

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);
}

}
ObscureRobot
  • 7,306
  • 2
  • 27
  • 36
Oren Bengigi
  • 994
  • 9
  • 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.

Sam R.
  • 16,027
  • 12
  • 69
  • 122
  • 1
    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. – The Butcher May 07 '13 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 07 '13 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.. :( – The Butcher May 08 '13 at 09:56