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

标签 java android image android-studio canvas

我尝试使用 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 是目标Rect,将自动填充Bitmap

您需要定义目标矩形:

  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/

相关文章:

java - 如何将一个大小为 M 的数组合并到另一个大小为 2M 的数组中

java - 使用 SSL 和 Bouncy CaSTLe 进行 Android 到服务器的通信

javascript - 使用 jquery 在页面上查找具有特定文件名的图像

java - 如何在png图像中保持透明背景

java - 将字符串的第二个索引与数组字符串中下一个字符串的第二个索引进行比较

java - 如何修复 `android.app.Application cannot be cast to` 错误

java - 是否可以在 CodenameOne 中实现一个命令,在不使用 URL 的情况下按下按钮后将显示 PDF

android - 是否有在 Android 中实现企业托管应用程序配置的标准方法?

android - Gradle(Android Studio)不会生成jar文件

css - 如何使用 PrimeNG 删除 Accordion 面板标题上的箭头图标?