Android dynamic Add picture line wrap

Source: Internet
Author: User

<strong> near the project to use to dynamically add pictures, and then line-wrapping implementation. Just started to use the GridView, but useless, what reason to forget. Let me take a look at my implementation. </strong>
<strong></strong>

Look at the code xml:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" match_parent "    android:o rientation= "vertical" >    <button        android:id= "@+id/button1"        android:layout_width= "Wrap_content"        android:layout_height= "wrap_content"        android:text= "@string/sel_pic_6"/>    <linearlayout        Android:id= "@+id/linearlayout"        android:layout_width= "match_parent"        android:layout_height= "Wrap_ Content "        android:orientation=" vertical ">    </LinearLayout></LinearLayout>

Activity:

Package Org.cn.ruimit.pic;import Java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.util.arraylist;import Org.cn.ruimit.r;import Android.content.intent;import Android.database.Cursor;import Android.graphics.bitmapfactory;import Android.net.uri;import Android.os.bundle;import Android.provider.MediaStore ; Import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.imageview;import Android.widget.imageview.scaletype;import Android.widget.linearlayout;import Android.widget.linearlayout.layoutparams;import Com.china.hunbohui.baseactivity;import com.china.hunbohui.iapplication;/** * @author wangjing */public class Addpicaty extends Baseactivity {private static FINA l int sel_pic = 1;private LinearLayout linearlayout = null;private arraylist<imagebean> imageBeans;@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.add_pic_lay); Initview ();} private void Initview () {Imagebeans = new arraylist<addpicaty.imagebean> (); linearlayout = (linearlayout) Findviewbyid (r.id.linearlayout); Findviewbyid (R.id.button1). Setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {// Local album selection Intent intent = new Intent (intent.action_get_content); "Android.intent.action.GET_CONTENT"//Intent.addcategory (intent.category_openable); Intent.settype ("image/*"); Startactivityforresult (Intent, sel_pic);});} @Overrideprotected void Onactivityresult (int requestcode, int resultcode, Intent data) {Super.onactivityresult ( Requestcode, ResultCode, data); if (ResultCode = = RESULT_OK) {if (Requestcode = = sel_pic) {uri uri = Data.getdata (); String[] proj = {MediaStore.Images.Media.DATA}; cursor cursor = managedquery (URI, proj, NULL, NULL, NULL);//According to my own understanding this is the index value of the picture that gets the user's choice int column_index = Cursor.getcolumnind Exorthrow (MediaStore.Images.Media.DATA);//move the cursor to the beginning, this is very important, it is easy to cause the cross-border Cursor.movetofirst ();//finally get the picture path based on the index value string Path = cursor.getstring (column_index); Imagebean bean = New Imagebean (); Bean.setpath (path); Imagebeans.add (bean); UpdateLayout ();}}}  /** * Update picture layout */private void UpdateLayout () {LinearLayout ll_horizontal = Null;linearlayout.removeallviews (); for (int i = 0; I < imagebeans.size (); i++) {if (i%4 = = 0) {ll_horizontal = new LinearLayout (context); ll_horizontal.setorientation (linearlayout.horizontal); Linearlayout.addview (ll_horizontal);} ImageView img = new ImageView (context); Setimglayoutparams (IMG); Setimagebitmap (IMG, Imagebeans.get (i). GetPath ()); Ll_ Horizontal.addview (IMG);}} /** * Set ImageView size */private void Setimglayoutparams (ImageView img) {//Layoutparams LP = (layoutparams) img.getlayoutpa Rams (); Layoutparams LP = new Layoutparams (0, 0); lp.width = (int) ((iapplication.with-20 * iapplication.dencity-3 * 5 * iappli cation.dencity)/4); lp.height = Lp.width;lp.rightmargin = (int) (5 * iapplication.dencity); lp.bottommargin = (int) (5 * I application.dencity); img.setlayoutparams (LP); Img.setscaletype (Scaletype.center_crop);} /** * Set picture for ImageView */private void Setimagebitmap (ImageView img,string url) {try {FileInputStream FIS = new FileInputStream (URL);     Img.setimagebitmap (Bitmapfactory.decodestream (FIS));     } catch (FileNotFoundException e) {e.printstacktrace (); }}/** * Picture Object */class Imagebean {private string Path;public string GetPath () {return path;} public void SetPath (String path) {this.path = path;}}}

Main learning points: the size of the picture set, the layout of the two pieces of the update.






Android dynamic Add picture line wrap

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.