Search in sources :

Example 36 with Matrix

use of android.graphics.Matrix in project android_frameworks_base by ResurrectionRemix.

the class OrientedBoundingBox method toPath.

/**
     * Currently used for debugging purpose only.
     *
     * @hide
     */
public Path toPath() {
    Path path = new Path();
    float[] point = new float[2];
    point[0] = -width / 2;
    point[1] = height / 2;
    Matrix matrix = new Matrix();
    matrix.setRotate(orientation);
    matrix.postTranslate(centerX, centerY);
    matrix.mapPoints(point);
    path.moveTo(point[0], point[1]);
    point[0] = -width / 2;
    point[1] = -height / 2;
    matrix.mapPoints(point);
    path.lineTo(point[0], point[1]);
    point[0] = width / 2;
    point[1] = -height / 2;
    matrix.mapPoints(point);
    path.lineTo(point[0], point[1]);
    point[0] = width / 2;
    point[1] = height / 2;
    matrix.mapPoints(point);
    path.lineTo(point[0], point[1]);
    path.close();
    return path;
}
Also used : Path(android.graphics.Path) Matrix(android.graphics.Matrix)

Example 37 with Matrix

use of android.graphics.Matrix in project android_frameworks_base by ResurrectionRemix.

the class WallpaperCropActivity method cropImageAndSetWallpaper.

protected void cropImageAndSetWallpaper(Uri uri, OnBitmapCroppedHandler onBitmapCroppedHandler, final boolean finishActivityWhenDone) {
    boolean centerCrop = getResources().getBoolean(R.bool.center_crop);
    // Get the crop
    boolean ltr = mCropView.getLayoutDirection() == View.LAYOUT_DIRECTION_LTR;
    Display d = getWindowManager().getDefaultDisplay();
    Point displaySize = new Point();
    d.getSize(displaySize);
    boolean isPortrait = displaySize.x < displaySize.y;
    Point defaultWallpaperSize = getDefaultWallpaperSize(getResources(), getWindowManager());
    // Get the crop
    RectF cropRect = mCropView.getCrop();
    Point inSize = mCropView.getSourceDimensions();
    int cropRotation = mCropView.getImageRotation();
    float cropScale = mCropView.getWidth() / (float) cropRect.width();
    Matrix rotateMatrix = new Matrix();
    rotateMatrix.setRotate(cropRotation);
    float[] rotatedInSize = new float[] { inSize.x, inSize.y };
    rotateMatrix.mapPoints(rotatedInSize);
    rotatedInSize[0] = Math.abs(rotatedInSize[0]);
    rotatedInSize[1] = Math.abs(rotatedInSize[1]);
    // Due to rounding errors in the cropview renderer the edges can be slightly offset
    // therefore we ensure that the boundaries are sanely defined
    cropRect.left = Math.max(0, cropRect.left);
    cropRect.right = Math.min(rotatedInSize[0], cropRect.right);
    cropRect.top = Math.max(0, cropRect.top);
    cropRect.bottom = Math.min(rotatedInSize[1], cropRect.bottom);
    // ADJUST CROP WIDTH
    // Extend the crop all the way to the right, for parallax
    // (or all the way to the left, in RTL)
    float extraSpace;
    if (centerCrop) {
        extraSpace = 2f * Math.min(rotatedInSize[0] - cropRect.right, cropRect.left);
    } else {
        extraSpace = ltr ? rotatedInSize[0] - cropRect.right : cropRect.left;
    }
    // Cap the amount of extra width
    float maxExtraSpace = defaultWallpaperSize.x / cropScale - cropRect.width();
    extraSpace = Math.min(extraSpace, maxExtraSpace);
    if (centerCrop) {
        cropRect.left -= extraSpace / 2f;
        cropRect.right += extraSpace / 2f;
    } else {
        if (ltr) {
            cropRect.right += extraSpace;
        } else {
            cropRect.left -= extraSpace;
        }
    }
    // ADJUST CROP HEIGHT
    if (isPortrait) {
        cropRect.bottom = cropRect.top + defaultWallpaperSize.y / cropScale;
    } else {
        // LANDSCAPE
        float extraPortraitHeight = defaultWallpaperSize.y / cropScale - cropRect.height();
        float expandHeight = Math.min(Math.min(rotatedInSize[1] - cropRect.bottom, cropRect.top), extraPortraitHeight / 2);
        cropRect.top -= expandHeight;
        cropRect.bottom += expandHeight;
    }
    final int outWidth = (int) Math.round(cropRect.width() * cropScale);
    final int outHeight = (int) Math.round(cropRect.height() * cropScale);
    Runnable onEndCrop = new Runnable() {

        public void run() {
            if (finishActivityWhenDone) {
                setResult(Activity.RESULT_OK);
                finish();
            }
        }
    };
    BitmapCropTask cropTask = new BitmapCropTask(this, uri, cropRect, cropRotation, outWidth, outHeight, true, false, onEndCrop);
    if (onBitmapCroppedHandler != null) {
        cropTask.setOnBitmapCropped(onBitmapCroppedHandler);
    }
    cropTask.execute();
}
Also used : RectF(android.graphics.RectF) Matrix(android.graphics.Matrix) Point(android.graphics.Point) Point(android.graphics.Point) Paint(android.graphics.Paint) Display(android.view.Display)

Example 38 with Matrix

use of android.graphics.Matrix in project android_frameworks_base by ResurrectionRemix.

the class DocumentsContract method getDocumentThumbnail.

/** {@hide} */
public static Bitmap getDocumentThumbnail(ContentProviderClient client, Uri documentUri, Point size, CancellationSignal signal) throws RemoteException, IOException {
    final Bundle openOpts = new Bundle();
    openOpts.putParcelable(ContentResolver.EXTRA_SIZE, size);
    AssetFileDescriptor afd = null;
    Bitmap bitmap = null;
    try {
        afd = client.openTypedAssetFileDescriptor(documentUri, "image/*", openOpts, signal);
        final FileDescriptor fd = afd.getFileDescriptor();
        final long offset = afd.getStartOffset();
        // Try seeking on the returned FD, since it gives us the most
        // optimal decode path; otherwise fall back to buffering.
        BufferedInputStream is = null;
        try {
            Os.lseek(fd, offset, SEEK_SET);
        } catch (ErrnoException e) {
            is = new BufferedInputStream(new FileInputStream(fd), THUMBNAIL_BUFFER_SIZE);
            is.mark(THUMBNAIL_BUFFER_SIZE);
        }
        // We requested a rough thumbnail size, but the remote size may have
        // returned something giant, so defensively scale down as needed.
        final BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = true;
        if (is != null) {
            BitmapFactory.decodeStream(is, null, opts);
        } else {
            BitmapFactory.decodeFileDescriptor(fd, null, opts);
        }
        final int widthSample = opts.outWidth / size.x;
        final int heightSample = opts.outHeight / size.y;
        opts.inJustDecodeBounds = false;
        opts.inSampleSize = Math.min(widthSample, heightSample);
        if (is != null) {
            is.reset();
            bitmap = BitmapFactory.decodeStream(is, null, opts);
        } else {
            try {
                Os.lseek(fd, offset, SEEK_SET);
            } catch (ErrnoException e) {
                e.rethrowAsIOException();
            }
            bitmap = BitmapFactory.decodeFileDescriptor(fd, null, opts);
        }
        // Transform the bitmap if requested. We use a side-channel to
        // communicate the orientation, since EXIF thumbnails don't contain
        // the rotation flags of the original image.
        final Bundle extras = afd.getExtras();
        final int orientation = (extras != null) ? extras.getInt(EXTRA_ORIENTATION, 0) : 0;
        if (orientation != 0) {
            final int width = bitmap.getWidth();
            final int height = bitmap.getHeight();
            final Matrix m = new Matrix();
            m.setRotate(orientation, width / 2, height / 2);
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, m, false);
        }
    } finally {
        IoUtils.closeQuietly(afd);
    }
    return bitmap;
}
Also used : AssetFileDescriptor(android.content.res.AssetFileDescriptor) Bundle(android.os.Bundle) AssetFileDescriptor(android.content.res.AssetFileDescriptor) ParcelFileDescriptor(android.os.ParcelFileDescriptor) FileDescriptor(java.io.FileDescriptor) FileInputStream(java.io.FileInputStream) Point(android.graphics.Point) Bitmap(android.graphics.Bitmap) ErrnoException(android.system.ErrnoException) Matrix(android.graphics.Matrix) BufferedInputStream(java.io.BufferedInputStream) BitmapFactory(android.graphics.BitmapFactory)

Example 39 with Matrix

use of android.graphics.Matrix in project android_frameworks_base by ResurrectionRemix.

the class Surface method setCompatibilityTranslator.

/**
     * Sets the translator used to scale canvas's width/height in compatibility
     * mode.
     */
void setCompatibilityTranslator(Translator translator) {
    if (translator != null) {
        float appScale = translator.applicationScale;
        mCompatibleMatrix = new Matrix();
        mCompatibleMatrix.setScale(appScale, appScale);
    }
}
Also used : Matrix(android.graphics.Matrix)

Example 40 with Matrix

use of android.graphics.Matrix in project android_frameworks_base by ResurrectionRemix.

the class RippleDrawable method updateMaskShaderIfNeeded.

/**
     * @return whether we need to use a mask
     */
private void updateMaskShaderIfNeeded() {
    if (mHasValidMask) {
        return;
    }
    final int maskType = getMaskType();
    if (maskType == MASK_UNKNOWN) {
        return;
    }
    mHasValidMask = true;
    final Rect bounds = getBounds();
    if (maskType == MASK_NONE || bounds.isEmpty()) {
        if (mMaskBuffer != null) {
            mMaskBuffer.recycle();
            mMaskBuffer = null;
            mMaskShader = null;
            mMaskCanvas = null;
        }
        mMaskMatrix = null;
        mMaskColorFilter = null;
        return;
    }
    // Ensure we have a correctly-sized buffer.
    if (mMaskBuffer == null || mMaskBuffer.getWidth() != bounds.width() || mMaskBuffer.getHeight() != bounds.height()) {
        if (mMaskBuffer != null) {
            mMaskBuffer.recycle();
        }
        mMaskBuffer = Bitmap.createBitmap(bounds.width(), bounds.height(), Bitmap.Config.ALPHA_8);
        mMaskShader = new BitmapShader(mMaskBuffer, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        mMaskCanvas = new Canvas(mMaskBuffer);
    } else {
        mMaskBuffer.eraseColor(Color.TRANSPARENT);
    }
    if (mMaskMatrix == null) {
        mMaskMatrix = new Matrix();
    } else {
        mMaskMatrix.reset();
    }
    if (mMaskColorFilter == null) {
        mMaskColorFilter = new PorterDuffColorFilter(0, PorterDuff.Mode.SRC_IN);
    }
    // Draw the appropriate mask anchored to (0,0).
    final int left = bounds.left;
    final int top = bounds.top;
    mMaskCanvas.translate(-left, -top);
    if (maskType == MASK_EXPLICIT) {
        drawMask(mMaskCanvas);
    } else if (maskType == MASK_CONTENT) {
        drawContent(mMaskCanvas);
    }
    mMaskCanvas.translate(left, top);
}
Also used : Rect(android.graphics.Rect) Matrix(android.graphics.Matrix) Canvas(android.graphics.Canvas) PorterDuffColorFilter(android.graphics.PorterDuffColorFilter) BitmapShader(android.graphics.BitmapShader) Paint(android.graphics.Paint)

Aggregations

Matrix (android.graphics.Matrix)1590 Bitmap (android.graphics.Bitmap)487 Paint (android.graphics.Paint)344 RectF (android.graphics.RectF)276 Test (org.junit.Test)154 Canvas (android.graphics.Canvas)139 Rect (android.graphics.Rect)127 ColorMatrix (android.graphics.ColorMatrix)87 Point (android.graphics.Point)79 View (android.view.View)78 Path (android.graphics.Path)74 ImageView (android.widget.ImageView)73 ShadowBitmap (org.robolectric.shadows.ShadowBitmap)67 ShadowMatrix (org.robolectric.shadows.ShadowMatrix)67 IOException (java.io.IOException)61 Drawable (android.graphics.drawable.Drawable)48 BitmapFactory (android.graphics.BitmapFactory)47 Bundle (android.os.Bundle)46 ViewGroup (android.view.ViewGroup)44 PointF (android.graphics.PointF)42