类似微信朋友圈,点击+号图片,可以加图片
微信 图片 可以 点击 类似 朋友圈
2023-09-11 14:20:27 时间
xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="40dp" android:orientation="vertical" > <com.sw.demo.widget.NinePhotoView android:id="@+id/photoview" android:layout_width="match_parent" android:layout_height="wrap_content" app:ninephoto_hspace="10dp" app:ninephoto_vspace="10dp" app:rainbowbar_color="@android:color/holo_blue_bright" > </com.sw.demo.widget.NinePhotoView>
NinePhotoView.java
public class NinePhotoView extends ViewGroup { public static final int MAX_PHOTO_NUMBER = 9; private int[] constImageIds = { R.drawable.girl_0, R.drawable.girl_1, R.drawable.girl_2, R.drawable.girl_3, R.drawable.girl_4, R.drawable.girl_5, R.drawable.girl_6, R.drawable.girl_7, R.drawable.girl_8 }; // horizontal space among children views int hSpace = Utils.dpToPx(10, getResources()); // vertical space among children views int vSpace = Utils.dpToPx(10, getResources()); // every child view width and height. int childWidth = 0; int childHeight = 0; // store images res id ArrayList<integer> mImageResArrayList = new ArrayList<integer>(9); private View addPhotoView; public NinePhotoView(Context context) { super(context); } public NinePhotoView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public NinePhotoView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); TypedArray t = context.obtainStyledAttributes(attrs, R.styleable.NinePhotoView, 0, 0); hSpace = t.getDimensionPixelSize( R.styleable.NinePhotoView_ninephoto_hspace, hSpace); vSpace = t.getDimensionPixelSize( R.styleable.NinePhotoView_ninephoto_vspace, vSpace); t.recycle(); addPhotoView = new View(context); addView(addPhotoView); mImageResArrayList.add(new integer()); }
Measure
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int rw = MeasureSpec.getSize(widthMeasureSpec); int rh = MeasureSpec.getSize(heightMeasureSpec); childWidth = (rw - 2 * hSpace) / 3; childHeight = childWidth; int childCount = this.getChildCount(); for (int i = 0; i < childCount; i++) { View child = this.getChildAt(i); //this.measureChild(child, widthMeasureSpec, heightMeasureSpec); LayoutParams lParams = (LayoutParams) child.getLayoutParams(); lParams.left = (i % 3) * (childWidth + hSpace); lParams.top = (i / 3) * (childWidth + vSpace); } int vw = rw; int vh = rh; if (childCount < 3) { vw = childCount * (childWidth + hSpace); } vh = ((childCount + 3) / 3) * (childWidth + vSpace); setMeasuredDimension(vw, vh); }
我们的子View三个一排,而且都是正方形,所以我们上面通过循环很好去得到所有子View的位置,注意我们上面把子View的左上角坐标存储到我们自定义的LayoutParams 的left和top二个字段中,Layout阶段会使用,最后我们算得整个ViewGroup的宽高,调用setMeasuredDimension设置。
Layout
@Override protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) { int childCount = this.getChildCount(); for (int i = 0; i < childCount; i++) { View child = this.getChildAt(i); LayoutParams lParams = (LayoutParams) child.getLayoutParams(); child.layout(lParams.left, lParams.top, lParams.left + childWidth, lParams.top + childHeight); if (i == mImageResArrayList.size() - 1 && mImageResArrayList.size() != MAX_PHOTO_NUMBER) { child.setBackgroundResource(R.drawable.add_photo); child.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { addPhotoBtnClick(); } }); }else { child.setBackgroundResource(constImageIds[i]); child.setOnClickListener(null); } } } public void addPhoto() { if (mImageResArrayList.size() < MAX_PHOTO_NUMBER) { View newChild = new View(getContext()); addView(newChild); mImageResArrayList.add(new integer()); requestLayout(); invalidate(); } } public void addPhotoBtnClick() { final CharSequence[] items = { "Take Photo", "Photo from gallery" }; AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); builder.setItems(items, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { addPhoto(); } }); builder.show(); }
最核心的就是调用layout方法,根据我们measure阶段获得的LayoutParams中的left和top字段,也很好对每个子View进行位置排列。然后判断在图片未达到最大值9张时,默认最后一张是+号图片,然后设置点击事件,弹出对话框供用户选择操作。
Draw
不需要重写,使用ViewGroup默认实现即可。
![NiewPhotoView.gif](http://upload-images.jianshu.io/upload_images/188580-302a72eeced57908.gif?imageView2/2/w/1240/q/100)
相关文章
- 微信小程序 - 图片懒加载
- 微信小程序-上传多张图片加进度条(支持预览、删除)
- 微信小程序轮子 - 瀑布流布局+数据懒加载(可替换图片瀑布流懒加载 / 视频瀑布流懒加载等等)
- 微信小程序 - 引入并使用 Fly.js 请求库(超级详细的教程及运行示例)提供 Fly.js 源码源文件下载,贴心的配置示例及注释,优雅快速的发起 http 网络请求
- 微信小程序 - 解决 rich-text 富文本解析图片无法自适应宽高问题(图片超出屏幕宽度且不受控)
- 微信小程序 - requestSubscribeMessage:fail can only be invoked by user TAP gesture.
- 微信小程序 - “本地资源图片无法通过WXSS 获取,可以使用网络图片,或者 base64,或者使用标签” 解决
- 微信小程序 - <image>图片 src 路径动态拼接多个变量
- 微信小程序使用 web-view 微信PC端不显示问题
- 微信小程序canvas在其他图片上画圆形图片
- 微信小程序轮播组件
- 微信小程序 - 相对定位和绝对定位 - 相对路径和绝对路径
- 一个Accecc_Token生成和缓存和读取类,微信/小程序开发必须学
- 《移动App测试的22条军规》——第23章,第10节测试微信App对于多种格式图片的支持
- 微信小程序项目实战之豆瓣天气
- 微信网页分享标题图片自定义设置(最新)
- 【微信小程序/实现】怎么设置背景图
- 微信小程序图片上传(土豆温床)
- 微信小程序使用本地图片在真机不显示的问题
- 微信小程序~基础组件
- 微信小程序图片宽度100%,高度自适应
- 微信小程序生成指定页面小程序码海报图片分享思路总结
- TP5结合微信公众号学习笔记一(入门篇)
- 微信小程序插入背景图片(将图片转为base64)
- 微信小程序不行了?连接应用场景面临挑战
- 微信小程序中短信验证码登录全流程及代码