**2.**多张图片异步上传
首先来看一下布局文件:
<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:background=“#f2f2f2” >
<LinearLayout
android:id=“@+id/layout_CONTENT”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:background=“#f2f2f2”
android:orientation=“vertical”
android:padding=“5dp” >
<LinearLayout
android:id=“@+id/layout_container”
android:layout_width=“300dp”
android:layout_height=“wrap_content”
android:layout_gravity=“center_horizontal”
android:layout_margin=“5dp”
android:background=“#cbcbcb”
android:orientation=“vertical”
android:padding=“0.2px” />
<TextView
android:id=“@+id/text_no_data”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_margin=“20dp”
android:text=“@string/text_picture_upload”
android:textSize=“16dp” />
布局很简单,主要是id为layout_container的一个LinearLayout作为父布局。
横向的线条和纵向的线条布局也很简单:
<View xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width=“match_parent”
android:layout_height=“1px”
android:background=“#cbcbcb” />
<View xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width=“1px”
android:layout_height=“match_parent”
android:background=“#cbcbcb” />
下面是动态生成布局的实现方式:
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<List, Integer, String> {
/* (non-Javadoc)
- @see android.os.AsyncTask#doInBackground(Params[])
*/
@Override
protected String doInBackground(List… params) {
final List pictureList = params[0];
【附】相关架构及资料
往期Android高级架构资料、源码、笔记、视频。高级UI、性能优化、架构师课程、NDK、混合式开发(ReactNative+Weex)微信小程序、Flutter全方面的Android进阶实践技术,群内还有技术大牛一起讨论交流解决问题。
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门,即可获取!
转存中…(img-5PthnT4Y-1714759485337)]
[外链图片转存中…(img-uFu0mI3K-1714759485339)]
往期Android高级架构资料、源码、笔记、视频。高级UI、性能优化、架构师课程、NDK、混合式开发(ReactNative+Weex)微信小程序、Flutter全方面的Android进阶实践技术,群内还有技术大牛一起讨论交流解决问题。
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门,即可获取!