gpt4 book ai didi

java - 使用 Canvas 和位图的全屏图像

转载 作者:太空宇宙 更新时间:2023-11-04 10:02:25 27 4
gpt4 key购买 nike

我尝试使用 Canvas 和位图在全屏中设置图像,但每当我加载应用程序时,图像都会放大并且仅显示应有的一半。我该怎么做?我已经附加了我的代码 fragment ,并且我尝试更新 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 left和top中定义图像左上角的(x,y),在您的情况下应该是(0,0)。

如果您想绘制 canvas 大小的背景,最好使用:

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

其中 src 是定义要绘制的 Bitmap 子集的 Rect(您可以将其保留为 null 以绘制整个 Bitmap),而 dst 是将自动填充 Bitmap 的目标 Rect

您需要定义目标矩形:

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

您现在可以调用

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

关于java - 使用 Canvas 和位图的全屏图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53331491/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com