Android custom Dynamic Layout-Multi-Image Upload,

Source: Internet
Author: User

Android custom Dynamic Layout-Multi-Image Upload,

Android custom Dynamic Layout-Multi-Image Upload


This article describes how to add images and upload multiple images in a Dynamic Layout in Android.


Project:



Technical points:

1.Dynamically Add the line in the pattern and add the + number of the image

2.Asynchronous upload of multiple images


First, let's take a look at the layout file:

<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent" android: background = "# f2f2f2"> <LinearLayout android: id = "@ + id/layout_CONTENT" android: layout_width = "match_parent" android: layout_height = "match_parent" android: background = "# f2f2f2" android: orientation = "vertical" android: padding = "5dp"> <! -- The layout is dynamically generated by the program --> <LinearLayout android: id = "@ + id/layout_container" android: layout_width = "300dp" android: layout_height = "wrap_content" android: layout_gravity = "center_horizontal" android: layout_margin = "5dp" android: background = "# cbcbcb" android: orientation = "vertical" android: padding = "0.2px"/> <TextView android: id = "@ + id/text_no_data" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_margin = "20dp" android: text = "@ string/text_picture_upload" android: textSize = "16dp"/> </LinearLayout>

The layout is very simple, mainly because a LinearLayout with id layout_container serves as the parent layout.



The horizontal line and vertical line layout are also very simple:

<View xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="1px"    android:background="#cbcbcb" />

<View xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="1px"    android:layout_height="match_parent"    android:background="#cbcbcb" />



The following describes how to dynamically generate a layout:

Private void initUI () {setContentView (R. layout. activity_main); // setTitle (R. string. button_service_upload_picture); // showBackwardView (R. string. button_backward, true); // showForwardView (R. string. button_upload, true); // top-level parent layout mLayout = (ViewGroup) findViewById (R. id. layout_container); final int count = 9; // 9 grid final int rowCount = (count + 2)/3; for (int I = 0; I <rowCount; I ++) {if (I! = 0) {// load the Lateral Layout line View. inflate (this, R. layout. layout_line_horizonal, mLayout);} // create the layout object and set the final LinearLayout linearLayout = new LinearLayout (this); linearLayout. setBackgroundResource (R. drawable. row_selector); for (int j = 0; j <3; j ++) {if (j! = 0) {// load the View of the vertical layout of the inner layer. inflate (this, R. layout. layout_line_vertical, linearLayout);} ImageButton imageButton = new ImageButton (this); imageButton. setBackgroundResource (R. drawable. row_selector); imageButton. setTag (TAG); imageButton. setOnClickListener (this); imageButton. setEnabled (false); LinearLayout. layoutParams layoutParams = new LinearLayout. layoutParams (LayoutParams. MATCH_PARENT, LayoutParams. MATCH_PARENT, 1.0f); // Add it to the linearLayout layout. addView (imageButton, layoutParams); // Add the imageButton object to the mImageButtonList. add (imageButton);} DisplayManager manager = DisplayManager. getInstance (); LayoutParams layoutParams = new LayoutParams (LayoutParams. MATCH_PARENT, manager. dipToPixel (100); // Add the View to the total parent layout mLayout. addView (linearLayout, layoutParams);} // set the final ImageButton attribute for the outer layer to final ImageButton currentImageButton = mImageButtonList. get (mCurrent); currentImageButton. setImageResource (R. drawable. ic_add_picture); currentImageButton. setScaleType (ScaleType. CENTER); currentImageButton. setEnabled (true );}


Image Upload function:

Private class UploadPictureTask extends AsyncTask <List <String>, Integer, String> {/* (non-Javadoc) * @ see android. OS. asyncTask # doInBackground (Params []) */@ Override protected String doInBackground (List <String>... params) {final List <String> pictureList = params [0]; for (int I = 0, len = pictureList. size (); I <len; I ++) {final File file = new File (pictureList. get (I); // final String response = PacheHttpUtils. post (mUrlPrefix + "/upload", new File [] {file}); // resolution, storage // final UploadInfo upload = new UploadParser (). parse (response ). getData ();/* if (upload! = Null) {final String url = upload. getUrl (); if (url! = Null) {mPictureUrlList. add (url) ;}} */publishProgress (I);} return null;}/* (non-Javadoc) * @ see android. OS. asyncTask # onProgressUpdate (Progress []) */@ Override protected void onProgressUpdate (Integer... values) {}/* (non-Javadoc) * @ see android. OS. asyncTask # onPostExecute (java. lang. object) */@ Override protected void onPostExecute (String result) {// addPictures (); super. onPostExecute (result );}}

Note: The class declares three lists to save the records of previous operations.

mImageButtonList = new ArrayList<ImageButton>();        mPicturePathList = new ArrayList<String>();        mPictureUrlList = new ArrayList<String>();



For more information, see download source code.


Download source code:Http://download.csdn.net/download/gao_chun/8776533


Reprinted please note.

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.