Android自定义动态布局 — 多图片上传(记录一下,以后可能会用到)

Android自定义动态布局 — 多图片上传


本文介绍Android中动态布局添加图片,多图片上传。


项目中效果图:

  


技术点:

1.动态添加格局中的线条和添加图片的+号

2.多张图片异步上传


首先来看一下布局文件:

[html]  view plain copy print ?
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="match_parent"  
  4.     android:background="#f2f2f2" >  
  5.   
  6.     <LinearLayout  
  7.         android:id="@+id/layout_CONTENT"  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="match_parent"  
  10.         android:background="#f2f2f2"  
  11.         android:orientation="vertical"  
  12.         android:padding="5dp" >  
  13.   
  14.         <!-- 布局由程序动态生成 -->  
  15.   
  16.         <LinearLayout  
  17.             android:id="@+id/layout_container"  
  18.             android:layout_width="300dp"  
  19.             android:layout_height="wrap_content"  
  20.             android:layout_gravity="center_horizontal"  
  21.             android:layout_margin="5dp"  
  22.             android:background="#cbcbcb"  
  23.             android:orientation="vertical"  
  24.             android:padding="0.2px" />  
  25.   
  26.         <TextView  
  27.             android:id="@+id/text_no_data"  
  28.             android:layout_width="wrap_content"  
  29.             android:layout_height="wrap_content"  
  30.             android:layout_margin="20dp"  
  31.             android:text="@string/text_picture_upload"  
  32.             android:textSize="16dp" />  
  33.     </LinearLayout>  
  34.   
  35. </LinearLayout>  

布局很简单,主要是id为layout_container的一个LinearLayout作为父布局。



横向的线条和纵向的线条布局也很简单:

[html]  view plain copy print ?
  1. <View xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="1px"  
  4.     android:background="#cbcbcb" />  

[html]  view plain copy print ?
  1. <View xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="1px"  
  3.     android:layout_height="match_parent"  
  4.     android:background="#cbcbcb" />  



下面是动态生成布局的实现方式:

[java]  view plain copy print ?
  1. private void initUI() {  
  2.         setContentView(R.layout.activity_main);  
  3.         //setTitle(R.string.button_service_upload_picture);  
  4.         //showBackwardView(R.string.button_backward, true);  
  5.         //showForwardView(R.string.button_upload,true);  
  6.   
  7.         //最顶层父布局  
  8.         mLayout = (ViewGroup) findViewById(R.id.layout_container);  
  9.   
  10.         final int count = 9;    //9格  
  11.         final int rowCount = (count + 2) / 3;  
  12.   
  13.         for (int i = 0; i < rowCount; i++) {  
  14.   
  15.             if (i != 0) {  
  16.                 //加载横向布局线条  
  17.                 View.inflate(this, R.layout.layout_line_horizonal, mLayout);  
  18.             }  
  19.             //创建布局对象,设置按下颜色  
  20.             final LinearLayout linearLayout = new LinearLayout(this);  
  21.             linearLayout.setBackgroundResource(R.drawable.row_selector);  
  22.   
  23.             for (int j = 0; j < 3; j++) {  
  24.   
  25.                 if (j != 0) {  
  26.                     //加载内层纵向布局线条  
  27.                     View.inflate(this, R.layout.layout_line_vertical, linearLayout);  
  28.                 }  
  29.   
  30.                 ImageButton imageButton = new ImageButton(this);  
  31.                 imageButton.setBackgroundResource(R.drawable.row_selector);  
  32.                 imageButton.setTag(TAG);  
  33.                 imageButton.setOnClickListener(this);  
  34.                 imageButton.setEnabled(false);  
  35.                 LinearLayout.LayoutParams layoutParams =  
  36.                         new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f);  
  37.                 //添加到linearLayout布局中  
  38.                 linearLayout.addView(imageButton, layoutParams);  
  39.   
  40.                 //将imageButton对象添加到列表  
  41.                 mImageButtonList.add(imageButton);  
  42.             }  
  43.   
  44.             DisplayManager manager = DisplayManager.getInstance();  
  45.             LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, manager.dipToPixel(100));  
  46.             //将View添加到总父布局  
  47.             mLayout.addView(linearLayout, layoutParams);  
  48.         }  
  49.         //外层设置ImageButton属性  
  50.         final ImageButton currentImageButton = mImageButtonList.get(mCurrent);  
  51.         currentImageButton.setImageResource(R.drawable.ic_add_picture);  
  52.         currentImageButton.setScaleType(ScaleType.CENTER);  
  53.         currentImageButton.setEnabled(true);  
  54.   
  55.     }  


图片上传功能:

[java]  view plain copy print ?
  1. private class UploadPictureTask extends AsyncTask<List<String>, Integer, String> {  
  2.   
  3.        /* (non-Javadoc) 
  4.         * @see android.os.AsyncTask#doInBackground(Params[]) 
  5.         */  
  6.        @Override  
  7.        protected String doInBackground(List<String>... params) {  
  8.            final List<String> pictureList = params[0];  
  9.            for (int i = 0, len = pictureList.size(); i < len; i++) {  
  10.                final File file = new File(pictureList.get(i));  
  11.                //final String response = ApacheHttpUtils.post(mUrlPrefix + "/upload", new File[] {file});  
  12.                // 解析,存储  
  13.                //final UploadInfo upload = new UploadParser().parse(response).getData();  
  14.                /*if (upload != null) { 
  15.                    final String url = upload.getUrl(); 
  16.                    if (url != null) { 
  17.                        mPictureUrlList.add(url); 
  18.                    } 
  19.                }*/  
  20.                publishProgress(i);  
  21.            }  
  22.            return null;  
  23.        }  
  24.   
  25.        /* (non-Javadoc) 
  26.         * @see android.os.AsyncTask#onProgressUpdate(Progress[]) 
  27.         */  
  28.        @Override  
  29.        protected void onProgressUpdate(Integer... values) {  
  30.   
  31.        }  
  32.   
  33.        /* (non-Javadoc) 
  34.         * @see android.os.AsyncTask#onPostExecute(java.lang.Object) 
  35.         */  
  36.        @Override  
  37.        protected void onPostExecute(String result) {  
  38.            //addPictures();  
  39.            super.onPostExecute(result);  
  40.        }  
  41.    }  

注:类中声明了三个列表去保存之前所操作的记录

[java]  view plain copy print ?
  1. mImageButtonList = new ArrayList<ImageButton>();  
  2.         mPicturePathList = new ArrayList<String>();  
  3.         mPictureUrlList = new ArrayList<String>();  



关于细节大家感兴趣的下载源码学习吧。


欢迎下载源码:http://download.csdn.net/download/gao_chun/8776533


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值