Search in sources :

Example 66 with Canvas

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

the class CompareActivity method loadBitmaps.

protected void loadBitmaps() {
    Trace.traceBegin(Trace.TRACE_TAG_ALWAYS, "loadBitmaps");
    if (forceRecreateBitmaps()) {
        int width = mSoftwareBitmap.getWidth();
        int height = mSoftwareBitmap.getHeight();
        mSoftwareBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        mHardwareBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    }
    Trace.traceBegin(Trace.TRACE_TAG_ALWAYS, "softwareDraw");
    mHardwareView.draw(new Canvas(mSoftwareBitmap));
    Trace.traceEnd(Trace.TRACE_TAG_ALWAYS);
    try {
        Method getHardwareLayer = View.class.getDeclaredMethod("getHardwareLayer");
        if (!getHardwareLayer.isAccessible())
            getHardwareLayer.setAccessible(true);
        Object hardwareLayer = getHardwareLayer.invoke(mHardwareView);
        if (hardwareLayer == null) {
            Log.d(LOG_TAG, "failure to access hardware layer");
            return;
        }
        Method copyInto = hardwareLayer.getClass().getDeclaredMethod("copyInto", Bitmap.class);
        if (!copyInto.isAccessible())
            copyInto.setAccessible(true);
        Trace.traceBegin(Trace.TRACE_TAG_ALWAYS, "copyInto");
        boolean success = (Boolean) copyInto.invoke(hardwareLayer, mHardwareBitmap);
        Trace.traceEnd(Trace.TRACE_TAG_ALWAYS);
        if (!success) {
            Log.d(LOG_TAG, "failure to copy hardware layer into bitmap");
        }
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }
    Trace.traceEnd(Trace.TRACE_TAG_ALWAYS);
}
Also used : Canvas(android.graphics.Canvas) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 67 with Canvas

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

the class EmulatorDisplayOverlay method drawIfNeeded.

private void drawIfNeeded() {
    if (!mDrawNeeded || !mVisible) {
        return;
    }
    mDrawNeeded = false;
    Rect dirty = new Rect(0, 0, mScreenSize.x, mScreenSize.y);
    Canvas c = null;
    try {
        c = mSurface.lockCanvas(dirty);
    } catch (IllegalArgumentException e) {
    } catch (OutOfResourcesException e) {
    }
    if (c == null) {
        return;
    }
    c.drawColor(Color.TRANSPARENT, PorterDuff.Mode.SRC);
    mSurfaceControl.setPosition(0, 0);
    // Always draw the overlay with square dimensions
    int size = Math.max(mScreenSize.x, mScreenSize.y);
    mOverlay.setBounds(0, 0, size, size);
    mOverlay.draw(c);
    mSurface.unlockCanvasAndPost(c);
}
Also used : Rect(android.graphics.Rect) OutOfResourcesException(android.view.Surface.OutOfResourcesException) Canvas(android.graphics.Canvas) Point(android.graphics.Point)

Example 68 with Canvas

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

the class SubscriptionInfo method createIconBitmap.

/**
     * Creates and returns an icon {@code Bitmap} to represent this {@code SubscriptionInfo} in a user
     * interface.
     *
     * @param context A {@code Context} to get the {@code DisplayMetrics}s from.
     *
     * @return A bitmap icon for this {@code SubscriptionInfo}.
     */
public Bitmap createIconBitmap(Context context) {
    int width = mIconBitmap.getWidth();
    int height = mIconBitmap.getHeight();
    DisplayMetrics metrics = context.getResources().getDisplayMetrics();
    // Create a new bitmap of the same size because it will be modified.
    Bitmap workingBitmap = Bitmap.createBitmap(metrics, width, height, mIconBitmap.getConfig());
    Canvas canvas = new Canvas(workingBitmap);
    Paint paint = new Paint();
    // Tint the icon with the color.
    paint.setColorFilter(new PorterDuffColorFilter(mIconTint, PorterDuff.Mode.SRC_ATOP));
    canvas.drawBitmap(mIconBitmap, 0, 0, paint);
    paint.setColorFilter(null);
    // Write the sim slot index.
    paint.setAntiAlias(true);
    paint.setTypeface(Typeface.create("sans-serif", Typeface.NORMAL));
    paint.setColor(Color.WHITE);
    // Set text size scaled by density
    paint.setTextSize(TEXT_SIZE * metrics.density);
    // Convert sim slot index to localized string
    final String index = String.format("%d", mSimSlotIndex + 1);
    final Rect textBound = new Rect();
    paint.getTextBounds(index, 0, 1, textBound);
    final float xOffset = (width / 2.f) - textBound.centerX();
    final float yOffset = (height / 2.f) - textBound.centerY();
    canvas.drawText(index, xOffset, yOffset, paint);
    return workingBitmap;
}
Also used : Bitmap(android.graphics.Bitmap) Rect(android.graphics.Rect) Canvas(android.graphics.Canvas) PorterDuffColorFilter(android.graphics.PorterDuffColorFilter) Paint(android.graphics.Paint) DisplayMetrics(android.util.DisplayMetrics) Paint(android.graphics.Paint)

Example 69 with Canvas

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

the class SurfaceHolderTarget method renderCanvas.

/**
     * Renders the given frame to the screen using a Canvas.
     * @param image the image to render
     */
private void renderCanvas(FrameImage2D image) {
    Canvas canvas = mSurfaceHolder.lockCanvas();
    Bitmap bitmap = image.toBitmap();
    Rect sourceRect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    Rect surfaceRect = mSurfaceHolder.getSurfaceFrame();
    RectF targetRect = getTargetRect(sourceRect, surfaceRect);
    canvas.drawColor(Color.BLACK);
    if (targetRect.width() > 0 && targetRect.height() > 0) {
        canvas.scale(surfaceRect.width(), surfaceRect.height());
        canvas.drawBitmap(bitmap, sourceRect, targetRect, new Paint());
    }
    mSurfaceHolder.unlockCanvasAndPost(canvas);
}
Also used : RectF(android.graphics.RectF) Bitmap(android.graphics.Bitmap) Rect(android.graphics.Rect) Canvas(android.graphics.Canvas) Paint(android.graphics.Paint)

Example 70 with Canvas

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

the class CropFilter method onProcess.

@Override
protected void onProcess() {
    OutputPort outPort = getConnectedOutputPort("image");
    // Pull input frame
    FrameImage2D inputImage = getConnectedInputPort("image").pullFrame().asFrameImage2D();
    int[] inDims = inputImage.getDimensions();
    int[] croppedDims = { (int) Math.ceil(mCropRect.xEdge().length() * inDims[0]), (int) Math.ceil(mCropRect.yEdge().length() * inDims[1]) };
    int[] outDims = { getOutputWidth(croppedDims[0], croppedDims[1]), getOutputHeight(croppedDims[0], croppedDims[1]) };
    FrameImage2D outputImage = outPort.fetchAvailableFrame(outDims).asFrameImage2D();
    if (isOpenGLSupported()) {
        FrameImage2D sourceFrame;
        Quad sourceQuad = null;
        boolean scaleDown = (outDims[0] < croppedDims[0]) || (outDims[1] < croppedDims[1]);
        if (scaleDown && mUseMipmaps) {
            mPow2Frame = TransformUtils.makeMipMappedFrame(mPow2Frame, croppedDims);
            int[] extDims = mPow2Frame.getDimensions();
            float targetWidth = croppedDims[0] / (float) extDims[0];
            float targetHeight = croppedDims[1] / (float) extDims[1];
            Quad targetQuad = Quad.fromRect(0f, 0f, targetWidth, targetHeight);
            mShader.setSourceQuad(mCropRect);
            mShader.setTargetQuad(targetQuad);
            mShader.process(inputImage, mPow2Frame);
            TransformUtils.generateMipMaps(mPow2Frame);
            sourceFrame = mPow2Frame;
            sourceQuad = targetQuad;
        } else {
            sourceFrame = inputImage;
            sourceQuad = mCropRect;
        }
        mShader.setSourceQuad(sourceQuad);
        mShader.setTargetRect(0f, 0f, 1f, 1f);
        mShader.process(sourceFrame, outputImage);
    } else {
        // Convert quads to canvas coordinate space
        Quad sourceQuad = mCropRect.scale2(inDims[0], inDims[1]);
        Quad targetQuad = Quad.fromRect(0f, 0f, inDims[0], inDims[1]);
        // Calculate transform for crop
        Matrix transform = Quad.getTransform(sourceQuad, targetQuad);
        transform.postScale(outDims[0] / (float) inDims[0], outDims[1] / (float) inDims[1]);
        // Create target canvas
        Bitmap.Config config = Bitmap.Config.ARGB_8888;
        Bitmap cropped = Bitmap.createBitmap(outDims[0], outDims[1], config);
        Canvas canvas = new Canvas(cropped);
        // Draw source bitmap into target canvas
        Paint paint = new Paint();
        paint.setFilterBitmap(true);
        Bitmap sourceBitmap = inputImage.toBitmap();
        canvas.drawBitmap(sourceBitmap, transform, paint);
        // Assign bitmap to output frame
        outputImage.setBitmap(cropped);
    }
    outPort.pushFrame(outputImage);
}
Also used : OutputPort(androidx.media.filterfw.OutputPort) Quad(androidx.media.filterfw.geometry.Quad) Bitmap(android.graphics.Bitmap) Matrix(android.graphics.Matrix) FrameImage2D(androidx.media.filterfw.FrameImage2D) Canvas(android.graphics.Canvas) 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