Example usage for android.graphics Paint FILTER_BITMAP_FLAG

List of usage examples for android.graphics Paint FILTER_BITMAP_FLAG

Introduction

In this page you can find the example usage for android.graphics Paint FILTER_BITMAP_FLAG.

Prototype

int FILTER_BITMAP_FLAG

To view the source code for android.graphics Paint FILTER_BITMAP_FLAG.

Click Source Link

Document

Paint flag that enables bilinear sampling on scaled bitmaps.

Usage

From source file:Main.java

/**
 * Returns a Bitmap representing the thumbnail of the specified Bitmap. The
 * size of the thumbnail is defined by the dimension
 * android.R.dimen.launcher_application_icon_size.
 * <p>//from www. ja v  a2s .c  o m
 * This method is not thread-safe and should be invoked on the UI thread
 * only.
 *
 * @param bitmap  The bitmap to get a thumbnail of.
 * @param context The application's context.
 * @return A thumbnail for the specified bitmap or the bitmap itself if the
 * thumbnail could not be created.
 */
public static Bitmap createThumbnailBitmap(Bitmap bitmap, Context context) {
    int sIconWidth = -1;
    int sIconHeight = -1;
    final Resources resources = context.getResources();
    sIconWidth = sIconHeight = (int) resources.getDimension(android.R.dimen.app_icon_size);

    final Paint sPaint = new Paint();
    final Rect sBounds = new Rect();
    final Rect sOldBounds = new Rect();
    Canvas sCanvas = new Canvas();

    int width = sIconWidth;
    int height = sIconHeight;

    sCanvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG, Paint.FILTER_BITMAP_FLAG));

    final int bitmapWidth = bitmap.getWidth();
    final int bitmapHeight = bitmap.getHeight();

    if (width > 0 && height > 0) {
        if (width < bitmapWidth || height < bitmapHeight) {
            final float ratio = (float) bitmapWidth / bitmapHeight;

            if (bitmapWidth > bitmapHeight) {
                height = (int) (width / ratio);
            } else if (bitmapHeight > bitmapWidth) {
                width = (int) (height * ratio);
            }

            final Config c = (width == sIconWidth && height == sIconHeight) ? bitmap.getConfig()
                    : Config.ARGB_8888;
            final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c);
            final Canvas canvas = sCanvas;
            final Paint paint = sPaint;
            canvas.setBitmap(thumb);
            paint.setDither(false);
            paint.setFilterBitmap(true);
            sBounds.set((sIconWidth - width) / 2, (sIconHeight - height) / 2, width, height);
            sOldBounds.set(0, 0, bitmapWidth, bitmapHeight);
            canvas.drawBitmap(bitmap, sOldBounds, sBounds, paint);
            return thumb;
        } else if (bitmapWidth < width || bitmapHeight < height) {
            final Config c = Config.ARGB_8888;
            final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c);
            final Canvas canvas = sCanvas;
            final Paint paint = sPaint;
            canvas.setBitmap(thumb);
            paint.setDither(false);
            paint.setFilterBitmap(true);
            canvas.drawBitmap(bitmap, (sIconWidth - bitmapWidth) / 2, (sIconHeight - bitmapHeight) / 2, paint);
            return thumb;
        }
    }

    return bitmap;
}

From source file:Main.java

/**
 * This is only used when the launcher shortcut is created.
 * /*from   w  w  w . j  a v a2 s  .  c  o  m*/
 * @param bitmap The artist, album, genre, or playlist image that's going to
 *            be cropped.
 * @param size The new size.
 * @return A {@link Bitmap} that has been resized and cropped for a launcher
 *         shortcut.
 */
public static final Bitmap resizeAndCropCenter(final Bitmap bitmap, final int size) {
    Bitmap blurbitmap = null;

    final int w = bitmap.getWidth();
    final int h = bitmap.getHeight();
    if (w == size && h == size) {
        return bitmap;
    }

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
    byte[] bitmapdata = bos.toByteArray();
    ByteArrayInputStream bs = new ByteArrayInputStream(bitmapdata);

    try {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 8;
        options.inJustDecodeBounds = true;
        blurbitmap = BitmapFactory.decodeStream(bs, null, options);
        options.inJustDecodeBounds = false;

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (bs != null) {
            try {
                bs.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    final float mScale = (float) size / Math.min(w, h);

    final Bitmap mTarget = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
    final int mWidth = Math.round(mScale * blurbitmap.getWidth());
    final int mHeight = Math.round(mScale * blurbitmap.getHeight());
    final Canvas mCanvas = new Canvas(mTarget);
    mCanvas.translate((size - mWidth) / 2f, (size - mHeight) / 2f);
    mCanvas.scale(mScale, mScale);
    final Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG);
    mCanvas.drawBitmap(bitmap, 0, 0, paint);
    return mTarget;

}

From source file:om.sstvencoder.CropView.java

public CropView(Context context, AttributeSet attrs) {
    super(context, attrs);
    mDetectorCompat = new GestureDetectorCompat(getContext(), new GestureListener());
    mScaleDetector = new ScaleGestureDetector(getContext(), new ScaleGestureListener());

    mBitmapOptions = new BitmapFactory.Options();

    mPaint = new Paint(Paint.FILTER_BITMAP_FLAG);
    mRectPaint = new Paint();
    mRectPaint.setStyle(Paint.Style.STROKE);
    mBorderPaint = new Paint();
    mBorderPaint.setColor(Color.BLACK);

    mCanvasDrawRect = new Rect();
    mImageDrawRect = new Rect();
    mCacheRect = new Rect();

    mSmallImage = false;/*from  w w  w  . j a  v  a 2 s.  c  o  m*/
    mImageOK = false;

    mLabelHandler = new LabelHandler();
}

From source file:com.visenze.visearch.camerademo.views.RoundImageView.java

@Override
protected void onDraw(Canvas canvas) {

    if (mBounds == null) {
        return;//from   www.java  2 s. c  o  m
    }

    int width = mBounds.width();
    int height = mBounds.height();

    if (width == 0 || height == 0) {
        return;
    }

    canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG));

    if (!mCacheValid || width != mCachedWidth || height != mCachedHeight) {

        // Need to redraw the cache
        if (width == mCachedWidth && height == mCachedHeight) {
            // Have a correct-sized bitmap cache already allocated. Just erase it.
            mCacheBitmap.eraseColor(0);
        } else {
            // Allocate a new bitmap with the correct dimensions.
            mCacheBitmap.recycle();
            //noinspection AndroidLintDrawAllocation
            mCacheBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            mCachedWidth = width;
            mCachedHeight = height;
        }

        Canvas cacheCanvas = new Canvas(mCacheBitmap);
        cacheCanvas
                .setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG));
        if (mMaskDrawable != null) {
            int sc = cacheCanvas.save();
            mMaskDrawable.draw(cacheCanvas);
            mMaskedPaint.setColorFilter((mDesaturateOnPress && isPressed()) ? mDesaturateColorFilter : null);
            cacheCanvas.saveLayer(mBoundsF, mMaskedPaint,
                    Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.FULL_COLOR_LAYER_SAVE_FLAG);
            super.onDraw(cacheCanvas);
            cacheCanvas.restoreToCount(sc);
        } else if (mDesaturateOnPress && isPressed()) {
            int sc = cacheCanvas.save();
            cacheCanvas.drawRect(0, 0, mCachedWidth, mCachedHeight, mBlackPaint);
            mMaskedPaint.setColorFilter(mDesaturateColorFilter);
            cacheCanvas.saveLayer(mBoundsF, mMaskedPaint,
                    Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.FULL_COLOR_LAYER_SAVE_FLAG);
            super.onDraw(cacheCanvas);
            cacheCanvas.restoreToCount(sc);
        } else {
            super.onDraw(cacheCanvas);
        }

        if (mBorderDrawable != null) {
            mBorderDrawable.draw(cacheCanvas);
        }
    }

    // Draw from cache
    canvas.drawBitmap(mCacheBitmap, mBounds.left, mBounds.top, null);
}

From source file:com.android.launcher3.folder.FolderIcon.java

public static FolderIcon fromXml(int resId, Launcher launcher, ViewGroup group, FolderInfo folderInfo,
        IconCache iconCache) {//from  w  w  w  .  j av a 2 s.  c o m
    @SuppressWarnings("all") // suppress dead code warning
    final boolean error = INITIAL_ITEM_ANIMATION_DURATION >= DROP_IN_ANIMATION_DURATION;
    if (error) {
        throw new IllegalStateException("DROP_IN_ANIMATION_DURATION must be greater than "
                + "INITIAL_ITEM_ANIMATION_DURATION, as sequencing of adding first two items "
                + "is dependent on this");
    }

    DeviceProfile grid = launcher.getDeviceProfile();
    FolderIcon icon = (FolderIcon) LayoutInflater.from(launcher).inflate(resId, group, false);

    // For performance and compatibility reasons we render the preview using a software layer.
    // In particular, hardware path clipping has spotty ecosystem support and bad performance.
    // Software rendering also allows us to use shadow layers.
    icon.setLayerType(LAYER_TYPE_SOFTWARE, new Paint(Paint.FILTER_BITMAP_FLAG));

    icon.setClipToPadding(false);
    icon.mFolderName = (BubbleTextView) icon.findViewById(R.id.folder_icon_name);
    icon.mFolderName.setText(folderInfo.title);
    icon.mFolderName.setCompoundDrawablePadding(0);
    FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) icon.mFolderName.getLayoutParams();
    lp.topMargin = grid.iconSizePx + grid.iconDrawablePaddingPx;

    icon.setTag(folderInfo);
    icon.setOnClickListener(launcher);
    icon.mInfo = folderInfo;
    icon.mLauncher = launcher;
    icon.setContentDescription(launcher.getString(R.string.folder_name_format, folderInfo.title));
    Folder folder = Folder.fromXml(launcher);
    folder.setDragController(launcher.getDragController());
    folder.setFolderIcon(icon);
    folder.bind(folderInfo);
    icon.setFolder(folder);
    icon.setAccessibilityDelegate(launcher.getAccessibilityDelegate());

    folderInfo.addListener(icon);

    icon.setOnFocusChangeListener(launcher.mFocusHandler);
    return icon;
}

From source file:Main.java

/**
 * This is only used when the launcher shortcut is created.
 *
 * @param bitmap The artist, album, genre, or playlist image that's going to
 *               be cropped.//w  ww.  j a  v  a2 s .c o  m
 * @param size   The new size.
 * @return A {@link Bitmap} that has been resized and cropped for a launcher
 * shortcut.
 */
public static final Bitmap resizeAndCropCenter(final Bitmap bitmap, final int size) {
    final int w = bitmap.getWidth();
    final int h = bitmap.getHeight();
    if (w == size && h == size) {
        return bitmap;
    }

    final float mScale = (float) size / Math.min(w, h);

    final Bitmap mTarget = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
    final int mWidth = Math.round(mScale * bitmap.getWidth());
    final int mHeight = Math.round(mScale * bitmap.getHeight());
    final Canvas mCanvas = new Canvas(mTarget);
    mCanvas.translate((size - mWidth) / 2f, (size - mHeight) / 2f);
    mCanvas.scale(mScale, mScale);
    final Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG);
    mCanvas.drawBitmap(bitmap, 0, 0, paint);
    return mTarget;
}

From source file:com.abslyon.abetterselection.CoverFlow.CoverFlowView.java

private void init() {
    setWillNotDraw(false);/*w w  w.ja va 2  s.co m*/
    setClickable(true);

    mChildTransfromer = new Matrix();
    mReflectionTransfromer = new Matrix();

    mTouchRect = new RectF();

    mImageRecorder = new SparseArray<int[]>();

    mDrawChildPaint = new Paint();
    mDrawChildPaint.setAntiAlias(true);
    mDrawChildPaint.setFlags(Paint.ANTI_ALIAS_FLAG);

    mCoverFlowPadding = new Rect();

    mDrawFilter = new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);

    mScroller = new Scroller(getContext(), new AccelerateDecelerateInterpolator());

    mRemoveReflectionPendingArray = new ArrayList<Integer>();
}

From source file:com.dolphinwang.imagecoverflow.CoverFlowView.java

private void init() {
    setWillNotDraw(false);/*www . j  a v a 2 s. com*/
    setClickable(true);

    mChildTransfromer = new Matrix();
    mReflectionTransfromer = new Matrix();

    mTouchRect = new RectF();

    mTouchRect2 = new ArrayList<RectF>();
    for (int i = 0; i < 5; i++) {
        RectF a = new RectF();
        mTouchRect2.add(a);
    }

    mImageRecorder = new SparseArray<int[]>();

    mDrawChildPaint = new Paint();
    mDrawChildPaint.setAntiAlias(true);
    mDrawChildPaint.setFlags(Paint.ANTI_ALIAS_FLAG);

    mCoverFlowPadding = new Rect();

    mDrawFilter = new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);

    mScroller = new Scroller(getContext(), new AccelerateDecelerateInterpolator());

    mRemoveReflectionPendingArray = new ArrayList<Integer>();
}

From source file:tw.medfirst.com.project.baseview.CoverFlowView.java

private void init() {
    setWillNotDraw(false);/*  w ww .  j  av a 2 s .c o m*/
    setClickable(true);

    mChildTransfromer = new Matrix();
    mReflectionTransfromer = new Matrix();

    mTouchRect = new RectF();
    mTouchRect2 = new ArrayList<RectF>();
    for (int i = 0; i < VISIBLE_VIEWS * 2 + 1; i++) {
        RectF r = new RectF();
        mTouchRect2.add(r);
    }

    mImageRecorder = new SparseArray<int[]>();

    mDrawChildPaint = new Paint();
    mDrawChildPaint.setAntiAlias(true);
    mDrawChildPaint.setFlags(Paint.ANTI_ALIAS_FLAG);

    mCoverFlowPadding = new Rect();

    mDrawFilter = new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);

    mScroller = new Scroller(getContext(), new AccelerateDecelerateInterpolator());

    mRemoveReflectionPendingArray = new ArrayList<Integer>();
}

From source file:cn.edu.zafu.easemob.imagecoverflow.CoverFlowView.java

private void init() {
    setWillNotDraw(false);/*from  ww w .  j a va 2s .c  om*/
    setClickable(true);

    mChildTransformer = new Matrix();
    mReflectionTransformer = new Matrix();

    mTouchRect = new RectF();

    mImageRecorder = new SparseArray<int[]>();

    mDrawChildPaint = new Paint();
    mDrawChildPaint.setAntiAlias(true);
    mDrawChildPaint.setFlags(Paint.ANTI_ALIAS_FLAG);

    mCoverFlowPadding = new Rect();

    mDrawFilter = new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);

    mScroller = new Scroller(getContext(), new AccelerateDecelerateInterpolator());
}