編輯:關於Android編程
Android自定義動態布局 — 多圖片上傳
本文介紹Android中動態布局添加圖片,多圖片上傳。
項目中效果圖:
技術點:
1.動態添加格局中的線條和添加圖片的+號
2.多張圖片異步上傳
首先來看一下布局文件:
橫向的線條和縱向的線條布局也很簡單:
下面是動態生成布局的實現方式:
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 ();
在之前文章的鋪墊下,再為大家分享一篇:Android手勢密碼,附源碼下載,不要錯過。源碼下載:http://xiazai.jb51.net/201610/yuanma/a
在Android應用的開發中,ListView是最常用的一個列表顯示控件,微博,聯系人啊,反正只要涉及多數據展示的情況,都會用到ListView(另外就是GridView
本文實例講述了Android編程之簡單計時器實現方法。分享給大家供大家參考,具體如下:這裡利用ContextMenu(上下文菜單),Chronometer實現簡單計數器。
自從2009年第一個Android手機病毒誕生以來,已經有越來越多的Android病毒和木馬充斥在網絡。2011年全年新增手機惡意軟件及木馬8714個,被感