我有一个画布,其中在图像Hand,位图下面有一个背景图像位图。位图手在中心有一只手,另一个空间是透明的,不可见的。所以,如果我应用方法hand.getPixel(x,Y)
并写出颜色的相应整数值,当我点击不可见部分(无论如何可见背景图像)时,我应该得到值0,即不可见的颜色。但我得到其他整数值,虽然背景只有一种颜色,但值不同。在此我编写代码,我希望有人可以帮助我。
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);
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;
}