Android 查看scratch游戏以隐藏图像
我想做一个游戏,一个scratch游戏(mpain1.setXfermode(新PorterDuffXfermode(Mode.CLEAR));),屏幕上有3个独立案例。怎么做 屏幕上仅显示一个案例。当这3个案例被清除后,分数将根据显示的图像显示。多谢各位Android 查看scratch游戏以隐藏图像,android,android-layout,Android,Android Layout,我想做一个游戏,一个scratch游戏(mpain1.setXfermode(新PorterDuffXfermode(Mode.CLEAR));),屏幕上有3个独立案例。怎么做 屏幕上仅显示一个案例。当这3个案例被清除后,分数将根据显示的图像显示。多谢各位 public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCre
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;
}
}
公共类MainActivity扩展活动{
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView图像=新的ImageView(此);
setImageResource(R.drawable.ic_启动器);
CaseView caseOne=新CaseView(这个,R.drawable.ic_启动器,0);
//TableLayout table=(TableLayout)findviewbyd(R.id.matable);
TableRow TableRow=(TableRow)findViewById(R.id.marow);
//table.setLayoutParams(tableParams);
tablerow.addView(案例一);
}
公共类CaseView扩展了视图{
专用静态最终浮点数MINP=1f;
专用静态最终浮点最大值=1f;
私有位图mBitmap1,_scratch=null;
私有画布mCanvas1;
私有画布mCanvas2、mCanvas3;
专用路径mPath1;
私人涂料mBitmapPaint1;
私人涂料公司1;
私人国际图像;
私有int位置TOP=0;
私有字符串colorGold=“#E29F2B”;
private boolean firstTime=false;
//私有位图图像;
//私人油漆;
布尔isErase;
公共案例视图(上下文ctx、属性集属性){
超级(ctx、ATTR);
}
公共案例视图(上下文c、内部剩余、内部位置顶部){
超级(c);
this.positionTop=positionTop;
第一次=正确;
mPaint1=新油漆();
mPaint1.setAntiAlias(true);
mPaint1.setDither(true);
mPaint1.setColor(颜色.透明);
mPaint1.setStyle(油漆、样式、笔划);
mPaint1.设定行程宽度(60);
//创建一个正方形
mBitmap1=Bitmap.createBitmap(100100,Bitmap.Config.ARGB_8888);
对于(int i=0;i<100;i++){
对于(int j=0;j<100;j++){
mBitmap1.setPixel(i,j,Color.parseColor(colorGold));
}
}
mCanvas1=新画布(mBitmap1);
mPath1=新路径();
mBitmapPaint1=新油漆(油漆抖动标志);
//mBitmapPaint3=新油漆(油漆抖动标志);
IdImage=剩余;
如果(_scratch==null){
_scratch=BitmapFactory.decodeResource(getResources(),IdImage);
}
}
@凌驾
已更改尺寸的受保护空心(整数w、整数h、整数oldw、整数oldh){
super.onSizeChanged(w,h,oldw,oldh);
}
@凌驾
受保护的void onDraw(画布){
//对数v(“,”onDraw“);
画布。drawColor(颜色。白色);
//标志
canvas.drawBitmap(_scratch,0,positionTop,null);
//canvas.drawBitmap(mBitmap1,null,新矩形(0,0100),
//mBitmapPaint1);
canvas.drawBitmap(mBitmap1,0,positionTop,mBitmapPaint1);
drawBitmap(_scratch,0,400,null);
drawBitmap(mBitmap2,0400,mBitmapPaint2);
//mBitmap=Bitmap.createBitmap(100100,Bitmap.Config.ARGB_8888);
如果((mX<100&&mX>=0)
&&(我的=positionTop)){
//Log.v(“onDraw”,“x:+mX+”/y:+mY);
mpain1.setMaskFilter(null);
mPath1.reset();
mpain1.setXfermode(新的PorterDuffXfermode(Mode.CLEAR));
//mBitmap.setPixel((int)mY,(int)mX,Color.TRANSPARENT);
//mBitmap1.擦除颜色(颜色.透明);
mCanvas1.setBitmap(mBitmap1);
canvas.drawBitmap(mBitmap1,0,positionTop,mBitmapPaint1);
使无效();
}否则{
mpain1.setXfermode(null);
}
super.onDraw(帆布);
int pixelColor=mBitmap1.getPixel(50,50);
int red=颜色。红色(像素颜色);
int green=Color.green(像素颜色);
Log.v(“红色”,“红色:+红色+”/绿色:+绿色);
//使无效();
}
私人浮动mX,我的;
专用静态最终浮动接触公差=4;
专用无效触摸启动(浮动x、浮动y){
mPath1.reset();
mPath1.移动到(x,y);
mX=x;
mY=y;
}
私有无效触摸移动(浮动x、浮动y){
float dx=Math.abs(x-mX);
float dy=Math.abs(y-mY);
如果(dx>=接触公差| | dy>=接触公差){
mPath1.quadTo(mX,mY,(x+mX)/2,(y+mY)/2);
mPath2.quadTo(mX,mY,(x+mX)/2,(y+mY)/2);
mX=x;
mY=y;
}
}
私人空间修补(){
mPath1.lineTo(mX,mY);
mPath2.lineTo(mX,mY);
//将路径提交到我们的屏幕外
mCanvas1.绘图路径(mPath1,mPaint1);
//杀了它,这样我们就不会重复抽签了
mPath1.reset();
}
@凌驾
公共布尔onTouchEvent(运动事件){
float x=event.getX();
float y=event.getY();
Log.v(“onTouchEvent”,“x:+x+”/y:+y”);
开关(event.getAction()){
case MotionEvent.ACTION\u DOWN:
如果((mX<100&&mX>=0)
&&(我的=positionTop)){
mPath1.reset();
mpain1.setMaskFilter(null);
mPaint1.设定行程宽度(90);
mpain1.setXfermode(新的PorterDuffXfermode(Mode.CLEAR));
//mCanvas1.绘图(x,y,10,mPaint1);
isErase=真;
}否则{
mpain1.setXfermode(null);
isErase=假;
}
触摸启动(x,y);
使无效();
打破
case MotionEvent.ACTION\u移动:
如果(iSeries){
mCanvas1.画圈(x,y,20,mPaint1);
}
触摸移动(x,y);
使无效();
打破
case MotionEvent.ACTION\u UP:
如果(iSeries){
mCanvas1.画圈(x,y,20,mPaint1);
}
润色;
使无效();
打破
}
返回true;
}
}
欢迎使用stackoverflow。您提供了大量代码,说明了您想要实现的目标,但没有说明您实际尝试了什么,以及您希望我们帮助您解决的确切问题。您能否澄清这一点(并提供一些最低限度的代码,而不是整个应用程序)我想做一个游戏,一个刮擦游戏。我决定创建一个显示油漆的视图。F