国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android仿微信朋友圈點擊加號添加圖片功能

2019-12-12 03:05:59
字體:
來源:轉載
供稿:網友

本文為大家分享了類似微信朋友圈,點擊+號圖片,可以加圖片功能,供大家參考,具體內容如下

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 viewsint hSpace = Utils.dpToPx(10, getResources());// vertical space among children viewsint vSpace = Utils.dpToPx(10, getResources()); // every child view width and height.int childWidth = 0;int childHeight = 0; // store images res idArrayList<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

@Overrideprotected 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

@Overrideprotected 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默認實現即可。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 焉耆| 芜湖县| 旅游| 蒙城县| 湘潭市| 中江县| 舞阳县| 通江县| 建平县| 温宿县| 谢通门县| 永寿县| 喀喇沁旗| 曲阜市| 蒙山县| 丰镇市| 张家港市| 霍邱县| 达拉特旗| 古蔺县| 五河县| 海原县| 宜兴市| 龙山县| 盐山县| 融水| 长宁区| 白朗县| 泸溪县| 莱西市| 平谷区| 清新县| 云龙县| 启东市| 囊谦县| 民勤县| 鹤壁市| 封丘县| 镇巴县| 新平| 三门县|