我正在开发儿童游戏,孩子可以匹配像a到a等字母。我的问题是如何在匹配字母时隐藏drawable,匹配功能正常工作.. 下面是我的代码
DrugDrop.java
public class DragDrop extends Activity{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//for no title
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
// draw the view
setContentView(new DrawView(this));
}
public class DrawView extends View
{
Context context;
private ColorBall[] colorballs = new ColorBall[3]; // array that holds the balls
private int balID = 0; // variable to know what ball is being dragged
int sx,sy,tx,ty;
Bitmap aa,pp,ppp,ll,ee,trans,nn,tt;
int a;
Point point1 = new Point();
Point point2 = new Point();
Point point3 = new Point();
public DrawView(Context context)
{
super(context);
setFocusable(true); //necessary for getting the touch events
//getting the screen size
Display display = getWindowManager().getDefaultDisplay();
sx = display.getWidth();
sy = display.getHeight();
a=R.drawable.a;
trans=BitmapFactory.decodeResource(getResources(), R.drawable.trans);
aa=BitmapFactory.decodeResource(getResources(), R.drawable.aa);
nn=BitmapFactory.decodeResource(getResources(), R.drawable.nn);
tt=BitmapFactory.decodeResource(getResources(), R.drawable.tt);
// setting the start point for the balls
point1.x = sx-200;
point1.y = 250;
point2.x = sx/2-50;
point2.y = 250;
point3.x = 100;
point3.y = 250;
// declare each ball with the ColorBall class
colorballs[0] = new ColorBall(context,a, point1);
colorballs[1] = new ColorBall(context,R.drawable.n, point2);
colorballs[2] = new ColorBall(context,R.drawable.t, point3);
}
// the method that draws the balls
@Override protected void onDraw(Canvas canvas)
{
canvas.drawBitmap(tt,100, 50, null);
canvas.drawBitmap(nn,sx/2-50, 50, null);
canvas.drawBitmap(aa,sx-200, 50, null);
if(set_a)
{
a=R.drawable.trans;
}
//draw the balls on the canvas
for (ColorBall ball : colorballs)
{
tx=ball.getX();
ty=ball.getY();
canvas.drawBitmap(ball.getBitmap(),tx, ty, null);
if(tx>sx/2-50 && tx<((sx/2)-50)+20 && ty>50 && ty<70 && balID==2)
{
nn=BitmapFactory.decodeResource(getResources(), R.drawable.n);
}
if(tx>sx-200 && tx<(sx-200)+20 && ty>50 && ty<=75 && balID==1)
{
aa=BitmapFactory.decodeResource(getResources(), R.drawable.a1);
set_a=true;
}
if(tx>100 && tx<120 && ty>50 && ty<=75 && balID==3)
{
tt=BitmapFactory.decodeResource(getResources(), R.drawable.t);
}
}
}
// events when touching the screen
public boolean onTouchEvent(MotionEvent event)
{
int eventaction = event.getAction();
int X = (int)event.getX();
int Y = (int)event.getY();
switch (eventaction )
{
case MotionEvent.ACTION_DOWN: // touch down so check if the finger is on a ball
balID = 0;
for (ColorBall ball : colorballs)
{
// check if inside the bounds of the ball (circle)
// get the center for the ball
int centerX = ball.getX() + 25;
int centerY = ball.getY() + 25;
// calculate the radius from the touch to the center of the ball
double radCircle = Math.sqrt( (double) (((centerX-X)*(centerX-X)) + (centerY-Y)*(centerY-Y)));
// if the radius is smaller then 23 (radius of a ball is 22), then it must be on the ball
if (radCircle < 23)
{
balID = ball.getID();
break;
}
// check all the bounds of the ball (square)
//if (X > ball.getX() && X < ball.getX()+50 && Y > ball.getY() && Y < ball.getY()+50){
// balID = ball.getID();
// break;
//}
}
break;
case MotionEvent.ACTION_MOVE: // touch drag with the ball
// move the balls the same as the finger
if (balID > 0)
{
colorballs[balID-1].setX(X-25);
colorballs[balID-1].setY(Y-25);
}
break;
case MotionEvent.ACTION_UP:
// touch drop - just do things here after dropping
break;
}
// redraw the canvas
invalidate();
return true;
}
}
}
ColorBall.java
public class ColorBall{
private Bitmap img; // the image of the ball
private int coordX = 0; // the x coordinate at the canvas
private int coordY = 0; // the y coordinate at the canvas
private int id; // gives every ball his own id, for now not necessary
private static int count = 1;
private boolean goRight = true;
private boolean goDown = true;
public ColorBall(Context context, int drawable)
{
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
img = BitmapFactory.decodeResource(context.getResources(), drawable);
id=count;
count++;
}
public ColorBall(Context context, int drawable, Point point)
{
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
img = BitmapFactory.decodeResource(context.getResources(), drawable);
id=count;
count++;
coordX= point.x;
coordY = point.y;
}
public static int getCount()
{
return count;
}
void setX(int newValue)
{
coordX = newValue;
}
public int getX()
{
return coordX;
}
void setY(int newValue)
{
coordY = newValue;
}
public int getY()
{
return coordY;
}
public int getID()
{
return id;
}
public Bitmap getBitmap()
{
return img;
}
public void moveBall(int goX, int goY)
{
// check the borders, and set the direction if a border has reached
if (coordX > 270){
goRight = false;
}
if (coordX < 0){
goRight = true;
}
if (coordY > 400){
goDown = false;
}
if (coordY < 0){
goDown = true;
}
// move the x and y
if (goRight){
coordX += goX;
}else
{
coordX -= goX;
}
if (goDown){
coordY += goY;
}else
{
coordY -= goY;
}
}
}
答案 0 :(得分:0)
目前尚不清楚要隐藏球的位置,甚至不清楚它们的确切类型,但隐藏视图的标准方法是使用setVisibility(View.INVISIBLE)
或setVisibility(View.GONE)
。由于您已覆盖onDraw()
,因此您可能需要在绘制每个球之前手动检查getVisibility()
,看看您是否隐藏了它。