So, I am dynamically creating an image within my app by animating various Views that I display to the user in the main layout of my application.
Currently I am generating my scene within a RelativeLayout, taking the image of the layout as a bitmap, then saving the bitmap to SD to be accessed by the appwidget via uri.
This is all working great, but... in an attmept to create rounded corners for the appwidget image, i've tried using these two snippets that I found here.
My Problem:
is that this method generates a drawable (whose rounded corners look perfect if displayed as a drawable) but I need to export these transparent corners as an image file. The drawToBitmap method in CustomView below, does generate a bitmap image, but the corners are full and square.
/**
* shows a bitmap as if it had rounded corners. based on :
* http://rahulswackyworld.blogspot.co.il/2013/04/android-drawables-with-rounded_7.html
*/
public class RoundedCornersDrawable extends BitmapDrawable {
private final BitmapShader bitmapShader;
private final Paint p;
private final RectF rect;
private final float borderRadius;
public RoundedCornersDrawable(final Resources resources, final Bitmap bitmap, final float borderRadius) {
super(resources, bitmap);
bitmapShader = new BitmapShader(getBitmap(), Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
final Bitmap b = getBitmap();
p = getPaint();
p.setAntiAlias(true);
p.setShader(bitmapShader);
final int w = b.getWidth(), h = b.getHeight();
rect = new RectF(0, 0, w, h);
this.borderRadius = borderRadius < 0 ? 0.15f * Math.min(w, h) : borderRadius;
}
@Override
public void draw(final Canvas canvas) {
canvas.drawRoundRect(rect, borderRadius, borderRadius, p);
}
}
and
public class CustomView extends ImageView {
private FrameLayout mMainContainer;
private boolean mIsDirty=false;
// TODO for each change of views/content, set mIsDirty to true and call invalidate
@Override
protected void onDraw(final Canvas canvas) {
if (mIsDirty) {
mIsDirty = false;
drawContent();
return;
}
super.onDraw(canvas);
}
/**
* draws the view's content to a bitmap. code based on :
* http://nadavfima.com/android-snippet-inflate-a-layout-draw-to-a-bitmap/
*/
public static Bitmap drawToBitmap(final View viewToDrawFrom, final int width, final int height) {
// Create a new bitmap and a new canvas using that bitmap
final Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(bmp);
viewToDrawFrom.setDrawingCacheEnabled(true);
// Supply measurements
viewToDrawFrom.measure(MeasureSpec.makeMeasureSpec(canvas.getWidth(), MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(canvas.getHeight(), MeasureSpec.EXACTLY));
// Apply the measures so the layout would resize before drawing.
viewToDrawFrom.layout(0, 0, viewToDrawFrom.getMeasuredWidth(), viewToDrawFrom.getMeasuredHeight());
// and now the bmp object will actually contain the requested layout
canvas.drawBitmap(viewToDrawFrom.getDrawingCache(), 0, 0, new Paint());
return bmp;
}
private void drawContent() {
if (getMeasuredWidth() <= 0 || getMeasuredHeight() <= 0)
return;
final Bitmap bitmap = drawToBitmap(mMainContainer, getMeasuredWidth(), getMeasuredHeight());
final RoundedCornersDrawable drawable = new RoundedCornersDrawable(getResources(), bitmap, 15);
setImageDrawable(drawable);
}
}
I understand that a bitmap doesn't carry the alpha information that is needed to have transparent rounded corners in the image, So I tried saving the file as PNG like so;
RCD_test is a RoundedCornersDrawable
Bitmap bitmap = RCD_test.getBitmap();
bitmap.setHasAlpha(true);
OutputStream stream = new
FileOutputStream(Environment.getExternalStorageDirectory().getPath()+"/test/screenshottest.png");
bitmap.compress(CompressFormat.PNG, 100, stream);
stream.close();
but to no avail. This whole approach may seem convoluded, but this is what I've come up with to address the constraints of AppWidget's RemoteViews.
My Question:
How can I take this RoundedCornersDrawable, and export it as a PNG file that properly depicts it's beautiful trasnparent corners?
thanks in advance for the help, and I'm open to any and all suggestions on different approaches to the problem as a whole!