2012-12-07 25 views
3

我正在做Android上的一个小刮刮卡游戏。我有一个ImageView放置在屏幕的中心和一个自定义视图。我使用特定的颜色填充自定义视图(比如绿色,以便ImageView未显示),然后当用户在屏幕上移动他的手指时,我想从自定义视图中清除颜色,以便从下方显示ImageView。我看到这个线程:Two layers, but can't show the bottom layer in android,但坚持如何创建可擦除位图,该位图的绘画和路径的绘制。 这里是我的XML布局:使用分层方法进行刮刮卡游戏

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <ImageView android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:src="@drawable/img_background" 
     android:contentDescription="@string/app_name"/> 
    <com.example.scratchcard.TouchEventView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
    /> 
</RelativeLayout> 

而且这里是我使用的代码,现在的TouchEventView:

public class TouchEventView extends View { 
    private Paint paint = new Paint(); 
    private Path path = new Path(); 

    public TouchEventView(Context context, AttributeSet attrs) { 
     super(context, attrs); 

     paint.setColor(Color.TRANSPARENT); 
     paint.setAntiAlias(true); 
     paint.setStyle(Paint.Style.STROKE); 
     paint.setStrokeJoin(Paint.Join.ROUND); 
     paint.setStrokeCap(Paint.Cap.ROUND); 
     paint.setStrokeWidth(10f); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 

     canvas.drawColor(Color.GREEN); 
     canvas.drawPath(path, paint); 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     float eventX = event.getX(); 
     float eventY = event.getY(); 

     switch (event.getAction()) { 
     case MotionEvent.ACTION_DOWN: 
      path.moveTo(eventX, eventY); 
      return true; 
     case MotionEvent.ACTION_MOVE: 
      path.lineTo(eventX, eventY); 
      break; 
     case MotionEvent.ACTION_UP: 
      // nothing to do 
       break; 
     default: 
      return false; 
     } 

     // Schedules a repaint. 
     invalidate(); 
     return true; 
    } 
} 

我期待TouchEventView的像素在手指移动到去透明但不会发生。任何帮助将不胜感激。

+2

问一个具体的问题。把一些代码(不是你的整个文件)和你正面临的问题的快照。 – Siddharth

+0

嗨Siddarth我已经更新了建议的问题。 – ABH

+0

看看您在sdk示例中的fingerpaint演示。 –

回答