Android Phone picture adaptation issues

Source: Internet
Author: User

Requirements: Today, when doing the ListView encountered a problem, is the ListView loading the picture. Some pictures are larger in size, so there is a problem with insufficient picture display.

First of all, do not do any processing of the case, the size is like this. The width is wrapcontent.

So how to solve it??

1, first Fix_xy, but this will cause distortion.

2, then need to change a solution, that is, custom view, rewrite the Onmeasure method.

Customize a property: Aspect ratio. Resolve by rewriting the Onmeasure method yourself.

The specific resolution code is as follows:

 Packagecom.itheima.googleplay_8.views;ImportCOM.ITHEIMA.GOOGLEPLAY_8.R;ImportAndroid.content.Context;ImportAndroid.content.res.TypedArray;ImportAndroid.util.AttributeSet;Importandroid.widget.FrameLayout;/** * @authorAdministrator * @time 2015-7-18 pm 2:10:54 * @des TODO * *@version$Rev: $ * @updateAuthor $Author: ADMIN $ * @updateDate $Date: 2015-07-18 15:13:26 +0800 (Saturday, 187 months) $ * @upd Atedes TODO*/ Public classRatiolayoutextendsFramelayout {Private floatMpicratio = 0f;//picture aspect ratio 2.43    Private Static Final intrelative_width = 0;//The width of the control is fixed, the aspect ratio of the picture is known, and the height of the control    Private Static Final intRelative_height = 1;//control height Fixed, width -to-height ratio of known picture    Private intMrelative =Relative_width;  PublicRatiolayout (Context context) { This(Context,NULL); }     PublicRatiolayout (Context context, AttributeSet attrs) {Super(context, attrs); TypedArray TypedArray=context.obtainstyledattributes (Attrs, r.styleable.ratiolayout); Mpicratio= Typedarray.getfloat (r.styleable.ratiolayout_picratio, 0); Mrelative=Typedarray.getint (r.styleable.ratiolayout_relative, relative_width);    Typedarray.recycle (); } @Overrideprotected voidOnmeasure (intWidthmeasurespec,intHeightmeasurespec) {        //The width of the control is fixed, the aspect ratio of the picture is known, and the height of the control        intParentwidthmode =Measurespec.getmode (WIDTHMEASURESPEC); //control height Fixed, width -to-height ratio of known picture        intParentheightmode =Measurespec.getmode (HEIGHTMEASURESPEC); if(Parentwidthmode = = measurespec.exactly && mpicratio! = 0 && mrelative = relative_width) {//The width of the control is fixed, the aspect ratio of the picture is known, and the height of the control//gets the width of the parent container            intParentwidth =measurespec.getsize (WIDTHMEASURESPEC); //get the child's width            intChildwidth = Parentwidth-getpaddingleft ()-getpaddingright (); //The width of the control/height of the control = Mpicratio; //Calculate the height of your child            intChildheight = (int) (Childwidth/mpicratio +. 5f); //Calculate the height of the parent container            intParentheight = Childheight + getpaddingbottom () +Getpaddingtop (); //Active mapping of children. Fixed child size            intChildwidthmeasurespec =Measurespec.makemeasurespec (Childwidth, measurespec.exactly); intChildheightmeasurespec =Measurespec.makemeasurespec (Childheight, measurespec.exactly);            Measurechildren (Childwidthmeasurespec, Childheightmeasurespec); //set your own test resultssetmeasureddimension (Parentwidth, parentheight); } Else if(Parentheightmode = = measurespec.exactly && mpicratio! = 0 && Mrelative = =relative_height) {            //control height Fixed, width -to-height ratio of known picture//get a father's height            intParentheight =measurespec.getsize (HEIGHTMEASURESPEC); //get the height of the child            intChildheight = Parentheight-getpaddingbottom ()-Getpaddingtop (); //The width of the control/height of the control = Mpicratio; //Calculate control Width            intChildwidth = (int) (Childheight * mpicratio +. 5f); //get the width of the Father            intParentwidth = Childwidth + getpaddingright () +Getpaddingleft (); //Active mapping of children. Fixed child size            intChildwidthmeasurespec =Measurespec.makemeasurespec (Childwidth, measurespec.exactly); intChildheightmeasurespec =Measurespec.makemeasurespec (Childheight, measurespec.exactly);            Measurechildren (Childwidthmeasurespec, Childheightmeasurespec); //set your own test resultssetmeasureddimension (Parentwidth, parentheight); } Else {            Super. Onmeasure (Widthmeasurespec, Heightmeasurespec); }    }}

Android Phone picture adaptation issues

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.