Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android自定義動態布局 — 多圖片上傳

Android自定義動態布局 — 多圖片上傳

編輯:關於Android編程

Android自定義動態布局 — 多圖片上傳

 

本文介紹Android中動態布局添加圖片,多圖片上傳。

 

項目中效果圖:

height=480 height=480 height=480

 

技術點:

1.動態添加格局中的線條和添加圖片的+號

2.多張圖片異步上傳

 

首先來看一下布局文件:

 

 


布局很簡單,主要是id為layout_container的一個LinearLayout作為父布局。

 

 

 

橫向的線條和縱向的線條布局也很簡單:

 

 

下面是動態生成布局的實現方式:

 

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格
        final int rowCount = (count + 2) / 3;

        for (int i = 0; i < rowCount; i++) {

            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 < 3; j++) {

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

    }


 

圖片上傳功能:

 

 private class UploadPictureTask extends AsyncTask, Integer, String> {

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

注:類中聲明了三個列表去保存之前所操作的記錄

 

 

mImageButtonList = new ArrayList();
        mPicturePathList = new ArrayList();
        mPictureUrlList = new ArrayList();

 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved