Java Code Examples for android.graphics.Canvas#CLIP_TO_LAYER_SAVE_FLAG

The following examples show how to use android.graphics.Canvas#CLIP_TO_LAYER_SAVE_FLAG . 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: ClipView.java    From YiZhi with Apache License 2.0 5 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    int LAYER_FLAGS = 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;
    //通过Xfermode的DST_OUT来产生中间的透明裁剪区域,一定要另起一个Layer(层)
    canvas.saveLayer(0, 0, this.getWidth(), this.getHeight(), null, LAYER_FLAGS);
    //设置背景
    canvas.drawColor(Color.parseColor("#a8000000"));
    paint.setXfermode(xfermode);
    //绘制圆形裁剪框
    if (clipType == ClipType.CIRCLE) {
        //中间的透明的圆
        canvas.drawCircle(this.getWidth() / 2, this.getHeight() / 2, clipRadiusWidth, paint);
        //白色的圆边框
        canvas.drawCircle(this.getWidth() / 2, this.getHeight() / 2, clipRadiusWidth, borderPaint);
    } else if (clipType == ClipType.RECTANGLE) { //绘制矩形裁剪框
        //绘制中间的矩形
        canvas.drawRect(mHorizontalPadding, this.getHeight() / 2 - clipWidth / 2,
                this.getWidth() - mHorizontalPadding, this.getHeight() / 2 + clipWidth / 2, paint);
        //绘制白色的矩形边框
        canvas.drawRect(mHorizontalPadding, this.getHeight() / 2 - clipWidth / 2,
                this.getWidth() - mHorizontalPadding, this.getHeight() / 2 + clipWidth / 2, borderPaint);
    }
    //出栈,恢复到之前的图层,意味着新建的图层会被删除,新建图层上的内容会被绘制到canvas (or the previous layer)
    canvas.restore();
}
 
Example 2
Source File: ViewPagerIndicator.java    From bannerview with Apache License 2.0 5 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    int flags = 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;
    int sc = canvas.saveLayer(0, 0, getWidth(), getHeight(), null, flags);

    int wg = mItemWidth + mItemGap;
    int x = (getWidth() - mWidth) / 2;
    int y = (getHeight() - mItemHeight) / 2;

    mItemDrawable.setBounds(0, 0, mItemWidth, mItemHeight);
    mItemDrawableSelected.setBounds(0, 0, mItemWidth, mItemHeight);

    for (int i = 0; i < mItemCount; i++) {
        canvas.save();
        canvas.translate(x + i * wg, y);
        mItemDrawable.draw(canvas);
        canvas.restore();
    }

    canvas.save();
    canvas.translate(x + (mPosition + mPositionOffset) * wg, y);
    mItemDrawableSelected.draw(canvas);
    canvas.restore();

    canvas.restoreToCount(sc);
}
 
Example 3
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 4
Source File: ShapeImageView.java    From BaseProject with Apache License 2.0 5 votes vote down vote up
private void drawDrawable(Canvas canvas, Bitmap bitmap) {
    Paint paint = new Paint();
    paint.setColor(0xffffffff);
    paint.setAntiAlias(true);

    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 == ShapeType.RECTANGLE) {
        RectF rectf = new RectF(borderWidth / 2, borderWidth / 2, getWidth() - borderWidth / 2, getHeight() - borderWidth / 2);
        canvas.drawRoundRect(rectf, radius, radius, paint);
    } else {
        canvas.drawCircle(width / 2, height / 2, width / 2 - borderWidth, paint);
    }

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); // SRC_IN 只显示两层图像交集部分的上层图像

    //Bitmap缩放
    float scaleWidth = ((float) getWidth()) / bitmap.getWidth();
    float scaleHeight = ((float) getHeight()) / bitmap.getHeight();
    Matrix matrix = new Matrix();
    matrix.postScale(scaleWidth, scaleHeight);
    bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    canvas.drawBitmap(bitmap, 0, 0, paint);
    canvas.restore();
}
 
Example 5
Source File: RoundImageView.java    From PocketEOS-Android with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * 实现圆角的绘制
 *
 * @param canvas
 * @param bitmap
 */
@SuppressLint("WrongConstant")
private void drawDrawable(Canvas canvas, Bitmap bitmap) {
    // 画笔
    Paint paint = new Paint();
    // 颜色设置
    paint.setColor(0xffffffff);
    // 抗锯齿
    paint.setAntiAlias(true);
    //Paint 的 Xfermode,PorterDuff.Mode.SRC_IN 取两层图像的交集部门, 只显示上层图像。
    PorterDuffXfermode xfermode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);
    // 标志
    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) {
        // 画遮罩,画出来就是一个和空间大小相匹配的圆(这里在半径上 -1 是为了不让图片超出边框)
        canvas.drawCircle(width / 2, height / 2, width / 2 - 1, paint);
    } else if (shapeType == 2) {
        // 当ShapeType == 2 时 图片为圆角矩形 (这里在宽高上 -1 是为了不让图片超出边框)
        RectF rectf = new RectF(1, 1, getWidth() - 1, getHeight() - 1);
        canvas.drawRoundRect(rectf, radius + 1, radius + 1, paint);
    }

    paint.setXfermode(xfermode);

    // 空间的大小 / bitmap 的大小 = bitmap 缩放的倍数
    float scaleWidth = ((float) getWidth()) / bitmap.getWidth();
    float scaleHeight = ((float) getHeight()) / bitmap.getHeight();

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

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

    //draw 上去
    canvas.drawBitmap(bitmap, 0, 0, paint);
    canvas.restore();
}