AppWidget图象带圆角 [英] AppWidget image with rounded corners

查看:546
本文介绍了AppWidget图象带圆角的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我用动态的动画,我要展示我的应用程序的主要布局了用户的各种视图中创建我的应用程序的图像。

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.

目前我生成一个RelativeLayout的在我的场景,走布局的图像作为位图,然后保存位图到SD由通过URI的appwidget进行访问。

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.

这是所有伟大的工作,但是...在attmept创建圆角为appwidget形象,我已经使用这两个片段,我发现<一试href="http://stackoverflow.com/questions/16161448/how-to-make-layout-with-rounded-corners">here.

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.

我的问题:

是,这种方法生成一个可绘制(其圆润的边角看起来完美的,如果显示为绘制),但我需要这些透明的角落导出为一个图像文件。所述drawToBitmap方法在CustomView下面,也会产生位图图像,但角落是充分和正方形。

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

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

据我所知,位图不执行是需要有图像中的透明圆角的alpha信息, 于是,我试着将文件保存为PNG像这样;

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是RoundedCornersDrawable

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

但无济于事。这整个方法看似convoluded,但是这是我想出来的解决AppWidget的RemoteViews的限制。

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.

我的问题:

我怎么能采取这种RoundedCornersDrawable,并将其导出为能够正确描述它的美丽trasnparent角落?PNG文件

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!

推荐答案

除了这个问题:

事实证明,我的AppWidget的大小裁剪图像   一直以来。裁剪它恰好能移开圆角,和   只是这么一点,以不被明显地裁剪。因此,对于任何人   那遇到这个问题......所有这些方法的工作,为这个   目的(只是别忘了让你的AppWidget大到足以见   的结果)。

'It turns out that the size of my AppWidget was clipping the image the whole time. Clipping it just enough to remove the rounded corners, and just so little as to not have been obviously clipped. So for anyone that comes across this question... all of these method's work for this purpose (just don't forget to make your AppWidget big enough to see the results).'

答:

我发现这是至关重要的通过CustomView.drawToBitMap()方法,将其导出到PNG文件之前也通过RoundedCornersDrawable。

I found it was critical to also pass the RoundedCornersDrawable through the CustomView.drawToBitMap() method before exporting it to the PNG file.

我希望这一切可以帮助别人在路上一天!

I hope this all helps someone down the road someday!

这篇关于AppWidget图象带圆角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆