Java Code Examples for android.graphics.Bitmap#setPremultiplied()

The following examples show how to use android.graphics.Bitmap#setPremultiplied() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ImageTools.java    From android-tv-launcher with MIT License 6 votes vote down vote up
/**
 *图片阴影
 * @param originalBitmap
 */
public static Drawable drawImageDropShadow(Bitmap originalBitmap, Context context) {

    BlurMaskFilter blurFilter = new BlurMaskFilter(3,BlurMaskFilter.Blur.NORMAL);
    Paint shadowPaint = new Paint();
    shadowPaint.setAlpha(80);
    shadowPaint.setColor(context.getResources().getColor(R.color.black));
    shadowPaint.setMaskFilter(blurFilter);
    int[] offsetXY = new int[2];
    Bitmap shadowBitmap = originalBitmap.extractAlpha(shadowPaint, offsetXY);
    Bitmap shadowImage32 = shadowBitmap.copy(Bitmap.Config.ARGB_8888, true);
    if ( !shadowImage32.isPremultiplied() )
    {
        shadowImage32.setPremultiplied( true );
    }
    Canvas c = new Canvas(shadowImage32);
    c.drawBitmap(originalBitmap, offsetXY[0], offsetXY[1], null);
    return new BitmapDrawable(shadowImage32);
}
 
Example 2
Source File: PlatformBitmapFactory.java    From fresco with MIT License 5 votes vote down vote up
/**
 * Set some property of the source bitmap to the destination bitmap
 *
 * @param source the source bitmap
 * @param destination the destination bitmap
 */
private static void setPropertyFromSourceBitmap(Bitmap source, Bitmap destination) {
  // The new bitmap was created from a known bitmap source so assume that
  // they use the same density
  destination.setDensity(source.getDensity());
  if (Build.VERSION.SDK_INT >= 12) {
    destination.setHasAlpha(source.hasAlpha());
  }

  if (Build.VERSION.SDK_INT >= 19) {
    destination.setPremultiplied(source.isPremultiplied());
  }
}
 
Example 3
Source File: LruBitmapPool.java    From GlideBitmapPool with Apache License 2.0 4 votes vote down vote up
@TargetApi(Build.VERSION_CODES.KITKAT)
private static void maybeSetPreMultiplied(Bitmap bitmap) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        bitmap.setPremultiplied(true);
    }
}
 
Example 4
Source File: TopAnchorFillWidthTransformation.java    From edx-app-android with Apache License 2.0 4 votes vote down vote up
@Override
protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform,
                           int outWidth, int outHeight) {
    final int width = toTransform.getWidth();
    final float widthRatio = outWidth / (float) width;

    /*
     Implementation Details:
     Following if-condition is an optimization to just match the aspect ratio of the View where
     the Bitmap has a smaller width, and not artificially scale up the Bitmap (as ImageView
     will automatically scale it up to match it's coordinates unless explicitly set not to do
     so). However, Glide uses a TransitionDrawable to transition from the placeholder to the
     actual image, and as we can't guarantee that the placeholder would also have a matching
     aspect ratio, this can cause the scaling to not be performed properly to fill the View.

     If we're always using a scaleType of fitXY or centerCrop in all our ImageView items, then
     we should not encounter any issues. However, if we might use fitCenter (the default),
     center, or centerInside scale types. we'll need to do following 2 things:
     1) Remove width and height manipulation (Done by removing the following if-section).
     2) Make the canvas scaling unconditional (Done by taking the line canvas.scale(....) out
     of the if-block).

     Since, we're using fitXY as our scaleType wherever we are applying this transformation, so
     the following condition gives us memory benefits.
     */
    if (outWidth > width) {
        outWidth = width;
        outHeight = Math.round(outHeight / widthRatio);
    }
    Bitmap newBitmap = Bitmap.createBitmap(
            outWidth, outHeight, toTransform.getConfig());
    newBitmap.setDensity(toTransform.getDensity());
    newBitmap.setPremultiplied(true);
    Canvas canvas = new Canvas(newBitmap);
    // It looks like Canvas has black color by default, and
    // there doesn't seem to be any simple way to set it as
    // transparent.
    canvas.drawColor(Color.WHITE);
    if (outWidth < width) {
        canvas.scale(widthRatio, widthRatio);
    }
    canvas.drawBitmap(toTransform, 0, 0, paint);
    return newBitmap;
}