Android在centerCropped图像中创建一个圆孔

时间:2016-01-29 14:35:42

标签: android bitmap hole-punching porter-duff

我再来一次脑机会的想法!

我想在imageView中放一个圆孔,哪个位图缩放为centerCrop。我知道在哪里需要放置圆孔(从左到右的dp)和孔半径。但是不知道如何构建它!

我知道我可以使用Porterduff来做洞,但你建议做什么?

  • 自定义位图
  • 自定义绘图/视图
  • 自定义代码

由于

在Answers之后有我的带有孔的CustomImage:

public class MyImageView extends ImageView {


private AttributeSet attrs;
private float y;
private float x;
private float r;
private Paint paint;
private Rect mSrcRect;
private Rect mDestRect;
private Bitmap mBitmap;
private int alreadycalled = 0;

public MyImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.attrs = attrs;
    initView();
}

public MyImageView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    this.attrs = attrs;
    initView();
}

public MyImageView(Context context, float x, float y, float radius) {
    super(context);
    this.x = x;
    this.y = y;
    this.r = radius;
    Log.d("parameters", String.format("left:%s , right:%s, radius:%s", String.valueOf(x), String.valueOf(y), String.valueOf(r)));
    initView();
}

private void initView() {
    paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setStyle(Paint.Style.FILL_AND_STROKE);
    paint.setStrokeWidth(10);
    paint.setColor(0xff000000);
}

@Override
protected void onDraw(Canvas canvas) {
    alreadycalled++;
    Log.d("alreadycalled", "called " + alreadycalled);
    Drawable mDrawable = getDrawable();
    if (mDrawable == null) {
        return; // couldn't resolve the URI
    }
    int dWidth = mDrawable.getIntrinsicWidth();
    int dHeight = mDrawable.getIntrinsicHeight();

    float scale = 1.0f;
    scale = Math.max(getWidth() * 1.0f / dWidth, getHeight()
            * 1.0f / dHeight);
    int nWidth = (int) (dWidth * scale);
    int nHeight = (int) (dHeight * scale);
    int offsetLeft = (nWidth - getWidth()) / 2;
    int offsetTop = (nHeight - getHeight()) / 2;
    mBitmap = ((BitmapDrawable) mDrawable).getBitmap();
    //custom mSrcRect mDestRect to achieve centerCrop
    mSrcRect = new Rect(0, 0, dWidth, dWidth);
    mDestRect = new Rect(-offsetLeft, -offsetTop, getWidth() + offsetLeft, getHeight() + offsetTop);
    Log.d("src", mSrcRect.toString());
    Log.d("dest", mDestRect.toString());
    int sc = canvas.saveLayer(0, 0, getWidth(), getHeight(), null,
            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);
    paint.setColor(0xffffffff);
    canvas.drawBitmap(mBitmap, mSrcRect, mDestRect, paint);
    paint.setColor(0xffff0000);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
    Log.d("position", String.format("%s , %s", String.valueOf(x), String.valueOf(y)));
    canvas.drawCircle(x, y, r, paint);
    paint.setXfermode(null);
    canvas.restoreToCount(sc);


}

我用这些行以编程方式调用它:

BitmapDrawable bd=(BitmapDrawable) getResources().getDrawable(R.drawable.triangle_bas_accueil2);
MyImageView customImView = new MyImageView(getApplicationContext(), mX, mY, mRadius);
customImView.setImageDrawable(bd);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
params.gravity = Gravity.BOTTOM;
customImView.setLayoutParams(params);
down_relative.addView(customImView);

onDraw()方法被调用两次(可能需要),但这让我有两个洞,一个我可以改变参数,但另一个仍然在同一个地方! MyImageView的容器是RelativeLayout

如果someOne有想法? @ tiny-sunlight?

2 个答案:

答案 0 :(得分:2)

这个只适用于CenterCrop,不能处理scaleType。这段代码可能会有一些问题,因为我不擅长画布。

public class MyImageView extends ImageView {


    private final AttributeSet attrs;
    private Paint paint;
    private Rect mSrcRect;
    private Rect mDestRect;
    private Bitmap mBitmap;
    public MyImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.attrs = attrs;
        initView();
    }

    public MyImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.attrs = attrs;
        initView();
    }

    private void initView() {
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        paint.setStrokeWidth(10);
        paint.setColor(0xff000000);
    }

    @Override
    protected void onDraw(Canvas canvas) {
//        super.onDraw(canvas);
        //create the drawable.Maybe you can cache it.
        Drawable mDrawable = (BitmapDrawable) getResources().getDrawable(R.drawable.triangle_bas_accueil2);
        if (mDrawable == null) {
            return; // couldn't resolve the URI
        }
        int dWidth = mDrawable.getIntrinsicWidth();
        int dHeight = mDrawable.getIntrinsicHeight();

        float scale = 1.0f;
        scale = Math.max(getWidth() * 1.0f / dWidth, getHeight()
                * 1.0f / dHeight);
        int nWidth = (int) (dWidth*scale);
        int nHeight = (int) (dHeight*scale);
        int offsetLeft = (nWidth - getWidth())/2;
        int offsetTop = (nHeight - getHeight())/2;
        //cache mBitmap 
        mBitmap = mBitmap == null ? ((BitmapDrawable) mDrawable).getBitmap(): mBitmap;
        //custom mSrcRect mDestRect to achieve centerCrop
        mSrcRect = new Rect(0, 0, dWidth, dWidth);
        mDestRect = new Rect(-offsetLeft, -offsetTop,getWidth()+offsetLeft, getHeight()+offsetTop);
        int x = 250;int r = 100;
        int sc = canvas.saveLayer(0, 0, getWidth(), getHeight(), null,
                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);
        paint.setColor(0xffffffff);
        canvas.drawBitmap(mBitmap,mSrcRect,mDestRect,paint);
        paint.setColor(0xff000000);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
        canvas.drawCircle(x,x,r,paint);
        paint.setXfermode(null);
        canvas.restoreToCount(sc);
    }
}

答案 1 :(得分:0)

自定义代码。循环遍历每个像素并更改alpha。这是一些伪代码:

for (int y = 0; y < imageHeight; y++) {
    for (int x = 0; x < imageWidth; x++) {
        // Check if the current pixel is within the circle
        if (dist(x, y, centerX, centerY) <= radius) {
            // Change the alpha
            image.setPixelAlpha(x, y, 0);
        }
    }
}