How to scale images with canvas as background

0

I try to put a Background in the app, something very simple to learn the use of canvas and bitmaps

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(new Vista(this));
}

public class Vista extends View{

    public Vista(Context context ){
        super(context);
    }

    protected void onDraw(Canvas canvas){
        Paint mipincel = new Paint();
        Bitmap res = BitmapFactory.decodeResource(getResources(),R.drawable.grassbg1);
        res.

        canvas.drawBitmap(res,0,0,null);
    }
}

Well, I understand that with canvas.drawBitmap(res,0,0,null) I am placing my image in the point (0,0) of the canvas that I have created, but of course the image does not occupy the whole screen (I guess because of the size it has and other), what method or what type of that must do to get it to occupy the entire background, and of course, that is scaled for any type of screen. Greetings.

    
asked by Ramosaurio 12.08.2016 в 18:02
source

1 answer

1

This code will help you

protected void onDraw(Canvas canvas){
    int nuevoWidth;
    int nuevoHeight;
    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    nuevoWidth = metrics.widthPixels;
    nuevoHeight = metrics.heightPixels;

    Paint mipincel = new Paint();

    Bitmap res = BitmapFactory.decodeResource(getResources(),R.drawable.grassbg1);
    Bitmap escalado = Bitmap.createScaledBitmap(res, nuevoWidth, nuevoHeight, false);

    canvas.drawBitmap(escalado,0,0,null);
}
    
answered by 10.04.2017 / 12:58
source