I'm trying to cut an Image view, for now I've created a custom component. This is the code:
package it.patrick91.eventually;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;
public class CutImageView extends ImageView {
private Path clipPath;
private int type = 0;
public int getType() {
return type;
}
public void setType(int type) {
Log.d("cut", new Integer(type).toString());
this.type = type;
}
public CutImageView(Context context) {
super(context);
}
public CutImageView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = getContext().obtainStyledAttributes(attrs,
R.styleable.CutImageView);
setType(a.getInt(R.styleable.CutImageView_type, 0));
}
public CutImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray a = getContext().obtainStyledAttributes(attrs,
R.styleable.CutImageView);
setType(a.getInt(R.styleable.CutImageView_type, 0));
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
Log.d("cut", "size changed " + w + " " + h);
clipPath = new Path();
if (type == 0) {
clipPath.moveTo(0, 0);
clipPath.lineTo(0, h);
clipPath.lineTo(w, 0);
clipPath.lineTo(0, 0);
} else {
clipPath.moveTo(w, 0);
clipPath.lineTo(w, h);
clipPath.lineTo(0, h);
}
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
protected void onDraw(Canvas canvas) {
//canvas.clipPath(clipPath);
super.onDraw(canvas);
Bitmap rounder = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(rounder);
Paint xferPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
xferPaint.setColor(Color.BLACK);
c.drawPath(clipPath, xferPaint);
xferPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
canvas.drawBitmap(rounder, 0, 0, xferPaint);
}
}
So I'm basically using the xfer method as shown in this answer.
The problem is that the erased part is black. As I can read from the comments of that answer I have to ensure that I'm in an ARGB drawing context, but I'm not sure if I can do it within a subclass of ImageView. Can you help me?
This is what I get: