How to Prevent image stretching when setting the background image

Source: Internet
Author: User

When setting the background image, if the image is not stretched enough, the image will be distorted. If the image is too large, the display of the View control will be affected.
If you only set images in imageview, you can use setscaletype in the program for dynamic settings. in XML, you can simply set images using Android: scaletype.
(Android: scaletype = "center_inside": images are smaller than views, and images are not stretched.
Android: scaletype = "center_crop": The image size is larger than that of the view. After the view is not stretched, the settings of other attributes are studied .)
What we need to do now is to set the background of linearlayout to make the image adaptive to the screen size, including the adjustment during screen rotation.
The program code is as follows:

Private drawable createimage (File imagefile) {try {// get the length and width of the current screen displaymetrics dm = new displaymetrics (); this. getwindowmanager (). getdefadisplay display (). getmetrics (DM); float screenwidth = DM. widthpixels; float screenheight = DM. heightpixels; // obtain the image size and calculate the scaling ratio bitmapfactory. options o = new bitmapfactory. options (); O. injustdecodebounds = true; bitmapfactory. decodestream (New fileinputstream (imagefile), Null, O); float bitmapwidth = O. outwidth; float bitmapheight = O. outheight; float scale = (screenwidth/bitmapwidth <screenheight/bitmapheight )? Screenwidth/bitmapwidth: screenheight/bitmapheight; // zoom in the Bitmap bitmap = bitmapfactory. decodestream (New fileinputstream (imagefile); matrix = new matrix (); matrix. postscale (scale, scale); bitmap resizedbitmap = bitmap. createbitmap (bitmap, 0, 0, bitmap. getwidth (), bitmap. getheight (), matrix, true); bitmap. recycle (); // draw the background image bitmap mbitmap = bitmap. createbitmap (INT) screenwidth, (INT) screenheight, bitmap. config. rgb_565); canvas mcanvas = new canvas (mbitmap); paint bitmappaint = new paint (paint. filter_bitmap_flag); // sets the background color mcanvas. drawcolor (0xff000000); mcanvas. drawbitmap (resizedbitmap, screenwidth/2-bitmapwidth * scale/2, screenheight/2-bitmapheight * scale/2, bitmappainting); mcanvas. save (); bitmapdrawable drawable = new bitmapdrawable (mbitmap); resizedbitmap. recycle (); Return drawable;} catch (filenotfoundexception e) {} return NULL ;}

The call method is as follows:

    try {            Drawable image = createImage(imageFile);            background.setBackgroundDrawable(image);        } catch (Exception e) {            Log.e("Exception", e.toString());            return false;        }

// ================================================ ============================================
In the above program, matrix is used to zoom in and out images.
The option of bitmapfactory. decodestream can only be scaled in or out by an integer multiple (usage is described in other articles)
Matrix features are very powerful, not only can zoom in and out, but can also set transparency, matrix operations, a total of divided into translate (translation), rotate (rotation), scale (scaling) and skew.
For more information, see:
Http://blog.csdn.net/hui_ttoo/article/details/6202762
Http://liliang1222.iteye.com/blog/1152474
Http://www.moandroid.com /? P = 1781
Http://www.cnblogs.com/leon19870907/articles/1978065.html
Http://www.r-base.net/archives/148
Http://my.oschina.net/amigos/blog/59598
// ================================================ ==========================================
Canvas is used to draw a new background image based on the scaling ratio of the previous image and the background image for display.
For specific usage, refer to the URL.
Http://aina-hk55hk.iteye.com/blog/690921
// ================================================ ==========================================
In addition, during the data query process, we also found another way to make images.
Reference: http://blog.csdn.net/pgmsoul/article/details/7073332

BitmapDrawable drawable = new BitmapDrawable(mBitmap);drawable.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);drawable.setDither(true);

You can implement tilemode. Repeat and tilemode. Mirror, and set different parameters in settilemodexy.
There is also another tilemode. Clamp, edge stretching effect, do not know under what circumstances will be used.
// ================================================ ==========================================
In addition, you can use draw9patch to create a background without distortion.
For example, textview border, you can use .9.png.
Specific can refer to the Web site: http://archive.cnblogs.com/a/2017591/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.