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

The following examples show how to use android.graphics.Canvas#drawBitmap() . 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: CBitmap.java    From FabricView with Apache License 2.0 7 votes vote down vote up
@Override
    public void draw(Canvas canvas) {
        Matrix matrix = new Matrix();
        for (CTransform t:
                getTransforms()) {
            t.applyTransform(matrix);
        }
        Bitmap canvasBitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas temp = new Canvas(canvasBitmap);
        temp.save();
        temp.concat(matrix);
        temp.drawBitmap(mBitmap, getXcoords(), getYcoords(), getPaint());
        temp.restore();
//        Bitmap transformedBitmap = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), matrix, true);

        canvas.drawBitmap(canvasBitmap, 0, 0, getPaint());
    }
 
Example 2
Source File: DoodleView.java    From Nimingban with Apache License 2.0 6 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
    if (mBitmap == null) {
        return;
    }

    canvas.drawBitmap(mBitmap, mOffsetX, mOffsetY, mBitmapPaint);

    int saved = canvas.save();

    canvas.translate(mOffsetX, mOffsetY);
    canvas.clipRect(0, 0, mBitmap.getWidth(), mBitmap.getHeight());

    drawStore(canvas, mPaint);

    mPaint.setColor(mEraser ? mBgColor : mColor);
    mPaint.setStrokeWidth(mWidth);
    if (mIsDot) {
        canvas.drawPoint(mX, mY, mPaint);
    } else {
        canvas.drawPath(mPath, mPaint);
    }

    canvas.restoreToCount(saved);
}
 
Example 3
Source File: BigView.java    From long_picture_view with Apache License 2.0 6 votes vote down vote up
/**
 * 画出内容
 */
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    //如果解码器拿不到,表示没有设置过要显示的图片
    if (null == mDecoder) {
        return;
    }
    //复用上一张bitmap
    mOptions.inBitmap = bitmap;
    //解码指定的区域
    bitmap = mDecoder.decodeRegion(mRect, mOptions);
    //把得到的矩阵大小的内存进行缩放  得到view的大小
    Matrix matrix = new Matrix();
    matrix.setScale(mScale, mScale);
    //画出来
    canvas.drawBitmap(bitmap, matrix, null);


}
 
Example 4
Source File: MyMaterialIntroView.java    From IdeaTrackerPlus with MIT License 6 votes vote down vote up
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if(this.isReady) {
        if(this.bitmap == null || canvas == null) {
            if(this.bitmap != null) {
                this.bitmap.recycle();
            }

            this.bitmap = Bitmap.createBitmap(this.width, this.height, Bitmap.Config.ARGB_8888);
            this.canvas = new Canvas(this.bitmap);
        }

        this.canvas.drawColor(0, PorterDuff.Mode.CLEAR);
        this.canvas.drawColor(this.maskColor);
        this.circleShape.draw(this.canvas, this.eraser, this.padding);
        canvas.drawBitmap(this.bitmap, 0.0F, 0.0F, (Paint)null);
    }
}
 
Example 5
Source File: ScrollingActivity.java    From FrostyBackgroundTestApp with Apache License 2.0 6 votes vote down vote up
public Bitmap captureView(View view) {
    //Find the view we are after
    //Create a Bitmap with the same dimensions
    Bitmap image = Bitmap.createBitmap(view.getMeasuredWidth(),
            view.getMeasuredHeight(),
            Bitmap.Config.ARGB_4444); //reduce quality and remove opacity
    //Draw the view inside the Bitmap
    Canvas canvas = new Canvas(image);
    view.draw(canvas);

    //Make it frosty
    Paint paint = new Paint();
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    ColorFilter filter = new LightingColorFilter(0xFFFFFFFF, 0x00222222); // lighten
    //ColorFilter filter = new LightingColorFilter(0xFF7F7F7F, 0x00000000);    // darken
    paint.setColorFilter(filter);
    canvas.drawBitmap(image, 0, 0, paint);

    return image;
}
 
Example 6
Source File: Util.java    From android-common-utils with Apache License 2.0 6 votes vote down vote up
/** 圆角 */
public static Bitmap getRCB(Bitmap bitmap, float roundPX) {
	if (bitmap == null || roundPX <= 0) {
		return bitmap;
	}
	Bitmap dstbmp = Bitmap.createBitmap(bitmap.getWidth(),
			bitmap.getHeight(), Config.ARGB_8888);
	Canvas canvas = new Canvas(dstbmp);
	Paint paint = new Paint();
	RectF rectF = new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight());
	Path mPath = new Path();
	float[] mCorner = new float[] { roundPX, roundPX, roundPX, roundPX,
			roundPX, roundPX, roundPX, roundPX };
	mPath.addRoundRect(rectF, mCorner, Path.Direction.CW);
	paint.setAntiAlias(true);
	canvas.drawARGB(0, 0, 0, 0);
	canvas.drawPath(mPath, paint);
	paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
	canvas.drawBitmap(bitmap, 0, 0, paint);
	return dstbmp;
}
 
Example 7
Source File: BaseEnvironmentMeter.java    From thunderboard-android with Apache License 2.0 6 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (enabled) {
        int width = getWidth();
        int height = getHeight();

        canvas.drawCircle(width / 2, height / 2, width / 2, backgroundBrush);
        canvas.drawBitmap(activeBitmap,
                          (width - activeBitmap.getWidth()) / 2,
                          (height - activeBitmap.getHeight()) / 2,
                          null);
    } else {
        canvas.drawBitmap(inactiveBitmap, 0, 0, null);
    }
}
 
Example 8
Source File: LauncherIcons.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
@Override
public void draw(Canvas canvas) {
    Rect bounds = getBounds();
    canvas.drawBitmap(mState.mShadow, null, bounds, mState.mPaint);
    canvas.save();
    // Ratio of child drawable size to shadow bitmap size
    float factor = 1 / (1 + 2 * ICON_SIZE_BLUR_FACTOR + ICON_SIZE_KEY_SHADOW_DELTA_FACTOR);

    canvas.translate(
            bounds.width() * factor *
                    (ICON_SIZE_BLUR_FACTOR + ICON_SIZE_KEY_SHADOW_DELTA_FACTOR / 2),
            bounds.height() * factor * ICON_SIZE_BLUR_FACTOR);
    canvas.scale(factor, factor);
    super.draw(canvas);
    canvas.restore();
}
 
Example 9
Source File: OvalImageView.java    From android-apps with MIT License 5 votes vote down vote up
public static Bitmap getOvalCroppedBitmap(Bitmap bitmap, int radius) {
	Bitmap finalBitmap;
	if (bitmap.getWidth() != radius || bitmap.getHeight() != radius)
		finalBitmap = Bitmap.createScaledBitmap(bitmap, radius, radius,
				false);
	else
		finalBitmap = bitmap;
	Bitmap output = Bitmap.createBitmap(finalBitmap.getWidth(),
			finalBitmap.getHeight(), Config.ARGB_8888);
	Canvas canvas = new Canvas(output);

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

	paint.setAntiAlias(true);
	paint.setFilterBitmap(true);
	paint.setDither(true);
	canvas.drawARGB(0, 0, 0, 0);
	paint.setColor(Color.parseColor("#BAB399"));
	RectF oval = new RectF(0, 0, 130, 150);
	canvas.drawOval(oval, paint);
	paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
	canvas.drawBitmap(finalBitmap, rect, oval, paint);

	return output;
}
 
Example 10
Source File: LineChartRenderer.java    From Ticket-Analysis with MIT License 5 votes vote down vote up
@Override
public void drawData(Canvas c) {

    int width = (int) mViewPortHandler.getChartWidth();
    int height = (int) mViewPortHandler.getChartHeight();

    if (mDrawBitmap == null
            || (mDrawBitmap.get().getWidth() != width)
            || (mDrawBitmap.get().getHeight() != height)) {

        if (width > 0 && height > 0) {

            mDrawBitmap = new WeakReference<Bitmap>(Bitmap.createBitmap(width, height, mBitmapConfig));
            mBitmapCanvas = new Canvas(mDrawBitmap.get());
        } else
            return;
    }

    mDrawBitmap.get().eraseColor(Color.TRANSPARENT);

    LineData lineData = mChart.getLineData();

    for (ILineDataSet set : lineData.getDataSets()) {

        if (set.isVisible())
            drawDataSet(c, set);
    }

    c.drawBitmap(mDrawBitmap.get(), 0, 0, mRenderPaint);
}
 
Example 11
Source File: RoundedImageView.java    From android-apps with MIT License 5 votes vote down vote up
private Bitmap getRoundedCroppedBitmap(Bitmap bitmap, int radius){
	Bitmap finalBitmap;
	
	if( bitmap.getWidth() != radius || bitmap.getHeight() != radius ){
		finalBitmap = Bitmap.createScaledBitmap(bitmap, radius, radius, false);
	} else{
		finalBitmap = bitmap;
	}
	
	Bitmap output = Bitmap.createBitmap(finalBitmap.getWidth(), finalBitmap.getHeight(), Config.ARGB_8888);
	Canvas canvas = new Canvas(output);
	
	final Paint paint = new Paint();
	final Rect rect = new Rect(0,0, finalBitmap.getWidth(), finalBitmap.getHeight());
	
	paint.setAntiAlias(true);
	paint.setFilterBitmap(true);
	paint.setDither(true);
	
	canvas.drawARGB(0, 0, 0, 0);
	paint.setColor(Color.parseColor("#BAB399"));
	
	canvas.drawCircle(finalBitmap.getWidth() / 2 + 0.7f,
               finalBitmap.getHeight() / 2 + 0.7f,
               finalBitmap.getWidth() / 2 + 0.1f, paint);
	
	paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
	canvas.drawBitmap(finalBitmap, rect, rect, paint);
	
	return output;
}
 
Example 12
Source File: IconsManager.java    From LaunchEnr with GNU General Public License v3.0 5 votes vote down vote up
private Bitmap generateBitmap(Bitmap defaultBitmap) {
    if (mBackImages.isEmpty()) {
        return defaultBitmap;
    }
    Random random = new Random();
    int id = random.nextInt(mBackImages.size());
    Bitmap backImage = mBackImages.get(id);
    int w = backImage.getWidth();
    int h = backImage.getHeight();

    Bitmap result = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(result);
    canvas.drawBitmap(backImage, 0, 0, null);

    Bitmap scaledBitmap = Bitmap.createScaledBitmap(defaultBitmap,
            (int) (w * mFactor), (int) (h * mFactor), false);

    Bitmap mutableMask = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    Canvas maskCanvas = new Canvas(mutableMask);
    Bitmap targetBitmap = mMaskImage == null ? mutableMask : mMaskImage;
    maskCanvas.drawBitmap(targetBitmap, 0, 0, new Paint());

    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
    canvas.drawBitmap(scaledBitmap, (w - scaledBitmap.getWidth()) / 2,
            (h - scaledBitmap.getHeight()) / 2, null);
    canvas.drawBitmap(mutableMask, 0, 0, paint);

    if (mFrontImage != null) {
        canvas.drawBitmap(mFrontImage, 0, 0, null);
    }
    return result;
}
 
Example 13
Source File: Image.java    From ShapeRipple with Apache License 2.0 5 votes vote down vote up
@Override
public void onDraw(Canvas canvas, int x, int y, float radiusSize, int color, int rippleIndex, Paint shapePaint) {
    int currentImageSize = (int) radiusSize;

    // Get the current alpha channel of the color
    int currentAlpha = 0xFF & (color >> 24);
    shapePaint.setAlpha(currentAlpha);

    this.rect.set(x - currentImageSize, y - currentImageSize, x + (int) radiusSize, y + (int) radiusSize);

    canvas.drawBitmap(bitmap, null, this.rect, shapePaint);
}
 
Example 14
Source File: LineChartRenderer.java    From android-kline with Apache License 2.0 5 votes vote down vote up
@Override
public void drawData(Canvas c) {

    int width = (int) mViewPortHandler.getChartWidth();
    int height = (int) mViewPortHandler.getChartHeight();

    if (mDrawBitmap == null
            || (mDrawBitmap.get().getWidth() != width)
            || (mDrawBitmap.get().getHeight() != height)) {

        if (width > 0 && height > 0) {

            mDrawBitmap = new WeakReference<Bitmap>(Bitmap.createBitmap(width, height, mBitmapConfig));
            mBitmapCanvas = new Canvas(mDrawBitmap.get());
        } else
            return;
    }

    mDrawBitmap.get().eraseColor(Color.TRANSPARENT);

    LineData lineData = mChart.getLineData();

    for (ILineDataSet set : lineData.getDataSets()) {

        if (set.isVisible())
            drawDataSet(c, set);
    }

    c.drawBitmap(mDrawBitmap.get(), 0, 0, mRenderPaint);
}
 
Example 15
Source File: RoundImageView.java    From codeexamples-android with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {

	// Sanity Check
	if (placeholder == null && image == null) {
		return;
	}

	// Only calculate
	if (framedPhoto == null) {
		createFramedPhotoBorder(Math.min(getWidth(), getHeight()));
	}
	canvas.drawBitmap(framedPhoto, 0, 0, null);
}
 
Example 16
Source File: AlphaPatternDrawable.java    From document-viewer with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void draw(final Canvas canvas) {
    final Bitmap b = getBitmap();
    if (b != null) {
        canvas.drawBitmap(b, null, getBounds(), mPaint);
    }
}
 
Example 17
Source File: EaseImageView.java    From Study_Android_Demo with Apache License 2.0 5 votes vote down vote up
/**
 * draw Rounded Rectangle
 *
 * @param canvas
 * @param bitmap
 */
private void drawDrawable(Canvas canvas, Bitmap bitmap) {
    Paint paint = new Paint();
    paint.setColor(0xffffffff);
    paint.setAntiAlias(true); //smooths out the edges of what is being drawn
    PorterDuffXfermode xfermode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);
    // set flags
    int saveFlags = Canvas.MATRIX_SAVE_FLAG
            | Canvas.CLIP_SAVE_FLAG
            | Canvas.HAS_ALPHA_LAYER_SAVE_FLAG
            | Canvas.FULL_COLOR_LAYER_SAVE_FLAG
            | Canvas.CLIP_TO_LAYER_SAVE_FLAG;
    canvas.saveLayer(0, 0, width, height, null, saveFlags);

    if (shapeType == 1) {
        canvas.drawCircle(width / 2, height / 2, width / 2 - 1, paint);
    } else if (shapeType == 2) {
        RectF rectf = new RectF(1, 1, getWidth() - 1, getHeight() - 1);
        canvas.drawRoundRect(rectf, radius + 1, radius + 1, paint);
    }

    paint.setXfermode(xfermode);

    float scaleWidth = ((float) getWidth()) / bitmap.getWidth();
    float scaleHeight = ((float) getHeight()) / bitmap.getHeight();

    Matrix matrix = new Matrix();
    matrix.postScale(scaleWidth, scaleHeight);

    //bitmap scale
    bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);

    canvas.drawBitmap(bitmap, 0, 0, paint);
    canvas.restore();
}
 
Example 18
Source File: StandardShowcaseDrawer.java    From UltimateAndroid with Apache License 2.0 4 votes vote down vote up
@Override
public void drawToCanvas(Canvas canvas, Bitmap bitmapBuffer) {
    canvas.drawBitmap(bitmapBuffer, 0, 0, basicPaint);
}
 
Example 19
Source File: SignaturePad.java    From SSForms with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
    if (mSignatureBitmap != null) {
        canvas.drawBitmap(mSignatureBitmap, 0, 0, mPaint);
    }
}
 
Example 20
Source File: SlidingDrawer.java    From Linphone4Android with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected void dispatchDraw(Canvas canvas) {
	final long drawingTime = getDrawingTime();
	final View handle = mHandle;
	final boolean isVertical = mVertical;

	drawChild(canvas, handle, drawingTime);

	if (mTracking || mAnimating) {
		final Bitmap cache = mContent.getDrawingCache();
		if (cache != null) {
			if (isVertical) {
				if (mInvert) {
					canvas.drawBitmap(cache, 0, handle.getTop()
							- (getCustomBottom() - getTop()) + mHandleHeight,
							null);
				} else {
					canvas.drawBitmap(cache, 0, handle.getBottom(), null);
				}
			} else {
				canvas.drawBitmap(cache,
						mInvert ? handle.getLeft() - cache.getWidth()
								: handle.getRight(), 0, null);
			}
		} else {
			canvas.save();
			if (mInvert) {
				canvas.translate(isVertical ? 0 : handle.getLeft()
						- mTopOffset - mContent.getMeasuredWidth(),
						isVertical ? handle.getTop() - mTopOffset
								- mContent.getMeasuredHeight() : 0);
			} else {
				canvas.translate(isVertical ? 0 : handle.getLeft()
						- mTopOffset, isVertical ? handle.getTop()
						- mTopOffset : 0);
			}
			drawChild(canvas, mContent, drawingTime);
			canvas.restore();
		}
		invalidate();
	} else if (mExpanded) {
		drawChild(canvas, mContent, drawingTime);
	}
}