create masked overlay the user can clear with touch
I'm beaking my head over something that sounds realy simple but apparently
is not. I want to create some grey with alpha 50% overlay over some
framelayout with drawable background.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/wrapper"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ic_launcher" >
<com.example.DrawView
android:id="@+id/draw_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="visible" />
</FrameLayout>
I want the user to be able to "clear" the over lay by touch, what I mean
is that the pixels that he touches - their color will turn from grey to
transparent. This is my solution:
@Override
protected void onDraw(Canvas canvas) {
mBitmap = getDrawingCache();
mCanvas = new Canvas(mBitmap);
for (Line line : lines) {
mCanvas.drawCircle(line.startX, line.startY, 10, mPaint);
mCanvas.drawLine(line.startX, line.startY, line.stopX, mPaint);
}
if (currentLine != null)
mCanvas.drawLine(currentLine.startX, currentLine.startY,
currentLine.stopX, currentLine.stopY, mPaint);
// mCanvas.drawBitmap(mBitmap, new Matrix(), null);
// if (currentLine != null) {
// for (int i = 0; i < 30; i++) {
// for (int j = 0; j < 30; j++) {
// mBitmap.setPixel((int) Math.min(currentLine.startX
+ i, mBitmap.getWidth()),
// (int) Math.min(currentLine.startY + j,
mBitmap.getHeight()), Color.TRANSPARENT);
// }
//
// }
//
// }
}
And from the constructor of the View I'm calling:
private void init(int width, int height) {
setBackgroundColor(R.color.mobli_dark_40);
setDrawingCacheEnabled(true);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(40);
mPaint.setAlpha(100);
mPaint.setColor(Color.TRANSPARENT);
mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
mBitmap.setPixel(i, j, R.color.mobli_dark_40);
}
}
mCanvas = new Canvas(mBitmap);
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
setOnTouchListener(this);
}
Now, no matter what I'm doing - the mCanvas.drawCircle and
mCanvas.drawLine DOES NOTHING when I'm trying to create transparent
circle/line in order to remove the pixels' color. Only when I'm setting
the pixels' one-by-one color to transparent(like in the comment in
OnDraw(..) the their color is realy being changed to transparent.
Thank you for the help!
No comments:
Post a Comment