samedi 25 janvier 2020

How to draw a canvas bitmap image at one of two locations randomly?

How to draw a bitmap at either one of two random positions? I wanna draw a bitmap image at either

  1. at a point which is in center from one side of screen and from center of the screen
  2. or at center from other side

like as show by the dots below

 ______________________
|     .     |     .    |             
|           |          |
|           |          |
|           |          |
|           |          |
|           |          |
|           |          |
|           |          |
|           |          |
|Typical android screen|
|           |          |
________________________

Here's how tried to attain it but failed

//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);
    }
}

actually I will use it in another class to use it as alien ships coming to attack but it only come at on the first dot..

Please give me as many details as you can




Aucun commentaire:

Enregistrer un commentaire