«

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

时间:2024-3-2 19:04     作者:韩俊     分类: Android


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


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


项目中效果图:



技术点:

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

2.多张图片异步上传


首先来看一下布局文件:

[html] view
plaincopyprint?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&quot;
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f2f2f2" >

&lt;LinearLayout  
    android:id=&quot;@&#43;id/layout_CONTENT&quot;  
    android:layout_width=&quot;match_parent&quot;  
    android:layout_height=&quot;match_parent&quot;  
    android:background=&quot;#f2f2f2&quot;  
    android:orientation=&quot;vertical&quot;  
    android:padding=&quot;5dp&quot; &gt;  

    &lt;!-- 布局由程序动态生成 --&gt;  

    &lt;LinearLayout  
        android:id=&quot;@&#43;id/layout_container&quot;  
        android:layout_width=&quot;300dp&quot;  
        android:layout_height=&quot;wrap_content&quot;  
        android:layout_gravity=&quot;center_horizontal&quot;  
        android:layout_margin=&quot;5dp&quot;  
        android:background=&quot;#cbcbcb&quot;  
        android:orientation=&quot;vertical&quot;  
        android:padding=&quot;0.2px&quot; /&gt;  

    &lt;TextView  
        android:id=&quot;@&#43;id/text_no_data&quot;  
        android:layout_width=&quot;wrap_content&quot;  
        android:layout_height=&quot;wrap_content&quot;  
        android:layout_margin=&quot;20dp&quot;  
        android:text=&quot;@string/text_picture_upload&quot;  
        android:textSize=&quot;16dp&quot; /&gt;  
&lt;/LinearLayout&gt;  

</LinearLayout>



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



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

[html] view
plaincopyprint?

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


[html] view
plaincopyprint?

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





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

[java] view
plaincopyprint?

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);

    //最顶层父布局  
    mLayout = (ViewGroup) findViewById(R.id.layout_container);  

    final int count = 9;    //9&#26684;  
    final int rowCount = (count &#43; 2) / 3;  

    for (int i = 0; i &lt; rowCount; i&#43;&#43;) {  

        if (i != 0) {  
            //加载横向布局线条  
            View.inflate(this, R.layout.layout_line_horizonal, mLayout);  
        }  
        //创建布局对象,设置按下颜色  
        final LinearLayout linearLayout = new LinearLayout(this);  
        linearLayout.setBackgroundResource(R.drawable.row_selector);  

        for (int j = 0; j &lt; 3; j&#43;&#43;) {  

            if (j != 0) {  
                //加载内层纵向布局线条  
                View.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);  
            //添加到linearLayout布局中  
            linearLayout.addView(imageButton, layoutParams);  

            //将imageButton对象添加到列表  
            mImageButtonList.add(imageButton);  
        }  

        DisplayManager manager = DisplayManager.getInstance();  
        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, manager.dipToPixel(100));  
        //将View添加到总父布局  
        mLayout.addView(linearLayout, layoutParams);  
    }  
    //外层设置ImageButton属性  
    final ImageButton currentImageButton = mImageButtonList.get(mCurrent);  
    currentImageButton.setImageResource(R.drawable.ic_add_picture);  
    currentImageButton.setScaleType(ScaleType.CENTER);  
    currentImageButton.setEnabled(true);  

}  




图片上传功能:

[java] view
plaincopyprint?

private class UploadPictureTask extends AsyncTask<List<String>, Integer, String> {

   /* (non-Javadoc) 
    * @see android.os.AsyncTask#doInBackground(Params[]) 
    */  
   @Override  
   protected String doInBackground(List&lt;String&gt;... params) {  
       final List&lt;String&gt; pictureList = params[0];  
       for (int i = 0, len = pictureList.size(); i &lt; len; i&#43;&#43;) {  
           final File file = new File(pictureList.get(i));  
           //final String response = ApacheHttpUtils.post(mUrlPrefix &#43; &quot;/upload&quot;, new File[] {file});  
           // 解析,存储  
           //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);  
   }  

}



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

[java] view
plaincopyprint?

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



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



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

标签: android

热门推荐