android.graphics.PorterDuff.Mode Java Examples

The following examples show how to use android.graphics.PorterDuff.Mode. 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: BitmapUtils.java    From sealtalk-android with MIT License 6 votes vote down vote up
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
            bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);
    final float roundPx = 12;

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    if (null != bitmap) {
        bitmap.recycle();
        bitmap = null;
    }

    return output;
}
 
Example #2
Source File: RLImgUtil.java    From Roid-Library with Apache License 2.0 6 votes vote down vote up
/**
 * @param bitmap
 * @param roundPx
 * @return
 */
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();
    Bitmap output = Bitmap.createBitmap(w, h, Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, w, h);
    final RectF rectF = new RectF(rect);
    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    return output;
}
 
Example #3
Source File: ProfileDataCache.java    From AndroidChromium with Apache License 2.0 6 votes vote down vote up
private Bitmap getCroppedBitmap(Bitmap bitmap) {
    Bitmap output = Bitmap.createBitmap(
            bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(Color.WHITE);

    final float radius =  (bitmap.getWidth() - mImageStrokePx) / 2f;
    canvas.drawCircle(bitmap.getWidth() / 2f, bitmap.getHeight() / 2f, radius, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    paint.setColor(mImageStrokeColor);
    paint.setStyle(Paint.Style.STROKE);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC));
    paint.setStrokeWidth(mImageStrokePx);
    canvas.drawCircle(bitmap.getWidth() / 2f, bitmap.getHeight() / 2f, radius, paint);

    return output;
}
 
Example #4
Source File: SwitchButton.java    From FimiX8-RE with MIT License 6 votes vote down vote up
public void draw(Canvas canvas) {
    super.draw(canvas);
    this.paint.setStyle(Style.STROKE);
    this.paint.setAntiAlias(true);
    float padding = AbViewUtil.dip2px(getContext(), 0.5f);
    this.rect.set(padding, padding, ((float) getWidth()) - padding, ((float) getHeight()) - padding);
    this.paint.setColor(603979775);
    this.paint.setStrokeWidth(AbViewUtil.dip2px(getContext(), 0.7f));
    canvas.drawRoundRect(this.rect, this.radius, this.radius, this.paint);
    this.paint.setStyle(Style.FILL);
    this.paint.setXfermode(new PorterDuffXfermode(Mode.SRC_OVER));
    this.rect.set((this.spotX - 1.0f) - this.radius, this.centerY - this.radius, (this.spotX + 1.1f) + this.radius, this.centerY + this.radius);
    this.paint.setColor(0);
    canvas.drawRoundRect(this.rect, this.radius, this.radius, this.paint);
    float spotR = ((float) this.spotSize) * 0.45f;
    this.rect.set(this.spotX - spotR, this.centerY - spotR, this.spotX + spotR, this.centerY + spotR);
    this.paint.setColor(this.spotColor);
    canvas.drawRoundRect(this.rect, spotR, spotR, this.paint);
}
 
Example #5
Source File: ImageUtils.java    From FlyWoo with Apache License 2.0 6 votes vote down vote up
/**
 * 获取圆角图片
 *
 * @param bitmap
 * @param pixels
 * @return
 */
public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) {

    Bitmap output = Bitmap
            .createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);
    final float roundPx = pixels;

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    return output;
}
 
Example #6
Source File: MizLib.java    From Mizuu with Apache License 2.0 6 votes vote down vote up
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
            .getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);
    final float roundPx = pixels;

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    return output;
}
 
Example #7
Source File: FloatingActionButton.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
private void onApplySupportImageTint() {
  Drawable drawable = getDrawable();
  if (drawable == null) {
    return;
  }

  if (imageTint == null) {
    DrawableCompat.clearColorFilter(drawable);
    return;
  }

  int color = imageTint.getColorForState(getDrawableState(), Color.TRANSPARENT);
  Mode mode = imageMode;
  if (mode == null) {
    mode = Mode.SRC_IN;
  }

  drawable
      .mutate()
      .setColorFilter(AppCompatDrawableManager.getPorterDuffColorFilter(color, mode));
}
 
Example #8
Source File: UEImage.java    From Auie with GNU General Public License v2.0 6 votes vote down vote up
public UEImage transformRound(float radius){
	final int width = bitmap.getWidth();
	final int height = bitmap.getHeight();
	final int color = 0xff888888; 
	final Paint paint = new Paint();
	final Rect rect = new Rect(0, 0, width, height);
	final RectF rectF = new RectF(rect);
	if (radius == TRANSFORMROUND_CIRCLE) {
		radius = width > height ? width/2 : height/2;
	}
	Bitmap mBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
	Canvas canvas = new Canvas(mBitmap);
	paint.setAntiAlias(true);
	paint.setColor(color);
	canvas.drawARGB(0, 0, 0, 0);
	canvas.drawRoundRect(rectF, radius, radius, paint);
	paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
	canvas.drawBitmap(bitmap, rect, rect, paint);
	this.bitmap = mBitmap;
	return this;
}
 
Example #9
Source File: RLImgUtil.java    From Roid-Library with Apache License 2.0 6 votes vote down vote up
/**
 * @param bitmap
 * @return
 */
public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {
    final int reflectionGap = 4;
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();
    Matrix matrix = new Matrix();
    matrix.preScale(1, -1);
    Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, h / 2, w, h / 2, matrix, false);
    Bitmap bitmapWithReflection = Bitmap.createBitmap(w, (h + h / 2), Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmapWithReflection);
    canvas.drawBitmap(bitmap, 0, 0, null);
    Paint deafalutPaint = new Paint();
    canvas.drawRect(0, h, w, h + reflectionGap, deafalutPaint);
    canvas.drawBitmap(reflectionImage, 0, h + reflectionGap, null);
    Paint paint = new Paint();
    LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0, bitmapWithReflection.getHeight()
            + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
    paint.setShader(shader);
    // Set the Transfer mode to be porter duff and destination in
    paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
    // Draw a rectangle using the paint with our linear gradient
    canvas.drawRect(0, h, w, bitmapWithReflection.getHeight() + reflectionGap, paint);
    return bitmapWithReflection;
}
 
Example #10
Source File: LayoutRipple.java    From XERUNG with Apache License 2.0 6 votes vote down vote up
/**
 * @param bitmap
 * @return 设置涟漪的边界,涟漪在这个区域里面可见。这里可以设置四角的弧度数
 */
public Bitmap cropRoundRect(Bitmap bitmap) {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
    bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    RectF rectF = new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight());
    canvas.drawRoundRect(rectF, rippleBorderRadius, rippleBorderRadius, paint);

    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    return output;
}
 
Example #11
Source File: LayoutRipple.java    From XERUNG with Apache License 2.0 6 votes vote down vote up
/**
 * @param bitmap
 * @return 设置涟漪的边界,涟漪在这个区域里面可见。这里可以设置四角的弧度数
 */
public Bitmap cropRoundRect(Bitmap bitmap) {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
    bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    RectF rectF = new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight());
    canvas.drawRoundRect(rectF, rippleBorderRadius, rippleBorderRadius, paint);

    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    return output;
}
 
Example #12
Source File: XBitmapUtils.java    From XFrame with Apache License 2.0 6 votes vote down vote up
/**
 * 合并
 *
 * @param bgd 后景Bitmap
 * @param fg  前景Bitmap
 * @return 合成后Bitmap
 */
public static Bitmap combineImagesToSameSize(Bitmap bgd, Bitmap fg) {
    Bitmap bmp;

    int width = bgd.getWidth() < fg.getWidth() ? bgd.getWidth() : fg
            .getWidth();
    int height = bgd.getHeight() < fg.getHeight() ? bgd.getHeight() : fg
            .getHeight();

    if (fg.getWidth() != width && fg.getHeight() != height) {
        fg = zoom(fg, width, height);
    }
    if (bgd.getWidth() != width && bgd.getHeight() != height) {
        bgd = zoom(bgd, width, height);
    }

    bmp = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Paint paint = new Paint();
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_ATOP));

    Canvas canvas = new Canvas(bmp);
    canvas.drawBitmap(bgd, 0, 0, null);
    canvas.drawBitmap(fg, 0, 0, paint);

    return bmp;
}
 
Example #13
Source File: CommonUtility.java    From appcan-android with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static Bitmap GetRoundedCornerBitmap(Bitmap bitmap, float roundPx) {
    try {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(output);
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()));
        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(Color.BLACK);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        final Rect src = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        canvas.drawBitmap(bitmap, src, rect, paint);

        return output;
    } catch (Exception e) {
        return null;
    }
}
 
Example #14
Source File: Utils.java    From MaterialQQLite with Apache License 2.0 6 votes vote down vote up
public static Bitmap getRoundedCornerBitmap(Bitmap bmp, float roundPx){
	if (null == bmp || roundPx <= 0)
		return bmp;
	
	Bitmap output = Bitmap.createBitmap(bmp.getWidth(), 
			bmp.getHeight(), Config.ARGB_8888); 
	Canvas canvas = new Canvas(output);

	final int color = 0xff424242; 
	final Paint paint = new Paint(); 
	final Rect rect = new Rect(0, 0, bmp.getWidth(), bmp.getHeight()); 
	final RectF rectF = new RectF(rect);

	paint.setAntiAlias(true); 
	canvas.drawARGB(0, 0, 0, 0); 
	paint.setColor(color); 
	canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

	paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 
	canvas.drawBitmap(bmp, rect, rect, paint);

	return output; 
}
 
Example #15
Source File: X8MainTopReturnTimeView.java    From FimiX8-RE with MIT License 6 votes vote down vote up
public void drawPercent(Canvas canvas, int percent) {
    canvas.saveLayer(0.0f, 0.0f, (float) getWidth(), (float) getHeight(), null, 31);
    if (percent > 100) {
        percent = 100;
    }
    Path path = new Path();
    path.moveTo((float) ((getWidth() - ((getWidth() * (100 - percent)) / 100)) + this.SPACING), 0.0f);
    path.lineTo((float) (getWidth() - ((getWidth() * (100 - percent)) / 100)), (float) getHeight());
    path.lineTo((float) getWidth(), (float) getHeight());
    path.lineTo((float) getWidth(), 0.0f);
    path.close();
    if (percent > 50) {
        canvas.drawBitmap(this.mBpFull, 0.0f, 0.0f, null);
    } else {
        canvas.drawBitmap(this.mBpMiddle, 0.0f, 0.0f, null);
    }
    this.mPaint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawPath(path, this.mPaint);
    this.mPaint.setXfermode(null);
    canvas.restore();
}
 
Example #16
Source File: ButtonFloat.java    From meiShi with Apache License 2.0 6 votes vote down vote up
public Bitmap cropCircle(Bitmap bitmap) {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
            bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2,
            bitmap.getWidth()/2, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    return output;
}
 
Example #17
Source File: ImageManageUtil.java    From bither-bitmap-sample with Apache License 2.0 6 votes vote down vote up
public static final Bitmap getRoundCornerBitmap(Bitmap bitmap, float roundPx) {
    if (bitmap == null)
        return bitmap;
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
            bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
    canvas.drawARGB(0, 0, 0, 0);
    final int color = 0xff010101;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);
    paint.setAntiAlias(true);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    bitmap.recycle();
    return output;
}
 
Example #18
Source File: BitmapUtil.java    From Leanplum-Android-SDK with Apache License 2.0 6 votes vote down vote up
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
  if (bitmap == null) {
    return null;
  }

  Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
      Config.ARGB_8888);
  android.graphics.Canvas canvas = new android.graphics.Canvas(output);

  final int color = 0xff000000;
  final android.graphics.Paint paint = new android.graphics.Paint();
  final android.graphics.Rect rect = new android.graphics.Rect(0, 0,
      bitmap.getWidth(), bitmap.getHeight());
  final android.graphics.RectF rectF = new android.graphics.RectF(rect);

  paint.setAntiAlias(true);
  canvas.drawARGB(0, 0, 0, 0);
  paint.setColor(color);
  canvas.drawRoundRect(rectF, pixels, pixels, paint);

  paint.setXfermode(new android.graphics.PorterDuffXfermode(Mode.SRC_IN));
  canvas.drawBitmap(bitmap, rect, rect, paint);

  return output;
}
 
Example #19
Source File: ProfileDataCache.java    From 365browser with Apache License 2.0 6 votes vote down vote up
private Bitmap getCroppedBitmap(Bitmap bitmap) {
    Bitmap output = Bitmap.createBitmap(
            bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(Color.WHITE);

    final float radius =  (bitmap.getWidth() - mImageStrokePx) / 2f;
    canvas.drawCircle(bitmap.getWidth() / 2f, bitmap.getHeight() / 2f, radius, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    paint.setColor(mImageStrokeColor);
    paint.setStyle(Paint.Style.STROKE);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC));
    paint.setStrokeWidth(mImageStrokePx);
    canvas.drawCircle(bitmap.getWidth() / 2f, bitmap.getHeight() / 2f, radius, paint);

    return output;
}
 
Example #20
Source File: ImageUtils_Deprecated.java    From UltimateAndroid with Apache License 2.0 6 votes vote down vote up
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {

        if (null == bitmap) {
            return bitmap;
        }

        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
                .getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(rect);
        final float roundPx = 10;

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);

        return output;
    }
 
Example #21
Source File: ButtonFloat.java    From PhoneTutorial with Apache License 2.0 6 votes vote down vote up
public Bitmap cropCircle(Bitmap bitmap) {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
            bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2,
            bitmap.getWidth()/2, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    return output;
}
 
Example #22
Source File: Utils.java    From android-utils with MIT License 6 votes vote down vote up
public static Bitmap roundBitmap(Bitmap bmp, int radius) {
    Bitmap sbmp;
    if (bmp.getWidth() != radius || bmp.getHeight() != radius) {
        sbmp = Bitmap.createScaledBitmap(bmp, radius, radius, false);
    } else {
        sbmp = bmp;
    }

    Bitmap output = Bitmap.createBitmap(sbmp.getWidth(), sbmp.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, sbmp.getWidth(), sbmp.getHeight());

    paint.setAntiAlias(true);
    paint.setFilterBitmap(true);
    paint.setDither(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(Color.parseColor("#BAB399"));
    canvas.drawCircle(sbmp.getWidth() / 2 + 0.7f, sbmp.getHeight() / 2 + 0.7f, sbmp.getWidth() / 2 + 0.1f, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(sbmp, rect, rect, paint);

    return output;
}
 
Example #23
Source File: ButtonFloat.java    From MaterialDesignLibrary with Apache License 2.0 6 votes vote down vote up
public Bitmap cropCircle(Bitmap bitmap) {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
            bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2,
            bitmap.getWidth()/2, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    return output;
}
 
Example #24
Source File: ImageUtils.java    From monolog-android with MIT License 6 votes vote down vote up
/**
 * 获得圆角图片的方�?
 *
 * @param bitmap
 * @param roundPx �?般设�?14
 * @return
 */
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {

    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
            bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    return output;
}
 
Example #25
Source File: ImgUtil.java    From QiQuYing with Apache License 2.0 6 votes vote down vote up
/**
 * 获取圆角图片
   * @author zhr
   * @create_date 2015-3-10 下午3:53:41
   * @param bitmap
   * @param pixels
   * @return
 */
public static Bitmap toRoundCorner(Bitmap bitmap,int r) {
	//构建一个bitmap  
       Bitmap backgroundBmp = Bitmap.createBitmap(r,  
                r, Config.ARGB_8888);  
       //new一个Canvas,在backgroundBmp上画图  
       Canvas canvas = new Canvas(backgroundBmp);  
       Paint paint = new Paint();  
       //设置边缘光滑,去掉锯齿  
       paint.setAntiAlias(true);  
       //宽高相等,即正方形  
       RectF rect = new RectF(0, 0, r, r);  
       //通过制定的rect画一个圆角矩形,当圆角X轴方向的半径等于Y轴方向的半径时,
       //且都等于r/2时,画出来的圆角矩形就是圆形  
       canvas.drawRoundRect(rect, r/2, r/2, paint);  
       //设置当两个图形相交时的模式,SRC_IN为取SRC图形相交的部分,多余的将被去掉  
       paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));  
       //canvas将bitmap画在backgroundBmp上  
       canvas.drawBitmap(bitmap, null, rect, paint);  
       //返回已经绘画好的backgroundBmp  
       return backgroundBmp;  
   }
 
Example #26
Source File: Slider.java    From appinventor-extensions with Apache License 2.0 5 votes vote down vote up
private void setSliderColors() {
  if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
    seekbar.setProgressTintList(ColorStateList.valueOf(leftColor));
    if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP_MR1 ||
        !(seekbar.getProgressDrawable() instanceof StateListDrawable)) {
      seekbar.setProgressBackgroundTintList(ColorStateList.valueOf(rightColor));
      seekbar.setProgressBackgroundTintMode(Mode.MULTIPLY);
    } else {
      // Looking at the AOSP code, the previous calls should effectively accomplish what the
      // following code does... except it doesn't on Android 5.0. Instead, the result is that the
      // right side color is 50% opacity of leftColor. The following code works on Android 5.0,
      // but assumes a Drawable hierarchy that may not be true if the device manufacturer deviates
      // from the AOSP design. If that is the case, then the right hand side will not change.
      StateListDrawable drawable = (StateListDrawable) seekbar.getProgressDrawable();
      if (drawable.getCurrent() instanceof LayerDrawable) {
        LayerDrawable layerDrawable = (LayerDrawable) drawable.getCurrent();
        Drawable background = layerDrawable.findDrawableByLayerId(R.id.background);
        background.setTintList(ColorStateList.valueOf(rightColor));
        background.setTintMode(Mode.MULTIPLY);
      }
    }
  } else {
    LayerDrawable fullBar = (LayerDrawable) seekbar.getProgressDrawable();
    fullBar.setColorFilter(rightColor,PorterDuff.Mode.SRC);
    fullBar.findDrawableByLayerId(R.id.progress).setColorFilter(leftColor, PorterDuff.Mode.SRC);
  }
}
 
Example #27
Source File: PolygonOnSelectionToolGroup.java    From geopaparazzi with GNU General Public License v3.0 5 votes vote down vote up
public boolean onTouch(View v, MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN: {
            v.getBackground().setColorFilter(buttonSelectionColor, Mode.SRC_ATOP);
            v.invalidate();
            break;
        }
        case MotionEvent.ACTION_UP: {
            v.getBackground().clearColorFilter();
            v.invalidate();
            break;
        }
    }
    return false;
}
 
Example #28
Source File: PolygonMainEditingToolGroup.java    From geopaparazzi with GNU General Public License v3.0 5 votes vote down vote up
public boolean onTouch(View v, MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN: {
            v.getBackground().setColorFilter(selectionColor, Mode.SRC_ATOP);
            v.invalidate();
            break;
        }
        case MotionEvent.ACTION_UP: {
            v.getBackground().clearColorFilter();
            v.invalidate();
            break;
        }
    }
    return false;
}
 
Example #29
Source File: MainActivity.java    From ImageEraser with GNU General Public License v3.0 5 votes vote down vote up
public void UpdateLastEiditedBitmapForUndoLimit() {
    Canvas canvas = new Canvas(lastEditedBitmap);
    for (int i = 0; i < 1; i += 1) {
        int brushSize = brushSizes.get(i);
        Paint paint = new Paint();
        paint.setColor(0);
        paint.setStyle(Style.STROKE);
        paint.setAntiAlias(true);
        paint.setStrokeJoin(Join.ROUND);
        paint.setStrokeCap(Cap.ROUND);
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC));
        paint.setStrokeWidth((float) brushSize);
        canvas.drawPath(paths.get(i), paint);
    }
}
 
Example #30
Source File: DrawHelper.java    From letv with Apache License 2.0 5 votes vote down vote up
public static void clearCanvas(Canvas canvas) {
    if (!USE_DRAWCOLOR_TO_CLEAR_CANVAS) {
        RECT.set(0.0f, 0.0f, (float) canvas.getWidth(), (float) canvas.getHeight());
        clearCanvas(canvas, RECT);
    } else if (USE_DRAWCOLOR_MODE_CLEAR) {
        canvas.drawColor(0, Mode.CLEAR);
    } else {
        canvas.drawColor(0);
    }
}