In Android, due to the diversity of mobile phones with different pixels, for a picture, there will be differences in the display of different mobile phones due to different pixels.
There is a requirement of filling the entire screen with a picture width and displaying it.
What we often do may be to process images in the following two ways.
Define the attribute layout_parent="match_parent" and layout_parent="wrap_content" through <ImageView>, and use the scaleType attribute of <ImageView> for scaling.
- scaleType="fitXY": When set to this property, it will cause the image to stretch horizontally, causing the image to deform, and if there is a font on the image, then this will be a bad thing.
- scaleType="centerCrop": It should be fine to scale in the same ratio, but it is very embarrassing, it still doesn't work. When processing the image, the size of the image will be obtained first, and then the width and height of the image will be determined before scaling. In this way, after the range of the picture is determined, the center point of the picture is scaled equally until the width completely fills the screen. This is very embarrassing, the upper and lower parts of the picture will be cut off, so the picture is not fully displayed, and it is still not what we want.
My approach is to get the image after scaling in the code, and then add the image by setting backgroud, which solves the problem. Then the above code:
//Get the resolution of the image, get the width DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); int mScreenWidth = dm.widthPixels;//Get screen resolution width int mScreenHeight = dm.heightPixels; //Load the image Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bank_help); int bitmapWidth = bitmap.getWidth(); int bitmapHeight = bitmap.getHeight(); //Get the image width ratio float num = mScreenWidth / (float)bitmapWidth; Matrix matrix = new Matrix(); matrix.postScale(num, num); // Generate the scaled Bitmap object Bitmap resizeBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmapWidth, bitmapHeight, matrix, true);
The above code is divided into the following steps
1. Get the screen resolution.
2. Obtain the image and measure the width and height of the image.
3. Get the corresponding zoom ratio through screen width/image width
4. Create a Matrix object and determine the scaling. (ps: This thing is very nice, and the object is also used to modify it in the gradient color in the front)
5. Generate the image through the Bitmap.createBitmap() method. (ps: The last parameter must be passed true. If false is passed, the zoomed image will not be displayed clearly)
The above is an introduction to Java image processing related operations, I hope it will be helpful to everyone's learning.