Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何用手指擦除油漆_Android - Fatal编程技术网

Android 如何用手指擦除油漆

Android 如何用手指擦除油漆,android,Android,我想按在灰度正方形位图上检测到手指的区域进行擦除,但它不起作用 我用ic_launch作为图像显示位图,然后在图像上显示一个灰色的正方形,可以将颜色修改为透明 有什么问题?多谢各位 private Bitmap mBitmap; private Canvas mCanvas; private Path mPath; private Paint mBitmapPaint; private Paint mPaint; public CaseView(Context c) { super(c

我想按在灰度正方形位图上检测到手指的区域进行擦除,但它不起作用

我用ic_launch作为图像显示位图,然后在图像上显示一个灰色的正方形,可以将颜色修改为透明

有什么问题?多谢各位

private Bitmap mBitmap;
private Canvas mCanvas;
private Path mPath;
private Paint mBitmapPaint;
private Paint mPaint;

public CaseView(Context c) {
    super(c);

    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setDither(true);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeWidth(40);


    mBitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
    for (int i = 0; i < 100; i++) {
        for (int j = 0; j < 100; j++) {
            mBitmap.setPixel(i, j, Color.GRAY);
        }

    }
    mCanvas = new Canvas(mBitmap);
    mPath = new Path();
    mBitmapPaint = new Paint(Paint.DITHER_FLAG);

}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(0, 0, oldw, oldh);

}

@Override
protected void onDraw(Canvas canvas) {

    canvas.drawRect(100, 100, 200, 200, mBitmapPaint);
    canvas.drawPath(mPath, mPaint);

    Bitmap _scratch = BitmapFactory.decodeResource(getResources(),
            R.drawable.ic_launcher);
    canvas.drawColor(Color.WHITE);

    // logo
    canvas.drawBitmap(_scratch, 0, 100, null);



    for (int i = 0; i < 100; i++) {
        for (int j = 0; j < 100; j++) {
            if ((mX < 100 && mX >= 0) && (mY < 100 && mY >= 0)) {

                mBitmap.setPixel((int) mY,(int) mX,  Color.TRANSPARENT);
            }
        }

    }
    canvas.drawBitmap(mBitmap, 0, 100, mBitmapPaint);

    Bitmap mutableBitmap = Bitmap.createBitmap(_scratch.getWidth(),
            _scratch.getHeight(), Bitmap.Config.ARGB_8888);
    mutableBitmap.setPixel(50, 50, 124);
    canvas.drawBitmap(mutableBitmap, 0, 100, null);
    int pixelColor = mBitmap.getPixel(50, 50);
    int red = Color.red(pixelColor);
    int green = Color.green(pixelColor);
    Log.v("red", "red:" + red + " /green:" + green);


}


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);
    // commit the path to our offscreen
    mCanvas.drawPath(mPath, mPaint);
    // kill this so we don't double draw
    mPath.reset();
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    float x = event.getX();
    float y = event.getY();
    Log.v("onTouchEvent", "x:" + x + "/y:" + y);

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        touch_start(x, y);
        invalidate();
        break;
    case MotionEvent.ACTION_MOVE:
        touch_move(x, y);
        invalidate();
        break;
    case MotionEvent.ACTION_UP:
        touch_up();
        invalidate();
        break;
    }
    return true;
}
私有位图mBitmap;
私人帆布mCanvas;
专用路径mPath;
私人油漆;
私人油漆;
公共案例视图(上下文c){
超级(c);
mPaint=新油漆();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setStyle(油漆、样式、笔划);
mPaint.设定行程宽度(40);
mBitmap=Bitmap.createBitmap(100100,Bitmap.Config.ARGB_8888);
对于(int i=0;i<100;i++){
对于(int j=0;j<100;j++){
mBitmap.setPixel(i,j,颜色.灰色);
}
}
mCanvas=新画布(mBitmap);
mPath=新路径();
mBitmapPaint=新油漆(油漆抖动标志);
}
@凌驾
已更改尺寸的受保护空心(整数w、整数h、整数oldw、整数oldh){
super.onSizeChanged(0,0,oldw,oldh);
}
@凌驾
受保护的void onDraw(画布){
画布.drawRect(100100200200MBitMapPoint);
画布绘制路径(mPath,mPaint);
位图_scratch=BitmapFactory.decodeResource(getResources(),
R.可牵引ic_发射器);
画布。drawColor(颜色。白色);
//标志
drawBitmap(_scratch,0,100,null);
对于(int i=0;i<100;i++){
对于(int j=0;j<100;j++){
如果((mX<100&&mX>=0)和((mY<100&&mY>=0)){
mBitmap.setPixel((int)mY,(int)mX,Color.TRANSPARENT);
}
}
}
drawBitmap(mBitmap,0,100,MbitMapPoint);
Bitmap mutableBitmap=Bitmap.createBitmap(_scratch.getWidth(),
_scratch.getHeight(),Bitmap.Config.ARGB_8888);
可变位图.setPixel(50,50,124);
drawBitmap(可变位图,0,100,null);
int pixelColor=mBitmap.getPixel(50,50);
int red=颜色。红色(像素颜色);
int green=Color.green(像素颜色);
Log.v(“红色”,“红色:+红色+”/绿色:+绿色);
}
私人浮动mX,我的;
专用静态最终浮动接触公差=4;
专用无效触摸启动(浮动x、浮动y){
mPath.reset();
移动到(x,y)的速度;
mX=x;
mY=y;
}
私有无效触摸移动(浮动x、浮动y){
float dx=Math.abs(x-mX);
float dy=Math.abs(y-mY);
如果(dx>=接触公差| | dy>=接触公差){
兆帕四分之一秒(mX,mY,(x+mX)/2,(y+mY)/2);
mX=x;
mY=y;
}
}
私人空间修补(){
mPath.lineTo(mX,mY);
//将路径提交到我们的屏幕外
mCanvas.drawPath(mPath,mPaint);
//杀了它,这样我们就不会重复抽签了
mPath.reset();
}
@凌驾
公共布尔onTouchEvent(运动事件){
float x=event.getX();
float y=event.getY();
Log.v(“onTouchEvent”,“x:+x+”/y:+y”);
开关(event.getAction()){
case MotionEvent.ACTION\u DOWN:
触摸启动(x,y);
使无效();
打破
case MotionEvent.ACTION\u移动:
触摸移动(x,y);
使无效();
打破
case MotionEvent.ACTION\u UP:
润色;
使无效();
打破
}
返回true;
}

对于绘制,您需要使用一个绘制类绘制路径。但要再次擦除,必须在触摸坐标上画一条路径,如下所示

mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));

android sdk示例中,他们给出了一个类FingerPaint,可以很好地解释它

您检查了吗?如果你解决了,请告诉我。如果是,请将其标记为answer@user1881979但是我在使用mPaint.setXfermode(新的PorterDuffXfermode(PorterDuff.Mode.CLEAR))时遇到了一些问题;你能看看这个问题吗