0

我想做一个游戏,一个划痕游戏 (mPaint1.setXfermode(new PorterDuffXfermode(Mode.CLEAR)); ),屏幕上有 3 个独立案例。怎么做?

http://img822.imageshack.us/img822/868/35861879.png

屏幕显示只有一个案例。当这三个案例被清除后,分数显示在显示图像的功能中。谢谢你。

public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ImageView image = new ImageView(this);
    image.setImageResource(R.drawable.ic_launcher);


    CaseView caseOne=new CaseView(this, R.drawable.ic_launcher, 0);

    //TableLayout table = (TableLayout)findViewById(R.id.matable);
    TableRow tablerow = (TableRow)findViewById(R.id.marow);
    //table.setLayoutParams(tableParams);
    tablerow.addView(caseOne);
}

public class CaseView extends View {

private static final float MINP = 1f;
private static final float MAXP = 1f;

private Bitmap mBitmap1, _scratch = null;
private Canvas mCanvas1;
private Canvas mCanvas2, mCanvas3;
private Path mPath1;
private Paint mBitmapPaint1;

private Paint mPaint1;

private int IdImage;
private int positionTop = 0;
private String colorGold = "#E29F2B";
private boolean firstTime = false;

// private Bitmap image;
// private Paint paint;

boolean isErase;

public CaseView(Context ctx, AttributeSet attrs) {
    super(ctx, attrs);
}

public CaseView(Context c, int resId, int positionTop) {
    super(c);

    this.positionTop = positionTop;
    firstTime = true;

    mPaint1 = new Paint();
    mPaint1.setAntiAlias(true);
    mPaint1.setDither(true);
    mPaint1.setColor(Color.TRANSPARENT);
    mPaint1.setStyle(Paint.Style.STROKE);
    mPaint1.setStrokeWidth(60);

    // create a square
    mBitmap1 = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
    for (int i = 0; i < 100; i++) {
        for (int j = 0; j < 100; j++) {
            mBitmap1.setPixel(i, j, Color.parseColor(colorGold));
        }
    }

    mCanvas1 = new Canvas(mBitmap1);

    mPath1 = new Path();

    mBitmapPaint1 = new Paint(Paint.DITHER_FLAG);

    // mBitmapPaint3 = new Paint(Paint.DITHER_FLAG);
    IdImage = resId;
    if (_scratch == null) {
        _scratch = BitmapFactory.decodeResource(getResources(), IdImage);
    }
}

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

@Override
protected void onDraw(Canvas canvas) {
    // Log.v("", "onDraw");

    canvas.drawColor(Color.WHITE);
    // logo
    canvas.drawBitmap(_scratch, 0, positionTop, null);

    // canvas.drawBitmap(mBitmap1, null, new Rect(0,0,100,100),
    // mBitmapPaint1);
    canvas.drawBitmap(mBitmap1, 0, positionTop, mBitmapPaint1);

    canvas.drawBitmap(_scratch, 0, 400, null);
    canvas.drawBitmap(mBitmap2, 0, 400, mBitmapPaint2);

    // mBitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);

    if ((mX < 100 && mX >= 0)
            && (mY < positionTop + 100 && mY >= positionTop)) {
        // Log.v("onDraw", "x:" + mX + "/y:" + mY);
        mPaint1.setMaskFilter(null);
        mPath1.reset();
        mPaint1.setXfermode(new PorterDuffXfermode(Mode.CLEAR));

        // mBitmap.setPixel((int) mY,(int) mX, Color.TRANSPARENT);
        // mBitmap1.eraseColor(Color.TRANSPARENT);
        mCanvas1.setBitmap(mBitmap1);

        canvas.drawBitmap(mBitmap1, 0, positionTop, mBitmapPaint1);
        invalidate();

    } else {
        mPaint1.setXfermode(null);  
    }

    super.onDraw(canvas);

    int pixelColor = mBitmap1.getPixel(50, 50);
    int red = Color.red(pixelColor);
    int green = Color.green(pixelColor);
    Log.v("red", "red:" + red + " /green:" + green);

    // invalidate();
}

private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;

private void touch_start(float x, float y) {
    mPath1.reset();
    mPath1.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) {
        mPath1.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
        mPath2.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
        mX = x;
        mY = y;
    }
}

private void touch_up() {
    mPath1.lineTo(mX, mY);
    mPath2.lineTo(mX, mY);
    // commit the path to our offscreen
    mCanvas1.drawPath(mPath1, mPaint1);

    // kill this so we don't double draw
    mPath1.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:
        if ((mX < 100 && mX >= 0)
                && (mY < positionTop + 100 && mY >= positionTop)) {
            mPath1.reset();
            mPaint1.setMaskFilter(null);
            mPaint1.setStrokeWidth(90);
            mPaint1.setXfermode(new PorterDuffXfermode(Mode.CLEAR));

            // mCanvas1.draw(x, y, 10, mPaint1);
            isErase = true;
        } else {
            mPaint1.setXfermode(null);
            isErase = false;
        }

        touch_start(x, y);
        invalidate();
        break;
    case MotionEvent.ACTION_MOVE:
        if (isErase) {
            mCanvas1.drawCircle(x, y, 20, mPaint1);
        }
        touch_move(x, y);
        invalidate();
        break;
    case MotionEvent.ACTION_UP:
        if (isErase) {
            mCanvas1.drawCircle(x, y, 20, mPaint1);
        }
        touch_up();
        invalidate();
        break;
    }
    return true;
}

  }
4

0 回答 0