[英]Bitmap.getPixel in SurfaceView
I have this code where there is a Backgroung under an Hand, both a Bitmap images. 我有这段代码,其中在手下有一个Backgroung,两个都是位图图像。 Now, i have to control with an onClick method if the click touches the hand or not. 现在,如果单击是否触摸到手,我必须使用onClick方法进行控制。 For this, i wrote here and some guys tell me to use a method called " bitmap.getPixel(int x, int y)
" that gives the color of pixel touched. 为此,我在这里写道,一些人告诉我使用一种称为“ bitmap.getPixel(int x, int y)
”的方法,该方法可以使被触摸的像素产生颜色。 For control if the click doesn't touch the hand, i write " bitmap.getPixel(x, y)
!= Color.TRASPARENT"
, but doesn't work perfectly. 为了控制单击是否不碰手,我写了“ bitmap.getPixel(x, y)
!= Color.TRASPARENT"
,但效果不理想。 Under this i write the code of SurfaceView. 在此之下,我编写了SurfaceView的代码。
PS The bitmap "Hand" is a picture of a central Hand with parts trasparent around. PS位图“手”是中央手的图片,周围有透明的部分。
public class GamePanel extends SurfaceView implements SurfaceHolder.Callback{
private long missileStartTime;
private ThreadLv1 thread;
private OggettiLv1 hand;
private int conto=0;
private Sfondo sfondo;
MediaPlayer ouch;
public GamePanel(Context context){
super(context);
getHolder().addCallback(this);
thread = new ThreadLv1(getHolder(), this);
setFocusable(true);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height){}
@Override
public void surfaceDestroyed(SurfaceHolder holder){
boolean retry = true;
while(retry)
{
try{thread.setRunning(false);
thread.join();
}catch(InterruptedException e){e.printStackTrace();}
retry = false;
}
}
@Override
public void surfaceCreated(SurfaceHolder holder){
hand = new OggettiLv1(BitmapFactory.decodeResource(getResources(), R.drawable.mano5));
sfondo = new Sfondo(BitmapFactory.decodeResource(getResources(), R.drawable.sfondo));
ouch= MediaPlayer.create(getContext(), R.raw.ouch);
knifesong.start();
thread.setRunning(true);
thread.start();
}
public void update() {
}
@Override
public void draw(Canvas canvas)
{
if(canvas!=null) {
final int savedState = canvas.save();
background.draw(canvas);
hand.draw(canvas);
drawText(canvas);
canvas.restoreToCount(savedState);
}
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
float x = event.getX();
float y = event.getY();
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
int c = hand.sprite.getPixel((int) event.getX(), (int) event.getY());
if(c!=Color.TRANSPARENT){
conto++;
}
return true;
}
return false;
}
} }
Someone know the problem and the solution in this code? 有人知道该代码中的问题和解决方案吗? Thanks in advance. 提前致谢。
I think this might be the problem: 我认为这可能是问题所在:
int c = hand.sprite.getPixel((int) event.getX(), (int) event.getY());
I think you are getting the value of a pixel at the x,y location on the sprite, you should get the value of the pixel on canvas. 我认为您正在sprite的x,y位置获得像素的值,应该在画布上获得像素的值。
EDIT: 编辑:
Since your canvas is static, I suggest doing the following: add a Bitmap to your class 由于您的画布是静态的,因此建议您执行以下操作:将Bitmap添加到您的类中
private Bitmap canvasState;
In the draw function, initialize canvasState before anything else 在draw函数中,首先初始化canvasState
canvasState = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.RGB_565));
canvas.setBitmap(canvasState);
after that, every change that you make to the canvas will be made to the canvasState, and you can use that in your event handler like so: 之后,对canvas所做的每个更改都将对canvasState进行,您可以在事件处理程序中使用它,如下所示:
int c = canvasState.getPixel((int) event.getX(), (int) event.getY());
I'm not sure if the code I wrote will compile (I wrote them from the top of my head) but I think you can work out compile errors yourself. 我不确定我编写的代码是否可以编译(我是从头开始编写的),但是我认为您可以自己解决编译错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.