I have a canvas with a background image. I need to know if it's possible to clear the paint from this canvas for redraw without clearing its background image. Here is my example and my results so far.
JAVA
public void setCanvas() {
if(mFile != null && mFile.exists()) {
mPictureBitmap = BitmapFactory.decodeFile(mFile.getAbsolutePath());
mBitmap = Bitmap.createScaledBitmap(mPictureBitmap, mImageView.getWidth(), mImageView.getHeight(), false);
mBitmap = mBitmap.copy(Bitmap.Config.ARGB_8888, true);
mCanvas = new Canvas(mBitmap);
mImageView.setImageBitmap(mBitmap);
mImageView.setOnTouchListener(this);
}
}
private void draw() {
mCanvas.drawColor(Color.TRANSPARENT, Mode.CLEAR); // THIS LINE CLEARS
int lastIndex = mCordHashMap.size() - 1;
int index = 0;
for (LinkedHashMap.Entry<String, ArrayList<Circle>> entry : mCordHashMap.entrySet()) {
ArrayList<Circle> coords = new ArrayList<Circle>();
coords = entry.getValue();
Path path = new Path();
String key = entry.getKey();
String surface = getSurfaceFromKey(key);
changePaint(surface);
if (coords.size() < 3) {
for (Circle c : coords) {
mCanvas.drawCircle(c.getmX(), c.getmY(), RADIUS, mCirclePaint);
}
} else {
for (Circle c : coords) {
if (c == coords.get(0)) {
path.moveTo(c.getmX(), c.getmY());
} else {
path.lineTo(c.getmX(), c.getmY());
}
}
path.close();
mCanvas.drawPath(path, mPaint);
mCanvas.drawPath(path, mStrokePaint);
}
if (index == lastIndex && !mSpecificPathSelected) {
for (Circle c : coords) {
mCanvas.drawCircle(c.getmX(), c.getmY(), RADIUS, mCirclePaint);
mCanvas.drawCircle(c.getmX(), c.getmY(), RADIUS, mCircleStrokePaint);
}
}
mImageView.invalidate();
index++;
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ExpandableListView
android:id="@+id/surfaceList"
android:background="@color/light_grey"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".20" />
<ImageView
android:id="@+id/drawContainer"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".80"
android:contentDescription="@string/app_name"/>
</LinearLayout>
So the code above loads an image into my Canvas
. Then each time I touch the Canvas
it draws a circle and eventually a path between the circles. However when I start adding more circles and paths, it's redrawing over itself, which looks terrible.
Now I can clear the paint from my canvas with this line.
mCanvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);
However I also lose the Bitmap
I set as it's background, and now my background is black. So, how do I keep my background image but clear my paint? Is this possible or do I have to use a work around such as two Canvas
's on top of each other?
I have already tried the following, and several variations of the same.
1) mCanvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);
2) mCanvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);
mCanvas.drawBitmap(mBitmap, 0, 0, mPaint);
3) mBitmap.eraseColor(Color.TRANSPARENT);
mCanvas.drawBitmap(mBitmap, 0, 0, mPaint);
Any help is appreciated