Code write a nine grid layout display picture

Source: Internet
Author: User

Do not set the layout in the XML, write a layout directly in the code, display the downloaded picture, as shown, the picture is a little ugly

Add a linearlayout to the XML

<relativelayout xmlns:android="Http://schemas.android.com/apk/res/android" xmlns:tools= "http://schemas.android.com/tools"android:layout_width="Match_ Parent "android:layout_height="match_parent "tools:context=" ${ Relativepackage}.${activityclass} " >                    <textview  android:id  = "@+id/text"  android:layout_width  =< Span class= "Hljs-value" > "wrap_content"  android:layout_height  = "wrap_content"  android:text  =" Nine Gongge display picture "/>      <linearlayoutandroid:id="@+id/report_photo_layout"android:layout_width ="Match_parent"android:layout_height="Wrap_content"android:layout_margin ="10dip"android:orientation="vertical"android:layout_below= "@+id/text"/>                                                       </relativelayout>

Also add a layout, display the picture view_files_image.xml

<?xml version= "1.0" encoding= "Utf-8"?><relativelayout  xmlns: Android  = "http://schemas.android.com/apk/res/android"  android:layout_width  =" match_parent " android:layout_height  =" match_parent " android:gravity  =;     <ImageViewandroid:id= "@+id/image"android:layout_width="Match_ Parent "android:layout_height=" Match_parent "android:layout_centerhorizontal= "true" Android:scaletype="Centercrop" />                                        </relativelayout>

The other is the code:

 Public  class mainactivity extends Activity {    PrivateLinearLayout Layoutphotos;//    Privatelinkedhashmap<string, string> Choosephotos =NewLinkedhashmap<string, string> ();//Selected photos    PrivateDisplaymetrics DM =NewDisplaymetrics ();@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_main); Layoutphotos = (linearlayout) Findviewbyid (r.id.report_photo_layout);//choosephotos Oh let's add a picture addressChoosephotos.put ("1","Http://lllqmw.qiniudn.com/1.png"); Choosephotos.put ("2","Http://lllqmw.qiniudn.com/2.png"); Choosephotos.put ("3","Http://lllqmw.qiniudn.com/3.png"); Choosephotos.put ("4","Http://lllqmw.qiniudn.com/4.png"); Choosephotos.put ("5","Http://lllqmw.qiniudn.com/5.png"); Choosephotos.put ("6","Http://lllqmw.qiniudn.com/6.png"); Choosephotos.put ("7","Http://lllqmw.qiniudn.com/7.png"); Choosephotos.put ("8","Http://lllqmw.qiniudn.com/8.png");//Show PicturesLayoutshowfiles (); }/** * Nine Gongge the way to display the downloaded picture * /    Private void Layoutshowfiles() {layoutphotos.removeallviews ();//Four views per line, calculate the width of the firstGetwindowmanager (). Getdefaultdisplay (). Getmetrics (DM);intmargins = (int) dm.density *2;intPhotowidth = (int) (((Dm.widthpixels-dm.density * -) /3)-Margins *2.5); LOG.I ("Info","The size of each view is:"+ Photowidth +", the spacing is:"+ margins); LinearLayout sublayout =NewLinearLayout ( This);        Sublayout.setorientation (linearlayout.horizontal); Layoutparams Subparams =NewLayoutparams (Photowidth, photowidth); Subparams.setmargins (margins, margins, margins, margins);inti =0; Iterator<entry<string, string>> iter = Choosephotos.entryset (). Iterator (); while(Iter.hasnext ()) {Sublayout.addview (Createlayoutphoto (i, Iter.next (). GetValue (), subparams));ifI3==2) {Layoutphotos.addview (sublayout,NewLayoutparams (Layoutparams.match_parent, layoutparams.wrap_content)); Sublayout =NewLinearLayout ( This);            Sublayout.setorientation (linearlayout.horizontal);        } i++; }//Calculate the sublayout can also put a few view, if already put manufactoring 4, just new line        if((3-Choosephotos.size ()%3) ==3) {sublayout =NewLinearLayout ( This);        Sublayout.setorientation (linearlayout.horizontal); } Layoutphotos.addview (Sublayout,NewLayoutparams (Layoutparams.match_parent, layoutparams.wrap_content)); }/** * Generate a picture * @param ID * @param path * @param params * @return  * *    PrivateViewCreatelayoutphoto(Final intImageposition,FinalString path, layoutparams params) {View v = getlayoutinflater (). Inflate (R.layout.view_files_image,NULL);        V.setlayoutparams (params); Imageloader.getinstance (). DisplayImage (Path, (ImageView) V.findviewbyid (r.id.image), softapplication.imageoptions );returnV }}

Application required to download the image

 Public  class softapplication extends application {       Public StaticDisplayimageoptions imageoptions;@Override     Public void onCreate() {Super. OnCreate (); Initimageloader (Getapplicationcontext ());//Set picture properties: Download the picture, download the failed picture, whether round cornerImageoptions =NewDisplayimageoptions.builder (). Cacheinmemory (true). Cacheondisc (true). Showimageonloading (r.drawable.default_loading)//Load start default picture. Showimageforemptyuri (r.drawable.default_loading)//URL to display the picture in empty, and put it in the drawable. Showimageonfail (r.drawable.default_loading)//loading the picture with a problem, the picture appears. Imagescaletype (Imagescaletype.in_sample_int). Bitmapconfig (Bitmap.Config.RGB_565). build (); } Public Static void Initimageloader(Context context) {imageloaderconfiguration config =NewImageloaderconfiguration.builder (context). threadpriority (Thread.norm_priority-2). Denycacheimagemultiplesizesinmemory (). Disccachefilenamegenerator (NewMd5filenamegenerator ()). Tasksprocessingorder (Queueprocessingtype.lifo). Writedebuglogs ()//Remove for release app. build ();    Imageloader.getinstance (). init (config); }   }

There's a place that's easy to forget: Androidmanifest.xml statement

  <application        android:name="com.example.demo_imagedisplay.SoftApplication"

OK, click Demo_imagedisplay to download the full code.

More communication and technical discussion group:71262831
Sweep, sit and look at the changeable. Scan the QR code below to focus on it talent (also searchable:it talent ).
For you to push the latest development resources, share it cattle career development experience:

Code write a nine grid layout display picture

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.