画布未设置为屏幕中心

时间:2014-03-28 10:35:36

标签: android canvas bitmap

我有两个画布和两个位图,一个位图在用户触摸时覆盖另一个位图并在第一个位图上移动它变得透明并显示第二个位图图像,直到听到每个东西都正常工作。但是当我试图将两个位图移动到屏幕中心时,触摸没有正确检测到,我必须在空白屏幕上方触摸图像以使其可擦除。

package com.example.canvastest;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.AvoidXfermode.Mode;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.BlurMaskFilter;
import android.graphics.BlurMaskFilter.Blur;
import android.graphics.Canvas;
 import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public class MainActivity extends Activity {
Canvas c2;
 @Override
 public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new TouchView(this));


}



class TouchView extends View{
Bitmap bgr;
Bitmap overlayDefault;
Bitmap overlay;
Paint pTouch;
int X = -100;
int Y = -100;


public TouchView(Context context) {
    super(context);

    bgr = BitmapFactory.decodeResource(getResources(),R.drawable.sg);
   // overlayDefault = BitmapFactory.decodeResource(getResources(),R.drawable.g325);
    overlay = BitmapFactory.decodeResource(getResources(),R.drawable.g325).copy(Config.ARGB_8888, true);  
    c2 = new Canvas(overlay);

    pTouch = new Paint(Paint.ANTI_ALIAS_FLAG);         
    pTouch.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN)); 
    pTouch.setColor(Color.TRANSPARENT);
    pTouch.setMaskFilter(new BlurMaskFilter(10, Blur.NORMAL));


}

@Override
public boolean onTouchEvent(MotionEvent ev) {

    switch (ev.getAction()) {

        case MotionEvent.ACTION_DOWN: {

            X = (int) ev.getX();
            Y = (int) ev.getY();
            invalidate();

            break;
        }

        case MotionEvent.ACTION_MOVE: {

                X = (int) ev.getX();
                Y = (int) ev.getY();
                invalidate();
                break;

        }           

        case MotionEvent.ACTION_UP:

            break;

    }
    return true;
}


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


    //draw background
    canvas.drawBitmap(bgr, 0, 0, null);
    //copy the default overlay into temporary overlay and punch a hole in it                          
    //c2.drawBitmap(overlayDefault, 0, 0, null); //exclude this line to show all as you draw
    c2.drawCircle(X, Y, 40, pTouch);
    //draw the overlay over the background
    //canvas.drawBitmap(overlay, 0, 0, null);

    Paint new_paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    new_paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
    canvas.drawBitmap(overlay, 0,  0, new_paint);

}


}


}

0 个答案:

没有答案