安卓:为选择项触摸事件在画布上 [英] Android: Touch event for select item on a canvas
问题描述
我在画布上绘制的许多圈子,想知道其中哪些是感动。
一种解决方案是创建位图作为在屏幕的尺寸和绘制在屏幕上只用不同的颜色,这样,当该呼叫与getPixel()位图poosso识别被点击该圆的圆圈的副本。我的问题是我不知道如何画上一个位图一个圆圈......那就是,如何绘制位图作为常在画布上绘制。
油漆涂料;
位图的屏幕;INT W,H;
INT像素= -1,PY = -1; //坐标@覆盖
保护无效onMeasure(INT widthMeasureSpec,诠释heightMeasureSpec){
// registriamo乐dimensioni德拉视图
W = MeasureSpec.getSize(widthMeasureSpec);
H = MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(W,H);
}公共CustomView(上下文的背景下,ATTRS的AttributeSet){
超(背景下,ATTRS); Bitmap.Config的conf = Bitmap.Config.ARGB_8888; //每个像素存储4个字节
屏幕= Bitmap.createBitmap(W,H,CONF); 油漆=新的油漆(); // pennello
paint.setColor(Color.RED);
paint.setAntiAlias(真);
}@覆盖
保护无效的onDraw(帆布油画){
// TODO自动生成方法存根
super.onDraw(画布);
如果(PX == - 1安培;&安培; PY == - 1){//本身非abbiamo安科拉settato勒协调,posizioniamo拉BMP人中心
PX = W / 2-BW / 2; //元德拉larghezza观点,梅诺元德拉FIGURA
PY = H / 2-BH / 2; //元dell'altezza观点,梅诺元德拉FIGURA
} canvas.drawCircle(PX,PY,70,油漆);
}公共无效updatePosition(INT X,int y)对{ 无效();
}
}
您想创建位图这样的事情相关的帆布
gCanvas =新的Canvas();
gCanvas.setBitmap(yourBitmap);
,然后用它作为画布,画出你想要的东西。
编辑:万一我不清楚,你这个画布上绘制的一切就像是你在你的位图绘制。然后根据你的X,Y得到的颜色和比较
希望这有助于和喜欢你的工作。
编辑:工作的例子。
公共类MainActivity延伸活动{
私人位图图像;
私人诠释宽度;
私人诠释高度;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.clickete); 显示显示= getWindowManager()getDefaultDisplay()。 宽度= display.getWidth();
高度= display.getHeight(); createClickArea(); findViewById(R.id.clickLayout).setOnTouchListener(新OnTouchListener(){ @覆盖
公共布尔onTouch(视图V,MotionEvent事件){
INT结果= whichCircle((int)的event.getRawX(),(INT)event.getRawY());
Log.i(颜色点击:将String.valueOf(结果));
返回false;
}
}); 可绘制博士=新BitmapDrawable(getResources(),图像); findViewById(R.id.clickLayout).setBackgroundDrawable(DR);
}私人无效createClickArea(){
Bitmap.Config的conf = Bitmap.Config.ARGB_8888; 图像= Bitmap.createBitmap(宽度,高度,CONF); 帆布gCanvas =新的Canvas();
gCanvas.setBitmap(图片);
涂料粉刷=新的油漆();
paint.setStyle(Style.FILL);
gCanvas.drawRGB(0,0,0);
对(INT I = 0; I&下; 4;我++){
开关(ⅰ){
情况下0:paint.setColor(Color.WHITE);打破;
案例1:paint.setColor(Color.YELLOW);打破;
案例2:paint.setColor(Color.BLUE);打破;
案例3:paint.setColor(Color.GREEN);打破;
}
gCanvas.drawCircle(第(i + 1)* 80,第(i + 1)* 80,40,漆);
}
}私人诠释whichCircle(INT X,int y)对{
INT结果为0;
INT颜色= image.getPixel(X,Y);
开关(彩色){
案例Color.WHITE:结果= 1;打破;
案例Color.YELLOW:结果= 2;打破;
案例Color.BLUE:结果= 3;打破;
案例Color.GREEN:结果= 4;打破;
}
返回结果;
}}
就解决德precated方法和隐藏自己的位图,抱歉,我没有时间,现在替换这个,我的午餐越来越冷:)。请记住,根据您的位图的位置这个工作在全屏或调整rawX,rawY。
I have many circles drawn on the Canvas and want to know which of them is touched. One solution is to create a bitmap as the size of the screen and draw a copy of the circles that are on the screen only with a different color so that when the call getPixel () the bitmap poosso recognize which circle is clicked. My problem is that I do not know how to draw a circle on a bitmap ... that is, how to draw the bitmap as a drawing normally on 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();
}
}
You want to create Canvas associated with your bitmap something like this
gCanvas = new Canvas();
gCanvas.setBitmap(yourBitmap);
and then use it as a canvas and draw what you want
EDIT: In case I wasn't clear, everything you draw on this canvas is like you draw on your bitmap. Then according to your X, Y get the color and compare
Hope this helps and enjoy your work
EDIT: Working example
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;
}
}
Just resolve deprecated methods and hide your bitmap, sorry but I don't have time to replace this now, my lunch is getting cold :)'. And remember work this in full screen or adjust your rawX, rawY according to your bitmap position.
这篇关于安卓:为选择项触摸事件在画布上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!