我對這個問題在互聯網上沒有答案,我試圖用不同的顏色繪製,用戶可以從中選擇,問題是這個繪製視圖並沒有繪製任何東西,我不知道觸摸橡皮擦Ø廣告的如何擦除只有觸摸點 這裏的customview:如何在android中繪製和擦除touch
public class DrawView extends android.support.v7.widget.AppCompatImageView {
private ArrayList<ColouredPaths> mTouches;
// Current used colour
private int mCurrColour;
private Paint mPaint;
private boolean erase=false;
ColouredPaths mPath;
Canvas mCanvas;
public void setErase(boolean isErase){
//set erase true or false
erase=isErase;
}
public void setColor(int colour) {
mCurrColour = colour;
}
public DrawView(Context context) {
super(context);
init();
}
// XML constructor
public DrawView(Context context, AttributeSet attrs) {
super(context, attrs);
init(); }
private void init() {
mTouches = new ArrayList<>();
mPaint = new Paint();
mPaint.setColor(mCurrColour);
mPaint.setAntiAlias(true);
mPaint.setStrokeWidth(5);
mPaint.setStyle(Paint.Style.FILL);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
}
private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;
private void touch_start(float x, float y) {
mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;
}
private void touch_move(float x, float y) {
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
mX = x;
mY = y;
}
}
private void touch_up() {
mPath.lineTo(mX, mY);
mPath.reset();
// commit the path to our offscreen
mTouches.add(mPath);
// kill this so we don't double draw
mPath.reset();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
mPath=new ColouredPaths(mCurrColour);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touch_start(x, y);
break;
case MotionEvent.ACTION_MOVE:
touch_move(x, y);
break;
case MotionEvent.ACTION_UP:
touch_up();
break;
}
return super.onTouchEvent(event);
}
@Override
protected void onDraw(Canvas c) {
// Let the image be drawn first
super.onDraw(c);
// Draw your custom points here
for (ColouredPaths p : mTouches) {
mPaint.setColor(p.colour);
c.drawPath(p,mPaint);}}
/**
* Class to store the coordinate and the colour of the point.
*/
private class ColouredPaths extends Path{
int colour;
public ColouredPaths(int colour) {
this.colour = colour;
}
}
}
將意識到,如果有人可以提供一個答案 這是我的日誌:
06-14 21:26:03.926 907-1410/? E/hwintelligencewifi: updataApInfo cellid =4190217533
06-14 21:26:03.926 907-1410/? E/hwintelligencewifi: addCurrentApInfo cellid =4190217533
06-14 21:26:03.928 907-1410/? E/hwintelligencewifi: addCurrentApInfo info is already there
06-14 21:26:03.932 907-1410/? E/hwintelligencewifi: inlineAddCurrentApInfo mInfos.size()=31
當我做類似的東西我喜歡[文件](https://github.com/SueSmith/android-drawing-app/blob/master/src/com/例如/ drawingfun/DrawingView.java)在[這個回購](https://github.com/SueSmith/android-drawing-app)。它就像你正在做的那樣繪製/擦除。如果有幫助,它也是教程的一部分(README中的鏈接)。 –
@BrettBeatty如果我想在一個位圖圖像上的imageview這可以使用? – rgl
對不起,我不明白這個問題。當用戶完成繪製時,您可以從視圖中獲取位圖(請參閱canvasBitmap屬性),如果這就是您要求的。 –