简体   繁体   中英

Full screen image using canvas and bitmap

I am tryin to set an image in full screen using canvas and bitmap but whenever I load the app the image gets zoomed in and only displays half of what it's supposed to. How would I do this? I have attached my code snippet and i have tried updating the canvas.drawbitmap line of code inside the onDraw method but it doesn't help, the image still appears zoomed in. Some help would greatly be appreciated thank you in advance...

public class Fish extends View {



private Bitmap fish [] =new Bitmap[2];

private  int fishX=10;
private int fishY;
private int fishSpeed;

private  int canvasWidth, canvasHeight;
private  int yellowX, yellowY, yellowSpeed=16;

private Paint yellowPaint=new Paint();
private int greenX, greenY, greenSpeed=20;
private Paint greenPaint=new Paint();

private int redX, redY, redSpeed=25;
private Paint redPaint=new Paint();


private int  score, lifeCountOfLife;

private boolean touch=false;

private Bitmap backgroundImage;
private Paint scorePaint= new Paint();

private Bitmap life[]=new Bitmap[2];

//updates the background



public Fish(Context context) {
    super(context);
    fish [0]=BitmapFactory.decodeResource(getResources(),R.drawable.fish1);
    fish [1]=BitmapFactory.decodeResource(getResources(),R.drawable.fish2);


    //updates the bg
    backgroundImage=BitmapFactory.decodeResource(getResources(),R.drawable.mn);

    //creates the ball/foood color
    yellowPaint.setColor(Color.YELLOW);
    yellowPaint.setAntiAlias(false);

    greenPaint.setColor(Color.GREEN);
    greenPaint.setAntiAlias(false);

    redPaint.setColor(Color.RED);
    redPaint.setAntiAlias(false);

    // updates score
    scorePaint.setColor(Color.WHITE);
    scorePaint.setTextSize(70);
    scorePaint.setTypeface(Typeface.DEFAULT_BOLD);
    scorePaint.setAntiAlias(true);

    //displays the heart and life
    life[0]=BitmapFactory.decodeResource(getResources(),R.drawable.heartts);
    life[1]=BitmapFactory.decodeResource(getResources(),R.drawable.greyheart);

    fishY=550;
    score=0;
    lifeCountOfLife=3;
}



@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    canvasWidth=canvas.getWidth();
    canvasHeight=canvas.getHeight();



    //displays it to the main actiivty
    canvas.drawBitmap(backgroundImage,canvasWidth,canvasHeight,null);
    int minFish= fish[0].getHeight();
    int maxFishY=canvasHeight-fish[0].getHeight() *3;
    fishY=fishY+fishSpeed;

    if(fishY<minFish){
        fishY=minFish;
    }

    if(fishY> maxFishY){
        fishY=maxFishY;
    }

    fishSpeed=fishSpeed+2;
    if(touch){
        canvas.drawBitmap(fish[1], fishX,fishY, null);
        touch=false;

    }else{
        canvas.drawBitmap(fish[0],fishX,fishY,null);
    }

    yellowX=yellowX-yellowSpeed;

    //updates the score if the ball is collected
    if(hitBallChecker(yellowX,yellowY)){




        score=score+10;
        yellowX=-100;
    }

    if(yellowX<0){
        yellowX=canvasWidth+21;
        yellowY=(int) Math.floor(Math.random() * (maxFishY-minFish))+ minFish;

    }
    //increases size of ball
    canvas.drawCircle(yellowX, yellowY, 25, yellowPaint);


    //Green ball

    greenX=greenX-greenSpeed;
    if(hitBallChecker(greenX,greenY)){
        score=score+20;
        greenX=-100;

    }

    if(greenX<0){
        greenX=canvasWidth+21;
        greenY=(int) Math.floor(Math.random() * (maxFishY-minFish))+ minFish;

    }
    //increases size of ball
    canvas.drawCircle(greenX, greenY, 25, greenPaint);


    //red ball

    //if the ball gets hit
    redX=redX-redSpeed;
    if(hitBallChecker(redX,redY)){







        redX=-100;
        lifeCountOfLife--;
        if(lifeCountOfLife==0){


            Intent gameOverIntent= new Intent(getContext(), GameOverActivity.class);
            gameOverIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

            gameOverIntent.putExtra("score", score);
            getContext().startActivity(gameOverIntent);
        }
    }

    //increases size of ball

    if(redX<0){
        redX=canvasWidth+21;
        redY=(int) Math.floor(Math.random() * (maxFishY-minFish))+ minFish;


    }
    canvas.drawCircle(redX, redY, 30, redPaint);
    canvas.drawText("Score: " +score,  20, 60, scorePaint);

    for(int i=0; i<3; i++){
        int x=(int) (580+life[0].getWidth() * 1.5 * i);
        int y=30;
        if(i<lifeCountOfLife){
            canvas.drawBitmap(life[0], x,y, null);


        }else{
            canvas.drawBitmap(life[1], x,y, scorePaint);

        }
    }





}
public boolean hitBallChecker(int x, int y){
    if(fishX< x && x<(fishX+fish[0].getWidth()) &&fishY<y  &&y<(fishY+ fish[0].getHeight())){
        return true;

    }
    return false;

}

//updates fish speed
@Override
public boolean onTouchEvent(MotionEvent event) {
    if(event.getAction()==MotionEvent.ACTION_DOWN){
        touch=true;
        fishSpeed=-22;

    }
    return true;
}
}
public void drawBitmap (Bitmap bitmap, 
                float left, 
                float top, 
                Paint paint)

In drawBitmap left and top define the (x,y) of the top left corner of the image, which in your case should be (0,0).

If you want to draw a canvas sized backgound it would probably be best to use:

public void drawBitmap (Bitmap bitmap, 
                Rect src, 
                Rect dst, 
                Paint paint)

Where src is the Rect that defines the subset of the Bitmap that you want to draw (you can leave it at null to draw the entire Bitmap ), and dst is the destination Rect that will automatically be filled with the Bitmap .

You are going to need to define the destination Rect :

  Rect backgroundRect = new Rect(0, 0, canvas.getWidth(), canvas.getHeight());

You can now call

canvas.drawBitmap(backgroundImage,null,backgroundRect,null);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM