【发布时间】:2014-09-04 18:20:52
【问题描述】:
我有一个带有背景图像的画布。我需要知道是否可以在不清除其背景图像的情况下清除此画布上的油漆以进行重绘。这是我的示例和到目前为止的结果。
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>
所以上面的代码将图像加载到我的Canvas 中。然后每次我触摸Canvas 时,它都会画一个圆圈,并最终在圆圈之间画一条路径。但是,当我开始添加更多圆圈和路径时,它会重新绘制自身,这看起来很糟糕。
现在我可以用这条线清除画布上的颜料了。
mCanvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);
但是我也丢失了我设置为背景的Bitmap,现在我的背景是黑色的。那么,如何保留背景图像但清除油漆?这是可能的还是我必须使用诸如两个Canvas 的解决方法?
我已经尝试了以下方法,以及相同的几种变体。
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);
感谢任何帮助
【问题讨论】:
标签: android bitmap android-canvas