Java Code Examples for android.graphics.Canvas#drawARGB()

The following examples show how to use android.graphics.Canvas#drawARGB() . 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: Button.java    From MaterialDesignLibrary with Apache License 2.0 6 votes vote down vote up
public Bitmap makeCircle() {
	Bitmap output = Bitmap.createBitmap(
			getWidth() - Utils.dpToPx(6, getResources()), getHeight()
					- Utils.dpToPx(7, getResources()), Config.ARGB_8888);
	Canvas canvas = new Canvas(output);
	canvas.drawARGB(0, 0, 0, 0);
	Paint paint = new Paint();
	paint.setAntiAlias(true);
	paint.setColor(rippleColor);
	canvas.drawCircle(x, y, radius, paint);
	if (radius > getHeight() / rippleSize)
		radius += rippleSpeed;
	if (radius >= getWidth()) {
		x = -1;
		y = -1;
		radius = getHeight() / rippleSize;
		if (onClickListener != null && clickAfterRipple)
			onClickListener.onClick(this);
	}
	return output;
}
 
Example 2
Source File: ImageUtils.java    From AndroidCacheFoundation 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));  //Mode.SRC_IN 用前面画的“圆角矩形”对bitmap进行裁剪。
    canvas.drawBitmap(bitmap, rect, rect, paint);

    return output;
}
 
Example 3
Source File: ImageHelper.java    From fanfouapp-opensource with Apache License 2.0 6 votes vote down vote up
/**
 * 圆角图片
 * 
 * @param bitmap
 * @param pixels
 * @return
 */
public static Bitmap getRoundedCornerBitmap(final Bitmap bitmap,
        final int pixels) {
    final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
            bitmap.getHeight(), Config.ARGB_8888);
    final 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 4
Source File: CircularImageView.java    From mimi-reader with Apache License 2.0 6 votes vote down vote up
public Bitmap getCircleImage(final Bitmap bitmap) {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
            bitmap.getHeight(), Bitmap.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 int radius;
    if (bitmap.getHeight() > bitmap.getWidth()) {
        radius = bitmap.getWidth() / 2;
    } else {
        radius = bitmap.getHeight() / 2;
    }

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    // paint.setColor(color);
    canvas.drawCircle(bitmap.getWidth() / 2,
            bitmap.getHeight() / 2, radius, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    return output;
}
 
Example 5
Source File: BmpUtils.java    From LLApp with Apache License 2.0 6 votes vote down vote up
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: QuickControlsFragment.java    From Muzesto with GNU General Public License v3.0 6 votes vote down vote up
public Bitmap getCroppedBitmap(Bitmap bitmap) {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
            bitmap.getHeight(), Bitmap.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.drawRoundRect(rectF, roundPx, roundPx, paint);
    canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2,
            bitmap.getWidth() / 2, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    //Bitmap _bmp = Bitmap.createScaledBitmap(output, 60, 60, false);
    //return _bmp;
    return output;
}
 
Example 7
Source File: BitmapUtils.java    From SprintNBA with Apache License 2.0 6 votes vote down vote up
/**
 * 获取圆形图片
 *
 * @param bitmap
 * @return
 */
public static Bitmap toRoundCorner(Bitmap bitmap) {
    int height = bitmap.getHeight();
    int width = bitmap.getHeight();
    Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(output);
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, width, height);
    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    //paint.setColor(0xff424242);
    paint.setColor(Color.TRANSPARENT);
    canvas.drawCircle(width / 2, height / 2, width / 2, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    return output;
}
 
Example 8
Source File: RandomPointView.java    From bither-android with Apache License 2.0 6 votes vote down vote up
@Override
public void draw(Canvas canvas) {
    removeCallbacks(redraw);
    canvas.drawARGB(0, 0, 0, 0);
    if (hasDots) {
        hasDots = false;
        postDelayed(redraw, DotsAppearInterval);
    } else {
        hasDots = true;
        int width = getWidth();
        int height = getHeight();
        float[] points = new float[PointCount * 2];
        for (int i = 0;
             i < PointCount;
             i++) {
            points[i * 2] = random.nextFloat() * width;
            points[i * 2 + 1] = random.nextFloat() * height;
        }
        canvas.drawPoints(points, paint);
        postDelayed(redraw, DotsRemainTime);
    }
    super.draw(canvas);
}
 
Example 9
Source File: CameraAppUI.java    From Camera2 with Apache License 2.0 6 votes vote down vote up
@Override
public Bitmap getScreenShot(int previewDownSampleFactor)
{
    Bitmap screenshot = Bitmap.createBitmap(mCameraRootView.getWidth(),
            mCameraRootView.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(screenshot);
    canvas.drawARGB(255, 0, 0, 0);
    Bitmap preview = mTextureViewHelper.getPreviewBitmap(previewDownSampleFactor);
    if (preview != null)
    {
        canvas.drawBitmap(preview, null, mTextureViewHelper.getPreviewArea(), null);
    }
    Bitmap overlay = getPreviewOverlayAndControls();
    if (overlay != null)
    {
        canvas.drawBitmap(overlay, 0f, 0f, null);
    }
    return screenshot;
}
 
Example 10
Source File: ImageHelper.java    From Doctorave with MIT License 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 11
Source File: PlayBgShape.java    From LitePlayer with Apache License 2.0 5 votes vote down vote up
@Override
	public void draw(Canvas canvas, Paint paint) {
//		Matrix matrix = new Matrix();
//		matrix.postScale(App.sScreenWidth / mBitmap.getWidth(), App.sScreenHeight / mBitmap.getHeight());
		canvas.drawBitmap(mBitmap, new Matrix(), paint);
		canvas.drawARGB(150, 128, 128, 128);
	}
 
Example 12
Source File: CircledImageView.java    From ExpandablePanel with Apache License 2.0 5 votes vote down vote up
private static Bitmap getCroppedBitmap(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(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int color = 0xffa19774;
        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);

        canvas.drawCircle(sbmp.getWidth() / 2+0.7f, sbmp.getHeight() / 2+0.7f,
                sbmp.getWidth() / 2+0.1f, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(sbmp, rect, rect, paint);

        // draw internal border
        paint.setColor(Color.parseColor("#ffffff"));
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth((float) 30);
        canvas.drawCircle(sbmp.getWidth()/ 2 + 0.7f, sbmp.getHeight() / 2 + 0.7f, sbmp.getWidth() / 2+0.1f, paint);

        return output;
    }
 
Example 13
Source File: Falcon.java    From Falcon with Apache License 2.0 5 votes vote down vote up
private static void drawRootToBitmap(ViewRootData config, Bitmap bitmap) {
  // now only dim supported
  if ((config._layoutParams.flags & FLAG_DIM_BEHIND) == FLAG_DIM_BEHIND) {
    Canvas dimCanvas = new Canvas(bitmap);

    int alpha = (int) (255 * config._layoutParams.dimAmount);
    dimCanvas.drawARGB(alpha, 0, 0, 0);
  }

  Canvas canvas = new Canvas(bitmap);
  canvas.translate(config._winFrame.left, config._winFrame.top);
  config._view.draw(canvas);
}
 
Example 14
Source File: FingerDraw.java    From codeexamples-android with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void draw(Canvas canvas) {
    super.draw(canvas);
    if (isInEditMode()) {
        canvas.drawARGB(120, 255, 0, 0);
    }
    canvas.drawCircle(100, 100, 50, paint);
    canvas.drawPath(path, paint);
}
 
Example 15
Source File: ImageHandler.java    From vocefiscal-android with Apache License 2.0 5 votes vote down vote up
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) 
{
	if(bitmap!=null)
	{
		int w = bitmap.getWidth();                                          
		int h = bitmap.getHeight();                                         

		int radius = Math.min(h / 2, w / 2);                                
		Bitmap output = Bitmap.createBitmap(w + 8, h + 8, Config.ARGB_8888);

		Paint p = new Paint();                                              
		p.setAntiAlias(true);                                               

		Canvas c = new Canvas(output);                                      
		c.drawARGB(0, 0, 0, 0);                                             
		p.setStyle(Style.FILL); 
		p.setColor(Color.WHITE);

		c.drawCircle((w / 2) + 4, (h / 2) + 4, radius, p);                  

		p.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));                 

		c.drawBitmap(bitmap, 4, 4, p);                                                     

		return output; 
	}else
	{
		return null;
	}

}
 
Example 16
Source File: AvatarService.java    From Conversations with GNU General Public License v3.0 5 votes vote down vote up
private void drawAvatar(Bitmap bitmap, Canvas canvas, Paint paint) {
	final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
	paint.setAntiAlias(true);
	canvas.drawARGB(0, 0, 0, 0);
	canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2, bitmap.getWidth() / 2, paint);
	paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
	canvas.drawBitmap(bitmap, rect, rect, paint);
}
 
Example 17
Source File: RoundedCornerTransformation.java    From narrate-android with Apache License 2.0 5 votes vote down vote up
@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int width, int height) {

    Bitmap transformed = super.transform(pool, toTransform, width, height);

    Bitmap output = null; //pool.get(width, height, toTransform.getConfig() != null ? toTransform.getConfig() : Bitmap.Config.ARGB_8888);

    if ( output == null )
            output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(output);

    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, width, height);
    final RectF rectF = new RectF(rect);

    paint.setAntiAlias(true);
    paint.setColor(0xFFFFFFFF);
    paint.setStyle(Paint.Style.FILL);
    canvas.drawARGB(0, 0, 0, 0);
    canvas.drawRoundRect(rectF, cornerSize, cornerSize, paint);

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

    transformed.recycle();

    return output;
}
 
Example 18
Source File: PopupZoomer.java    From 365browser with Apache License 2.0 4 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
    if (!isShowing() || mZoomedBitmap == null) return;
    if (!acceptZeroSizeView() && (getWidth() == 0 || getHeight() == 0)) return;

    if (mNeedsToInitDimensions) {
        mNeedsToInitDimensions = false;
        initDimensions();
    }

    canvas.save();
    // Calculate the elapsed fraction of animation.
    float time = (SystemClock.uptimeMillis() - mAnimationStartTime + mTimeLeft)
            / ((float) ANIMATION_DURATION);
    time = constrain(time, 0, 1);
    if (time >= 1) {
        mAnimating = false;
        if (!isShowing()) {
            hideImmediately();
            return;
        }
    } else {
        invalidate();
    }

    // Fraction of the animation to actally show.
    float fractionAnimation;
    if (mShowing) {
        fractionAnimation = mShowInterpolator.getInterpolation(time);
    } else {
        fractionAnimation = mHideInterpolator.getInterpolation(time);
    }

    // Draw a faded color over the entire view to fade out the original content, increasing
    // the alpha value as fractionAnimation increases.
    // TODO(nileshagrawal): We should use time here instead of fractionAnimation
    // as fractionAnimaton is interpolated and can go over 1.
    canvas.drawARGB((int) (80 * fractionAnimation), 0, 0, 0);
    canvas.save();

    // Since we want the content to appear directly above its counterpart we need to make
    // sure that it starts out at exactly the same size as it appears in the page,
    // i.e. scale grows from 1/mScale to 1. Note that extrusion values are already zoomed
    // with mScale.
    float scale = fractionAnimation * (mScale - 1.0f) / mScale + 1.0f / mScale;

    // Since we want the content to appear directly above its counterpart on the
    // page, we need to remove the mShiftX/Y effect at the beginning of the animation.
    // The unshifting decreases with the animation.
    float unshiftX = -mShiftX * (1.0f - fractionAnimation) / mScale;
    float unshiftY = -mShiftY * (1.0f - fractionAnimation) / mScale;

    // Compute the |mDrawRect| to show.
    mDrawRect.left = mTouch.x - mLeftExtrusion * scale + unshiftX;
    mDrawRect.top = mTouch.y - mTopExtrusion * scale + unshiftY;
    mDrawRect.right = mTouch.x + mRightExtrusion * scale + unshiftX;
    mDrawRect.bottom = mTouch.y + mBottomExtrusion * scale + unshiftY;
    canvas.clipRect(mDrawRect);

    // Since the canvas transform APIs all pre-concat the transformations, this is done in
    // reverse order. The canvas is first scaled up, then shifted the appropriate amount of
    // pixels.
    canvas.scale(scale, scale, mDrawRect.left, mDrawRect.top);
    canvas.translate(mPopupScrollX, mPopupScrollY);
    canvas.drawBitmap(mZoomedBitmap, mDrawRect.left, mDrawRect.top, null);
    canvas.restore();
    Drawable overlayNineTile = getOverlayDrawable(getContext());
    overlayNineTile.setBounds((int) mDrawRect.left - sOverlayPadding.left,
            (int) mDrawRect.top - sOverlayPadding.top,
            (int) mDrawRect.right + sOverlayPadding.right,
            (int) mDrawRect.bottom + sOverlayPadding.bottom);
    // TODO(nileshagrawal): We should use time here instead of fractionAnimation
    // as fractionAnimaton is interpolated and can go over 1.
    int alpha = constrain((int) (fractionAnimation * 255), 0, 255);
    overlayNineTile.setAlpha(alpha);
    overlayNineTile.draw(canvas);
    canvas.restore();
}
 
Example 19
Source File: PhotoUtil.java    From Conquer with Apache License 2.0 4 votes vote down vote up
/**
 * 将图片转化为圆形头像
 * 
 * @Title: toRoundBitmap
 * @throws
 */
public static Bitmap toRoundBitmap(Bitmap bitmap) {
	int width = bitmap.getWidth();
	int height = bitmap.getHeight();
	float roundPx;
	float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;
	if (width <= height) {
		roundPx = width / 2;

		left = 0;
		top = 0;
		right = width;
		bottom = width;

		height = width;

		dst_left = 0;
		dst_top = 0;
		dst_right = width;
		dst_bottom = width;
	} else {
		roundPx = height / 2;

		float clip = (width - height) / 2;

		left = clip;
		right = width - clip;
		top = 0;
		bottom = height;
		width = height;

		dst_left = 0;
		dst_top = 0;
		dst_right = height;
		dst_bottom = height;
	}

	Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888);
	Canvas canvas = new Canvas(output);

	final Paint paint = new Paint();
	final Rect src = new Rect((int) left, (int) top, (int) right, (int) bottom);
	final Rect dst = new Rect((int) dst_left, (int) dst_top, (int) dst_right, (int) dst_bottom);
	final RectF rectF = new RectF(dst);

	paint.setAntiAlias(true);// 设置画笔无锯齿

	canvas.drawARGB(0, 0, 0, 0); // 填充整个Canvas

	// 以下有两种方法画圆,drawRounRect和drawCircle
	canvas.drawRoundRect(rectF, roundPx, roundPx, paint);// 画圆角矩形,第一个参数为图形显示区域,第二个参数和第三个参数分别是水平圆角半径和垂直圆角半径。
	// canvas.drawCircle(roundPx, roundPx, roundPx, paint);

	paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));// 设置两张图片相交时的模式,参考http://trylovecatch.iteye.com/blog/1189452
	canvas.drawBitmap(bitmap, src, dst, paint); // 以Mode.SRC_IN模式合并bitmap和已经draw了的Circle

	return output;
}
 
Example 20
Source File: BitmapUtils.java    From BigApp_Discuz_Android with Apache License 2.0 3 votes vote down vote up
/**
 * 获得圆角图片的方法
 */
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);

    canvas.drawBitmap(bitmap, rect, rect, paint);

    return output;

}