Android自己定义动态布局 — 多图片上传
Android自己定义动态布局 — 多图片上传
本文介绍Android中动态布局加入图片,多图片上传。
项目中效果图:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZ2FvX2NodW4=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" width="300" height="480" alt="">
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZ2FvX2NodW4=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" width="300" height="480" alt="">
技术点:
1.动态加入格局中的线条和加入图片的+号
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" />
</LinearLayout> </LinearLayout>
布局非常easy。主要是id为layout_container的一个LinearLayout作为父布局。
横向的线条和纵向的线条布局也非常easy:
<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<String>, Integer, String> {
/* (non-Javadoc)
* @see android.os.AsyncTask#doInBackground(Params[])
*/
@Override
protected String doInBackground(List<String>... params) {
final List<String> 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<ImageButton>();
mPicturePathList = new ArrayList<String>();
mPictureUrlList = new ArrayList<String>();
关于细节大家感兴趣的下载源代码学习吧。
欢迎下载源代码:http://download.csdn.net/download/gao_chun/8776533
转载请注明.
Android自己定义动态布局 — 多图片上传的更多相关文章
- android之使用GridView+仿微信图片上传功能
由于工作要求最近在使用GridView完成图片的批量上传功能,我的例子当中包含仿微信图片上传.拍照.本地选择.相片裁剪等功能,如果有需要的朋友可以看一下,希望我的实际经验能对您有所帮助. 直接上图,下 ...
- Android仿微信图片上传,可以选择多张图片,缩放预览,拍照上传等
仿照微信,朋友圈分享图片功能 .可以进行图片的多张选择,拍照添加图片,以及进行图片的预览,预览时可以进行缩放,并且可以删除选中状态的图片 .很不错的源码,大家有需要可以下载看看 . 微信 微信 微信 ...
- android拍照选择图片上传服务器自定义控件
做android项目的时候总免不了遇到图片上传功能,虽然就是调用android系统的拍照和相册选择功能,但是总面部了把一大推代码写在activity里,看上去一大推代码头都昏了.不如把这些功能都集成一 ...
- 利用WCF与Android实现图片上传并传参
利用WCF与Android实现图片上传并传参 最近做一个项目后端使用WCF接收Android手机拍照并带其它参数保存到服务器里:刚好把最近学习的WCF利用上,本以为是个比较简单的功能应该很好实现,没想 ...
- Android开发中使用七牛云存储进行图片上传下载
Android开发中的图片存储本来就是比较耗时耗地的事情,而使用第三方的七牛云,便可以很好的解决这些后顾之忧,最近我也是在学习七牛的SDK,将使用过程在这记录下来,方便以后使用. 先说一下七牛云的存储 ...
- LocalResizeIMG前端HTML5本地压缩图片上传,兼容移动设备IOS,android
LocalResizeIMG前端HTML5本地压缩图片上传,兼容移动设备IOS,android jincon 发表于 2015-02-26 18:31:01 发表在: php开发 localresiz ...
- Android学习笔记进阶之在图片上涂鸦(能清屏)
Android学习笔记进阶之在图片上涂鸦(能清屏) 2013-11-19 10:52 117人阅读 评论(0) 收藏 举报 HandWritingActivity.java package xiaos ...
- 【咸鱼教程】本地图片上传。动态GIF表情图生成
本案例参考:http://emoji.decathlon.trustingme.cn/但是实现方式不一样. 教程目录一 head first二 打开本地图片功能三 拖拽和缩放手势,调整图片四 gifj ...
- Android图片上传(头像裁切+原图原样)
下面简单铺一下代码: (一)头像裁切.上传服务器(代码) 这里上边的按钮是头像的点击事件,弹出底部的头像选择框,下边的按钮跳到下个页面,进行原图上传. ? 1 2 3 4 5 6 7 8 9 10 1 ...
随机推荐
- nyoj--12--喷水装置(二)(区间覆盖问题+贪心)
喷水装置(二) 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 有一块草坪,横向长w,纵向长为h,在它的橫向中心线上不同位置处装有n(n<=10000)个点状的喷水 ...
- Nginx搭建图片服务器
Nginx搭建图片服务器 标签(空格分隔): linux,nginx Nginx常用命令 ./nginx 启动 ./nginx -s reload 重载配置文件 ./nginx -s stop|sta ...
- 使用Onedrive
买了个某捷的大硬盘,于是发现比较坑,非要用云存储才能获得额外200G.于是费了一个多小时.不过学习到了很多. 首先硬盘下会给出对应的exe文件,点击运行就可以. 之后注册希捷的账号,以及微软的账号. ...
- hiho147周 - 数据结构 bitset
题目链接 n个5维数,对于每个数,输出小于他的数的个数(每个维度都比他小); #include <cstdio> #include <cstring> #include < ...
- centos7 初始化安装
CENTOS7 初装 一.分区 挂载路径 格式 容量 / xfs 102400 swap 等同内存大小 /home xfs 剩余 二.时区 Asia/Shanghai 三.安装包选择 选择最小化安装 ...
- BZOJ 3639: Query on a tree VII LCT_set维护子树信息
用 set 维护子树信息,细节较多. Code: #include <cstring> #include <cstdio> #include <algorithm> ...
- php八大设计模式之工厂模式
简单点来说,就是用到什么类,就去实例化对应的类.比如:php 可能连接 mysql,可能连接 sqlserver,也可能是 oracle 数据库,可以动态的去链接. 书籍<php权威编程> ...
- Tp5 的 validate 自动验证
tp5自带的验证功能: 用法之一: $validate = new \think\Validate([ ['name', 'require|alphaDash', '用户名不能为空|用户名格式只能是字 ...
- 超链接:a标签
a标签的功能:实现跳转功能 a标签的重要属性:href,target href的值为跳转目标的地址,如果是跳转页面的话,需要这个页面的超链接. target的值有四个:_blank._self._pa ...
- 转移顺序的艺术 luogu4394 + lougu2966 + luogu3537
lougu4394: N个政党要组成一个联合内阁,每个党都有自己的席位数. 现在希望你找出一种方案,你选中的党的席位数要大于总数的一半,并且联合内阁的席位数越多越好. 对于一个联合内阁,如果某个政党退 ...