Search in sources :

Example 56 with Canvas

use of android.graphics.Canvas in project platform_frameworks_base by android.

the class TransitionUtils method createViewBitmap.

/**
     * Creates a Bitmap of the given view, using the Matrix matrix to transform to the local
     * coordinates. <code>matrix</code> will be modified during the bitmap creation.
     *
     * <p>If the bitmap is large, it will be scaled uniformly down to at most 1MB size.</p>
     * @param view The view to create a bitmap for.
     * @param matrix The matrix converting the view local coordinates to the coordinates that
     *               the bitmap will be displayed in. <code>matrix</code> will be modified before
     *               returning.
     * @param bounds The bounds of the bitmap in the destination coordinate system (where the
     *               view should be presented. Typically, this is matrix.mapRect(viewBounds);
     * @return A bitmap of the given view or null if bounds has no width or height.
     */
public static Bitmap createViewBitmap(View view, Matrix matrix, RectF bounds) {
    Bitmap bitmap = null;
    int bitmapWidth = Math.round(bounds.width());
    int bitmapHeight = Math.round(bounds.height());
    if (bitmapWidth > 0 && bitmapHeight > 0) {
        float scale = Math.min(1f, ((float) MAX_IMAGE_SIZE) / (bitmapWidth * bitmapHeight));
        bitmapWidth *= scale;
        bitmapHeight *= scale;
        matrix.postTranslate(-bounds.left, -bounds.top);
        matrix.postScale(scale, scale);
        bitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        canvas.concat(matrix);
        view.draw(canvas);
    }
    return bitmap;
}
Also used : Bitmap(android.graphics.Bitmap) Canvas(android.graphics.Canvas)

Example 57 with Canvas

use of android.graphics.Canvas in project platform_frameworks_base by android.

the class RenderDrawable method renderImage.

private BufferedImage renderImage(HardwareConfig hardwareConfig, Drawable d, BridgeContext context) {
    // create a simple FrameLayout
    FrameLayout content = new FrameLayout(context);
    // get the actual Drawable object to draw
    content.setBackground(d);
    // set the AttachInfo on the root view.
    AttachInfo_Accessor.setAttachInfo(content);
    // measure
    int w = d.getIntrinsicWidth();
    int h = d.getIntrinsicHeight();
    final int screenWidth = hardwareConfig.getScreenWidth();
    final int screenHeight = hardwareConfig.getScreenHeight();
    if (w == -1 || h == -1) {
        // Use screen size when either intrinsic width or height isn't available
        w = screenWidth;
        h = screenHeight;
    } else if (w > screenWidth || h > screenHeight) {
        // If image wouldn't fit to the screen, resize it to avoid cropping.
        // We need to find scale such that scale * w <= screenWidth, scale * h <= screenHeight
        double scale = Math.min((double) screenWidth / w, (double) screenHeight / h);
        // scale * w / scale * h = w / h, so, proportions are preserved.
        w = (int) Math.floor(scale * w);
        h = (int) Math.floor(scale * h);
    }
    int w_spec = MeasureSpec.makeMeasureSpec(w, MeasureSpec.EXACTLY);
    int h_spec = MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY);
    content.measure(w_spec, h_spec);
    // now do the layout.
    content.layout(0, 0, w, h);
    // preDraw setup
    AttachInfo_Accessor.dispatchOnPreDraw(content);
    // draw into a new image
    BufferedImage image = getImage(w, h);
    // create an Android bitmap around the BufferedImage
    Bitmap bitmap = Bitmap_Delegate.createBitmap(image, true, /*isMutable*/
    hardwareConfig.getDensity());
    // create a Canvas around the Android bitmap
    Canvas canvas = new Canvas(bitmap);
    canvas.setDensity(hardwareConfig.getDensity().getDpiValue());
    // and draw
    content.draw(canvas);
    return image;
}
Also used : Bitmap(android.graphics.Bitmap) FrameLayout(android.widget.FrameLayout) Canvas(android.graphics.Canvas) BufferedImage(java.awt.image.BufferedImage)

Example 58 with Canvas

use of android.graphics.Canvas in project platform_frameworks_base by android.

the class RenderSessionImpl method render.

/**
     * Renders the scene.
     * <p>
     * {@link #acquire(long)} must have been called before this.
     *
     * @param freshRender whether the render is a new one and should erase the existing bitmap (in
     *      the case where bitmaps are reused). This is typically needed when not playing
     *      animations.)
     *
     * @throws IllegalStateException if the current context is different than the one owned by
     *      the scene, or if {@link #acquire(long)} was not called.
     *
     * @see SessionParams#getRenderingMode()
     * @see RenderSession#render(long)
     */
public Result render(boolean freshRender) {
    checkLock();
    SessionParams params = getParams();
    try {
        if (mViewRoot == null) {
            return ERROR_NOT_INFLATED.createResult();
        }
        measure(params);
        HardwareConfig hardwareConfig = params.getHardwareConfig();
        Result renderResult = SUCCESS.createResult();
        if (params.isLayoutOnly()) {
            // delete the canvas and image to reset them on the next full rendering
            mImage = null;
            mCanvas = null;
        } else {
            // draw the views
            // create the BufferedImage into which the layout will be rendered.
            boolean newImage = false;
            // When disableBitmapCaching is true, we do not reuse mImage and
            // we create a new one in every render.
            // This is useful when mImage is just a wrapper of Graphics2D so
            // it doesn't get cached.
            boolean disableBitmapCaching = Boolean.TRUE.equals(params.getFlag(RenderParamsFlags.FLAG_KEY_DISABLE_BITMAP_CACHING));
            if (mNewRenderSize || mCanvas == null || disableBitmapCaching) {
                mNewRenderSize = false;
                if (params.getImageFactory() != null) {
                    mImage = params.getImageFactory().getImage(mMeasuredScreenWidth, mMeasuredScreenHeight);
                } else {
                    mImage = new BufferedImage(mMeasuredScreenWidth, mMeasuredScreenHeight, BufferedImage.TYPE_INT_ARGB);
                    newImage = true;
                }
                if (params.isBgColorOverridden()) {
                    // since we override the content, it's the same as if it was a new image.
                    newImage = true;
                    Graphics2D gc = mImage.createGraphics();
                    gc.setColor(new Color(params.getOverrideBgColor(), true));
                    gc.setComposite(AlphaComposite.Src);
                    gc.fillRect(0, 0, mMeasuredScreenWidth, mMeasuredScreenHeight);
                    gc.dispose();
                }
                // create an Android bitmap around the BufferedImage
                Bitmap bitmap = Bitmap_Delegate.createBitmap(mImage, true, /*isMutable*/
                hardwareConfig.getDensity());
                if (mCanvas == null) {
                    // create a Canvas around the Android bitmap
                    mCanvas = new Canvas(bitmap);
                } else {
                    mCanvas.setBitmap(bitmap);
                }
                mCanvas.setDensity(hardwareConfig.getDensity().getDpiValue());
            }
            if (freshRender && !newImage) {
                Graphics2D gc = mImage.createGraphics();
                gc.setComposite(AlphaComposite.Src);
                gc.setColor(new Color(0x00000000, true));
                gc.fillRect(0, 0, mMeasuredScreenWidth, mMeasuredScreenHeight);
                // done
                gc.dispose();
            }
            if (mElapsedFrameTimeNanos >= 0) {
                long initialTime = System_Delegate.nanoTime();
                if (!mFirstFrameExecuted) {
                    // We need to run an initial draw call to initialize the animations
                    render(getContext(), mViewRoot, NOP_CANVAS, mMeasuredScreenWidth, mMeasuredScreenHeight);
                    // The first frame will initialize the animations
                    Choreographer_Delegate.doFrame(initialTime);
                    mFirstFrameExecuted = true;
                }
                // Second frame will move the animations
                Choreographer_Delegate.doFrame(initialTime + mElapsedFrameTimeNanos);
            }
            renderResult = render(getContext(), mViewRoot, mCanvas, mMeasuredScreenWidth, mMeasuredScreenHeight);
        }
        mSystemViewInfoList = visitAllChildren(mViewRoot, 0, params.getExtendedViewInfoMode(), false);
        // success!
        return renderResult;
    } catch (Throwable e) {
        // get the real cause of the exception.
        Throwable t = e;
        while (t.getCause() != null) {
            t = t.getCause();
        }
        return ERROR_UNKNOWN.createResult(t.getMessage(), t);
    }
}
Also used : SessionParams(com.android.ide.common.rendering.api.SessionParams) Bitmap(android.graphics.Bitmap) HardwareConfig(com.android.ide.common.rendering.api.HardwareConfig) Color(java.awt.Color) Canvas(android.graphics.Canvas) NopCanvas(com.android.layoutlib.bridge.android.graphics.NopCanvas) BufferedImage(java.awt.image.BufferedImage) Result(com.android.ide.common.rendering.api.Result) Graphics2D(java.awt.Graphics2D)

Example 59 with Canvas

use of android.graphics.Canvas in project platform_packages_apps_launcher by android.

the class Utilities method createIconThumbnail.

/**
     * Returns a Drawable representing the thumbnail of the specified Drawable.
     * The size of the thumbnail is defined by the dimension
     * android.R.dimen.launcher_application_icon_size.
     *
     * This method is not thread-safe and should be invoked on the UI thread only.
     *
     * @param icon The icon to get a thumbnail of.
     * @param context The application's context.
     *
     * @return A thumbnail for the specified icon or the icon itself if the
     *         thumbnail could not be created. 
     */
static Drawable createIconThumbnail(Drawable icon, Context context) {
    if (sIconWidth == -1) {
        final Resources resources = context.getResources();
        sIconWidth = sIconHeight = (int) resources.getDimension(android.R.dimen.app_icon_size);
    }
    int width = sIconWidth;
    int height = sIconHeight;
    float scale = 1.0f;
    if (icon instanceof PaintDrawable) {
        PaintDrawable painter = (PaintDrawable) icon;
        painter.setIntrinsicWidth(width);
        painter.setIntrinsicHeight(height);
    } else if (icon instanceof BitmapDrawable) {
        // Ensure the bitmap has a density.
        BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;
        Bitmap bitmap = bitmapDrawable.getBitmap();
        if (bitmap.getDensity() == Bitmap.DENSITY_NONE) {
            bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics());
        }
    }
    int iconWidth = icon.getIntrinsicWidth();
    int iconHeight = icon.getIntrinsicHeight();
    if (width > 0 && height > 0) {
        if (width < iconWidth || height < iconHeight || scale != 1.0f) {
            final float ratio = (float) iconWidth / iconHeight;
            if (iconWidth > iconHeight) {
                height = (int) (width / ratio);
            } else if (iconHeight > iconWidth) {
                width = (int) (height * ratio);
            }
            final Bitmap.Config c = icon.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
            final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c);
            final Canvas canvas = sCanvas;
            canvas.setBitmap(thumb);
            // Copy the old bounds to restore them later
            // If we were to do oldBounds = icon.getBounds(),
            // the call to setBounds() that follows would
            // change the same instance and we would lose the
            // old bounds
            sOldBounds.set(icon.getBounds());
            final int x = (sIconWidth - width) / 2;
            final int y = (sIconHeight - height) / 2;
            icon.setBounds(x, y, x + width, y + height);
            icon.draw(canvas);
            icon.setBounds(sOldBounds);
            icon = new FastBitmapDrawable(thumb);
        } else if (iconWidth < width && iconHeight < height) {
            final Bitmap.Config c = Bitmap.Config.ARGB_8888;
            final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c);
            final Canvas canvas = sCanvas;
            canvas.setBitmap(thumb);
            sOldBounds.set(icon.getBounds());
            final int x = (width - iconWidth) / 2;
            final int y = (height - iconHeight) / 2;
            icon.setBounds(x, y, x + iconWidth, y + iconHeight);
            icon.draw(canvas);
            icon.setBounds(sOldBounds);
            icon = new FastBitmapDrawable(thumb);
        }
    }
    return icon;
}
Also used : Bitmap(android.graphics.Bitmap) Canvas(android.graphics.Canvas) PaintDrawable(android.graphics.drawable.PaintDrawable) Resources(android.content.res.Resources) BitmapDrawable(android.graphics.drawable.BitmapDrawable) Paint(android.graphics.Paint)

Example 60 with Canvas

use of android.graphics.Canvas in project androidquery by androidquery.

the class BitmapAjaxCallback method getRoundedCornerBitmap.

private static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);
    final float roundPx = pixels;
    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    return output;
}
Also used : RectF(android.graphics.RectF) Bitmap(android.graphics.Bitmap) Rect(android.graphics.Rect) PorterDuffXfermode(android.graphics.PorterDuffXfermode) Canvas(android.graphics.Canvas) Paint(android.graphics.Paint) Paint(android.graphics.Paint)

Aggregations

Canvas (android.graphics.Canvas)1237 Bitmap (android.graphics.Bitmap)890 Paint (android.graphics.Paint)609 Rect (android.graphics.Rect)257 BitmapDrawable (android.graphics.drawable.BitmapDrawable)192 RectF (android.graphics.RectF)136 PorterDuffXfermode (android.graphics.PorterDuffXfermode)103 Matrix (android.graphics.Matrix)80 Point (android.graphics.Point)76 Drawable (android.graphics.drawable.Drawable)68 BitmapShader (android.graphics.BitmapShader)60 Test (org.junit.Test)46 IOException (java.io.IOException)43 FileOutputStream (java.io.FileOutputStream)40 View (android.view.View)39 Path (android.graphics.Path)38 File (java.io.File)37 SuppressLint (android.annotation.SuppressLint)36 ColorMatrix (android.graphics.ColorMatrix)36 ColorMatrixColorFilter (android.graphics.ColorMatrixColorFilter)33