簡體   English   中英

使用畫布和位圖的全屏圖像

[英]Full screen image using canvas and bitmap

我正在嘗試使用畫布和位圖在全屏模式下設置圖像,但是每當我加載該應用程序時,圖像都會被放大,並且僅顯示其應有的一半。 我該怎么做? 我已附上我的代碼段,並嘗試更新onDraw方法中的canvas.drawbitmap行的代碼,但它無濟於事,圖像仍會放大顯示。有些幫助將不勝感激,謝謝您提前...

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)

drawBitmap左上角和頂部,定義圖像左上角的(x,y),在您的情況下應為(0,0)。

如果要繪制canvas大小的背景,最好使用:

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

其中srcRect ,它定義要繪制的Bitmap的子集(可以將其保留為null以繪制整個Bitmap ),而dst是目標Rect ,它將自動用Bitmap填充。

您將需要定義目標Rect

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

您現在可以打電話

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM