【问题标题】:Full screen image using canvas and bitmap使用画布和位图的全屏图像
【发布时间】:2019-04-19 06:23:32
【问题描述】:

我正在尝试使用画布和位图全屏设置图像,但是每当我加载应用程序时,图像都会放大并且只显示应有的一半。我该怎么做?我已经附上了我的代码 sn-p 并且我尝试在 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;
}
}

【问题讨论】:

    标签: java android image android-studio canvas


    【解决方案1】:
    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)
    

    其中srcRect,它定义了您要绘制的Bitmap 的子集(可以将其保留为空以绘制整个Bitmap),dst 是目标@ 987654330@ 将自动填充为Bitmap

    您需要定义目的地Rect

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

    你现在可以打电话了

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

    【讨论】:

    • 我应该在哪里创建 drawBitmap 方法?
    • 您不需要创建它,它与您使用的 Canvas 中的方法相同,只是签名不同(参数的顺序和类型不同)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-14
    • 2013-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多