最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android仿微信朋友圈點(diǎn)擊加號(hào)添加圖片功能

 更新時(shí)間:2017年04月14日 10:26:02   作者:ganchuanpu  
這篇文章主要為大家詳細(xì)介紹了Android仿微信朋友圈點(diǎn)擊加號(hào)添加圖片功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文為大家分享了類似微信朋友圈,點(diǎn)擊+號(hào)圖片,可以加圖片功能,供大家參考,具體內(nèi)容如下

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三個(gè)一排,而且都是正方形,所以我們上面通過(guò)循環(huán)很好去得到所有子View的位置,注意我們上面把子View的左上角坐標(biāo)存儲(chǔ)到我們自定義的LayoutParams 的left和top二個(gè)字段中,Layout階段會(huì)使用,最后我們算得整個(gè)ViewGroup的寬高,調(diào)用setMeasuredDimension設(shè)置?! ?/p>

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();
}

  最核心的就是調(diào)用layout方法,根據(jù)我們measure階段獲得的LayoutParams中的left和top字段,也很好對(duì)每個(gè)子View進(jìn)行位置排列。然后判斷在圖片未達(dá)到最大值9張時(shí),默認(rèn)最后一張是+號(hào)圖片,然后設(shè)置點(diǎn)擊事件,彈出對(duì)話框供用戶選擇操作。

Draw

不需要重寫(xiě),使用ViewGroup默認(rèn)實(shí)現(xiàn)即可。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

鹤壁市| 泾阳县| 左贡县| 讷河市| 辉县市| 栾川县| 江北区| 海晏县| 昂仁县| 蒲城县| 那曲县| 阿克陶县| 灯塔市| 穆棱市| 呼伦贝尔市| 临沭县| 即墨市| 蓝山县| 余姚市| 克什克腾旗| 封开县| 延津县| 江城| 怀安县| 江川县| 博白县| 鹤岗市| 英吉沙县| 丰顺县| 姜堰市| 蓝田县| 镇平县| 亳州市| 汉中市| 广昌县| 金昌市| 高安市| 永登县| 宁海县| 通渭县| 如东县|