android.graphics.Bitmap.extractAlpha()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(8.8k)|赞(0)|评价(0)|浏览(258)

本文整理了Java中android.graphics.Bitmap.extractAlpha()方法的一些代码示例,展示了Bitmap.extractAlpha()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Bitmap.extractAlpha()方法的具体详情如下:
包路径:android.graphics.Bitmap
类名称:Bitmap
方法名:extractAlpha

Bitmap.extractAlpha介绍

暂无

代码示例

代码示例来源:origin: stackoverflow.com

// An added margin to the initial image
 int margin = 24;
 int halfMargin = margin / 2;
 // the glow radius
 int glowRadius = 16;
 // the glow color
 int glowColor = Color.rgb(0, 192, 255);
 // The original image to use
 Bitmap src = BitmapFactory.decodeResource(getResources(),
     R.drawable.ic_launcher);
 // extract the alpha from the source image
 Bitmap alpha = src.extractAlpha();
 // The output bitmap (with the icon + glow)
 Bitmap bmp = Bitmap.createBitmap(src.getWidth() + margin,
     src.getHeight() + margin, Bitmap.Config.ARGB_8888);
 // The canvas to paint on the image
 Canvas canvas = new Canvas(bmp);
 Paint paint = new Paint();
 paint.setColor(glowColor);
 // outer glow
 paint.setMaskFilter(new BlurMaskFilter(glowRadius, Blur.OUTER));
 canvas.drawBitmap(alpha, halfMargin, halfMargin, paint);
 // original icon
 canvas.drawBitmap(src, halfMargin, halfMargin, null);
 ((ImageView) findViewById(R.id.bmpImg)).setImageBitmap(bmp);

代码示例来源:origin: robolectric/robolectric

@Test
@SdkSuppress(minSdkVersion = M) // getAlpha() returns 0 on less than M
public void testExtractAlpha() {
 // normal case
 Bitmap bitmap =
   BitmapFactory.decodeResource(resources, R.drawable.an_image, new BitmapFactory.Options());
 Bitmap ret = bitmap.extractAlpha();
 int source = bitmap.getPixel(10, 20);
 int result = ret.getPixel(10, 20);
 assertThat(Color.alpha(result)).isEqualTo(Color.alpha(source));
}

代码示例来源:origin: stackoverflow.com

bmp = bmp.extractAlpha();

代码示例来源:origin: jinguangyue/Android-CustomCamera

public static Bitmap getAlphaBitmap(Bitmap mBitmap, int mColor) {
  //BitmapDrawable的getIntrinsicWidth()方法,Bitmap的getWidth()方法
  //注意这两个方法的区别
  //Bitmap mAlphaBitmap = Bitmap.createBitmap(mBitmapDrawable.getIntrinsicWidth(), mBitmapDrawable.getIntrinsicHeight(), Config.ARGB_8888);
  Bitmap mAlphaBitmap = Bitmap.createBitmap(mBitmap.getWidth(), mBitmap.getHeight(), Bitmap.Config.ARGB_8888);
  Canvas mCanvas = new Canvas(mAlphaBitmap);
  Paint mPaint = new Paint();
  mPaint.setColor(mColor);
  //从原位图中提取只包含alpha的位图
  Bitmap alphaBitmap = mBitmap.extractAlpha();
  //在画布上(mAlphaBitmap)绘制alpha位图
  mCanvas.drawBitmap(alphaBitmap, 0, 0, mPaint);
  return mAlphaBitmap;
}

代码示例来源:origin: jinguangyue/Android-CustomCamera

public static Bitmap getAlphaBitmap(Bitmap mBitmap, int mColor) {
  //BitmapDrawable的getIntrinsicWidth()方法,Bitmap的getWidth()方法
  //注意这两个方法的区别
  //Bitmap mAlphaBitmap = Bitmap.createBitmap(mBitmapDrawable.getIntrinsicWidth(), mBitmapDrawable.getIntrinsicHeight(), Config.ARGB_8888);
  Bitmap mAlphaBitmap = Bitmap.createBitmap(mBitmap.getWidth(), mBitmap.getHeight(), Config.ARGB_8888);
  Canvas mCanvas = new Canvas(mAlphaBitmap);
  Paint mPaint = new Paint();
  mPaint.setColor(mColor);
  //从原位图中提取只包含alpha的位图
  Bitmap alphaBitmap = mBitmap.extractAlpha();
  //在画布上(mAlphaBitmap)绘制alpha位图
  mCanvas.drawBitmap(alphaBitmap, 0, 0, mPaint);
  return mAlphaBitmap;
}

代码示例来源:origin: stackoverflow.com

private Bitmap createShadowBitmap(Bitmap originalBitmap) {
  BlurMaskFilter blurFilter = new BlurMaskFilter(5, BlurMaskFilter.Blur.OUTER);
  Paint shadowPaint = new Paint();
  shadowPaint.setMaskFilter(blurFilter);

  int[] offsetXY = new int[2];
  Bitmap shadowImage = originalBitmap.extractAlpha(shadowPaint, offsetXY);

  /* Need to convert shadowImage from 8-bit to ARGB here. */
  Bitmap shadowImage32 = shadowImage.copy(Bitmap.Config.ARGB_8888, true);
  Canvas c = new Canvas(shadowImage32);
  c.drawBitmap(originalBitmap, -offsetXY[0], -offsetXY[1], null);

  return shadowImage32;
}

代码示例来源:origin: stackoverflow.com

BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap source = BitmapFactory.decodeStream(pngStream, null, opts);
Bitmap mask = source.extractAlpha();
source.recycle();
// mask.getConfig() is now ALPHA_8

代码示例来源:origin: 121880399/QuickMvp

/**
 * 转为alpha位图
 *
 * @param src     源图片
 * @param recycle 是否回收
 * @return alpha位图
 */
public static Bitmap toAlpha(final Bitmap src, final Boolean recycle) {
  if (isEmptyBitmap(src)) return null;
  Bitmap ret = src.extractAlpha();
  if (recycle && !src.isRecycled()) src.recycle();
  return ret;
}

代码示例来源:origin: TomRoush/PdfBox-Android

private static Bitmap getAlphaImage(Bitmap image) throws IOException
{
  if (!image.hasAlpha())
  {
    return null;
  }
  return image.extractAlpha();
}

代码示例来源:origin: yaozs/YzsLib

/**
 * 转为alpha位图
 *
 * @param src     源图片
 * @param recycle 是否回收
 * @return alpha位图
 */
public static Bitmap toAlpha(Bitmap src, Boolean recycle) {
  if (isEmptyBitmap(src)) return null;
  Bitmap ret = src.extractAlpha();
  if (recycle && !src.isRecycled()) src.recycle();
  return ret;
}

代码示例来源:origin: 0xm1nam0/RxCore

/**
 * 转为alpha位图
 *
 * @param src     源图片
 * @param recycle 是否回收
 * @return alpha位图
 */
public static Bitmap toAlpha(Bitmap src, Boolean recycle) {
  if (isEmptyBitmap(src)) return null;
  Bitmap ret = src.extractAlpha();
  if (recycle && !src.isRecycled()) src.recycle();
  return ret;
}

代码示例来源:origin: lianghuiyong/AndroidBase

/**
 * 转为alpha位图
 *
 * @param src     源图片
 * @param recycle 是否回收
 * @return alpha位图
 */
public static Bitmap toAlpha(Bitmap src, Boolean recycle) {
  if (isEmptyBitmap(src)) return null;
  Bitmap ret = src.extractAlpha();
  if (recycle && !src.isRecycled()) src.recycle();
  return ret;
}

代码示例来源:origin: gumingwei/WellSwipe

static void drawSelectedAllAppsBitmap(Canvas dest, int destWidth, int destHeight,
                   boolean pressed, Bitmap src) {
  synchronized (sCanvas) { // we share the statics :-(
    if (sIconWidth == -1) {
      // We can't have gotten to here without src being initialized, which
      // comes from this file already.  So just assert.
      //initStatics(context);
      throw new RuntimeException("Assertion failed: Utilities not initialized");
    }
    dest.drawColor(0, PorterDuff.Mode.CLEAR);
    int[] xy = new int[2];
    Bitmap mask = src.extractAlpha(sBlurPaint, xy);
    float px = (destWidth - src.getWidth()) / 2;
    float py = (destHeight - src.getHeight()) / 2;
    dest.drawBitmap(mask, px + xy[0], py + xy[1],
        pressed ? sGlowColorPressedPaint : sGlowColorFocusedPaint);
    mask.recycle();
  }
}

代码示例来源:origin: hoangkien0705/Android-UtilCode

/**
 * 转为alpha位图
 *
 * @param src     源图片
 * @param recycle 是否回收
 * @return alpha位图
 */
public static Bitmap toAlpha(Bitmap src, Boolean recycle) {
  if (isEmptyBitmap(src)) return null;
  Bitmap ret = src.extractAlpha();
  if (recycle && !src.isRecycled()) src.recycle();
  return ret;
}

代码示例来源:origin: THEONE10211024/ApiDemos

public SampleView(Context context) {
  super(context);
  setFocusable(true);
  InputStream is = context.getResources().openRawResource(R.drawable.app_sample_code);
  mBitmap = BitmapFactory.decodeStream(is);
  mBitmap2 = mBitmap.extractAlpha();
  mBitmap3 = Bitmap.createBitmap(200, 200, Bitmap.Config.ALPHA_8);
  drawIntoBitmap(mBitmap3);
  mShader = new LinearGradient(0, 0, 100, 70, new int[] {
                 Color.RED, Color.GREEN, Color.BLUE },
                 null, Shader.TileMode.MIRROR);
}

代码示例来源:origin: qiubiteme/android_api_demos

public SampleView(Context context) {
  super(context);
  setFocusable(true);
  InputStream is = context.getResources().openRawResource(R.drawable.app_sample_code);
  mBitmap = BitmapFactory.decodeStream(is);
  mBitmap2 = mBitmap.extractAlpha();
  mBitmap3 = Bitmap.createBitmap(200, 200, Bitmap.Config.ALPHA_8);
  drawIntoBitmap(mBitmap3);
  mShader = new LinearGradient(0, 0, 100, 70, new int[] {
                 Color.RED, Color.GREEN, Color.BLUE },
                 null, Shader.TileMode.MIRROR);
}

代码示例来源:origin: enricocid/LaunchEnr

public synchronized Bitmap recreateIcon(Bitmap icon) {
  int[] offset = new int[2];
  Bitmap shadow = icon.extractAlpha(mBlurPaint, offset);
  Bitmap result = Bitmap.createBitmap(mIconSize, mIconSize, Config.ARGB_8888);
  mCanvas.setBitmap(result);
  // Draw ambient shadow
  mDrawPaint.setAlpha(AMBIENT_SHADOW_ALPHA);
  mCanvas.drawBitmap(shadow, offset[0], offset[1], mDrawPaint);
  // Draw key shadow
  mDrawPaint.setAlpha(KEY_SHADOW_ALPHA);
  mCanvas.drawBitmap(shadow, offset[0], offset[1] + KEY_SHADOW_DISTANCE * mIconSize, mDrawPaint);
  // Draw the icon
  mDrawPaint.setAlpha(255);
  mCanvas.drawBitmap(icon, 0, 0, mDrawPaint);
  mCanvas.setBitmap(null);
  return result;
}

代码示例来源:origin: klinker24/launcher3

public synchronized Bitmap recreateIcon(Bitmap icon) {
  int[] offset = new int[2];
  Bitmap shadow = icon.extractAlpha(mBlurPaint, offset);
  Bitmap result = Bitmap.createBitmap(mIconSize, mIconSize, Config.ARGB_8888);
  mCanvas.setBitmap(result);
  // Draw ambient shadow
  mDrawPaint.setAlpha(AMBIENT_SHADOW_ALPHA);
  mCanvas.drawBitmap(shadow, offset[0], offset[1], mDrawPaint);
  // Draw key shadow
  mDrawPaint.setAlpha(KEY_SHADOW_ALPHA);
  mCanvas.drawBitmap(shadow, offset[0], offset[1] + KEY_SHADOW_DISTANCE * mIconSize, mDrawPaint);
  // Draw the icon
  mDrawPaint.setAlpha(255);
  mCanvas.drawBitmap(icon, 0, 0, mDrawPaint);
  mCanvas.setBitmap(null);
  return result;
}

代码示例来源:origin: mcxtzhang/SwipeCaptcha

private void craeteMask() {
  mMaskBitmap = getMaskBitmap(((BitmapDrawable) getDrawable()).getBitmap(), mCaptchaPath);
  //滑块阴影
  mMaskShadowBitmap = mMaskBitmap.extractAlpha();
  //拖动的位移重置
  mDragerOffset = 0;
  //isDrawMask  绘制失败闪烁动画用
  isDrawMask = true;
}

代码示例来源:origin: stackoverflow.com

public class MainActivity extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ImageView view = ((ImageView) findViewById(R.id.img_outline));
    view.setImageBitmap(createOutline(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher)));
    view.setColorFilter(Color.RED);
  }

  private Bitmap createOutline(Bitmap src) {
    Paint p = new Paint();
    p.setMaskFilter(new BlurMaskFilter(10, Blur.OUTER));
    return src.extractAlpha(p, null);
  }
}

相关文章