Code to write a nine-palace grid layout display picture, nine-palace grid layout display picture
Do not set the layout in xml. Write a layout directly in the code to display the downloaded image, as shown in. The picture is a bit ugly.
Add linearLayout in 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 =" wrap_content "android: layout_height =" wrap_content "android: text = ""/> <LinearLayout android: 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>
Add a layout to 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="center" > <ImageView android: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 {private LinearLayout layoutPhotos; // private LinkedHashMap <String, String> choosePhotos = new LinkedHashMap <String, String> (); // private DisplayMetrics dm = new DisplayMetrics (); @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); layoutPhotos = (LinearLayout) findViewById (R. id. report_photo_layout); // select choosePhotos to add the image address choosePhotos. 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"); // display the picture layoutShowFiles ();}/*** display the downloaded picture in the mode of the 9 lattice */private void layoutShowFiles () {layoutPhotos. removeAllViews (); // calculate the first getWindowManager () in four views per line (). getdefadisplay display (). getMetrics (dm); int margins = (int) dm. density * 2; int photoWidth = (int) (dm. widthPixels-dm. density * 30)/3)-margins * 2.5); Log. I ("info", "the size of each view is:" + photoWidth + ", and the spacing is:" + margins); LinearLayout subLayout = new LinearLayout (this); subLayout. setOrientation (LinearLayout. HORIZONTAL); LayoutParams subParams = new LayoutParams (photoWidth, photoWidth); subParams. setMargins (margins, margins); int I = 0; Iterator <Entry <String, String> iter = choosePhotos. entrySet (). iterator (); while (iter. hasNext () {subLayout. addView (createLayoutPhoto (I, iter. next (). getValue (), subParams); if (I % 3 = 2) {layoutPhotos. addView (subLayout, new LayoutParams (LayoutParams. MATCH_PARENT, LayoutParams. WRAP_CONTENT); subLayout = new LinearLayout (this); subLayout. setOrientation (LinearLayout. HORIZONTAL);} I ++;} // calculate the number of views in sublayout. if four views have been merged, a new line, if (3-choosePhotos. size () % 3) = 3) {subLayout = new LinearLayout (this); subLayout. setOrientation (LinearLayout. HORIZONTAL);} layoutPhotos. addView (subLayout, new LayoutParams (LayoutParams. MATCH_PARENT, LayoutParams. WRAP_CONTENT);}/*** generate an image * @ param id * @ param path * @ param params * @ return */private View createLayoutPhoto (final int imagePosition, final String 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); return v ;}}
Application required for downloading images
Public class SoftApplication extends Application {public static DisplayImageOptions imageOptions; @ Override public void onCreate () {super. onCreate (); initImageLoader (getApplicationContext (); // set the image attributes: the image that is downloaded, the image that fails to be downloaded, and whether the image is rounded to imageOptions = new DisplayImageOptions. builder (). cacheInMemory (true ). cacheOnDisc (true ). showImageOnLoading (R. drawable. default_loading) // loads the default image. showImageForEmptyUri (R. drawable. default_loading) // If the url is null, the image is displayed and placed in drawable. showImageOnFail (R. drawable. default_loading) // if an error occurs during image loading, the image is displayed. imageScaleType (ImageScaleType. IN_SAMPLE_INT ). bitmapConfig (Bitmap. config. RGB_565 ). build ();} public static void initImageLoader (Context context) {ImageLoaderConfiguration config = new ImageLoaderConfiguration. builder (context ). threadPriority (Thread. NORM_PRIORITY-2 ). denyCacheImageMultipleSizesInMemory (). discCacheFileNameGenerator (new Md5FileNameGenerator ()). tasksProcessingOrder (QueueProcessingType. LIFO ). writeDebugLogs () // Remove for release app. build (); ImageLoader. getInstance (). init (config );}}
The AndroidManifest. xml declaration is easy to forget.
<application android:name="com.example.demo_imagedisplay.SoftApplication"
OK. Click Demo_imageDisplay to download the complete code.
More discussion groups can be added:71262831
Let's take a look at the changing situation. Scan the QR code below to follow the it talents (You can also search:It talents).
Push the latest development resources for you and share your it career development experience: