2

我在画布上画了很多圆圈,想知道其中哪个被触摸了。一种解决方案是创建一个与屏幕大小相同的位图,并在屏幕上绘制一个仅具有不同颜色的圆圈的副本,以便在调用 getPixel() 时,位图可以识别单击了哪个圆圈。我的问题是我不知道如何在位图上画一个圆......也就是说,如何在Canvas上正常绘制位图作为绘图。

Paint paint;
Bitmap screen;

int w,h;
int px=-1,py=-1; //coordinate 

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    //registriamo le dimensioni della view
    w=MeasureSpec.getSize(widthMeasureSpec);
    h=MeasureSpec.getSize(heightMeasureSpec);
    setMeasuredDimension(w,h);
}

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

    Bitmap.Config conf = Bitmap.Config.ARGB_8888;  //Each pixel is stored on 4 bytes
    screen=Bitmap.createBitmap(w,h, conf);

    paint=new Paint(); // pennello
    paint.setColor(Color.RED);   
    paint.setAntiAlias(true);   
}

@Override
protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    super.onDraw(canvas);
    if(px==-1&&py==-1){ // se non abbiamo ancora settato le coordinate, posizioniamo la bmp al centro
        px=w/2-bw/2; //metà della larghezza view, meno metà della figura
        py=h/2-bh/2; //metà dell'altezza view, meno metà della figura
    }

    canvas.drawCircle(px, py, 70, paint);       
}

public void updatePosition(int x, int y) {      

    invalidate();
}
}
4

2 回答 2

1

您想创建与您的位图相关联的画布,如下所示

gCanvas = new Canvas();
gCanvas.setBitmap(yourBitmap);

然后将其用作画布并绘制您想要的东西

编辑:如果我不清楚,您在此画布上绘制的所有内容就像您在位图上绘制一样。然后根据你的X,Y得到颜色并比较

希望这对您有所帮助并享受您的工作

编辑:工作示例

public class MainActivity extends Activity {
private Bitmap image;
private int width;
private int height;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.clickete);

    Display display = getWindowManager().getDefaultDisplay();               

    width = display.getWidth();
    height = display.getHeight();

    createClickArea();

    findViewById(R.id.clickLayout).setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int result = whichCircle((int)event.getRawX(), (int)event.getRawY());
            Log.i("Color clicked: ", String.valueOf(result));
            return false;
        }
    });

    Drawable dr = new BitmapDrawable(getResources(), image);        

    findViewById(R.id.clickLayout).setBackgroundDrawable(dr);
}

private void createClickArea(){
    Bitmap.Config conf = Bitmap.Config.ARGB_8888; 

    image = Bitmap.createBitmap(width, height, conf);

    Canvas gCanvas = new Canvas();
    gCanvas.setBitmap(image);
    Paint paint = new Paint();
    paint.setStyle(Style.FILL);
    gCanvas.drawRGB(0, 0, 0);       
    for(int i = 0; i < 4; i++){
        switch(i){
        case 0: paint.setColor(Color.WHITE); break;
        case 1: paint.setColor(Color.YELLOW); break;
        case 2: paint.setColor(Color.BLUE); break;
        case 3: paint.setColor(Color.GREEN); break;
        }
        gCanvas.drawCircle((i + 1) * 80, (i + 1) * 80, 40, paint);
    }
}

private int whichCircle(int x, int y){
    int result = 0;
    int color = image.getPixel(x, y);
    switch(color){
    case Color.WHITE: result = 1; break;
    case Color.YELLOW: result = 2; break;
    case Color.BLUE: result = 3; break;
    case Color.GREEN: result = 4; break;
    }
    return result;
}

}

只需解决不推荐使用的方法并隐藏您的位图,抱歉,我现在没有时间替换它,我的午餐越来越冷了 :)'。请记住全屏工作或根据您的位图位置调整您的 rawX、rawY。

于 2013-05-18T16:17:46.230 回答
0

您可以尝试这个,因为您可以通过实现 surfaceholder.callback 将位图绘制为要在 ondraw 函数中首先在画布上绘制的背景

canvas.drawBitmap(bgrReverse, fromRect2, toRect2, null);

然后实现 onTouch() 事件,在该事件中您将获得手指移动的指针并获得这些移动,您可以将圆圈绘制到接收到的特定 onTouch() 并移动手指。

于 2013-05-20T11:19:08.430 回答