有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在两个位置之一随机绘制画布位图图像?

如何在两个随机位置之一绘制位图? 我想在任何一个位置画一个位图图像

  1. 位于屏幕一侧和屏幕中心的中心点
  2. 或者在另一边的中心

如下面的点所示

 ______________________
|     .     |     .    |             
|           |          |
|           |          |
|           |          |
|           |          |
|           |          |
|           |          |
|           |          |
|           |          |
|Typical 安卓 screen|
|           |          |
________________________

下面是如何试图达到它,但失败了

//package and import
public class items extends View {
   //here declared to some variables
    private int RandomSing;
    Point center;
    public items(int color, Context c) {
        super(c);

        // create a bitmap from the supplied image (the image is the icon that is part of the app)
      items= Bitmap.createScaledBitmap(BitmapFactory.decodeResource(c.getResources(),
                R.drawable.rocket),dst,dst, false);


    }
    public void setBounds(int lx, int ly, int ux, int uy) {
        lowerX = lx;
        lowerY = ly;
        upperX = ux;
        upperY = uy;
        x = y = 0;
        center=new Point(0,0);
        RandomSing=0;
    }
    public boolean move() {
    // use to move the bitmap in vertical direction downwards 
        y += stepY;

        if(stepY<8){stepY+=0.0025;}

        if (y + dst > upperY) {
            x = y = 0 ;
            return true;
        }
        else
            return true;
    }

    public void reset() {
        x=0;
        RandomSing +=7;
        y=0;
    }

    public void draw(Canvas canvas)
    {
        super.draw(canvas);
        X=getWidth();
        Y=getHeight();
        center=new Point((int)(X/4),0 );
        if(RandomSing%2!=0) x = (dst +center.x );
        else x = (dst+center.x+(X/2));
        canvas.drawBitmap(items,x,y, null);
    }
}

事实上,我会在另一个类中使用它,作为外来船只来攻击 但它只在第一个点出现

请给我尽可能多的细节


共 (0) 个答案