欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

本文為大家分享了類似微信朋友圈,點擊+號圖片,可以加圖片功能,供大家參考,具體內(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三個一排,而且都是正方形,所以我們上面通過循環(huán)很好去得到所有子View的位置,注意我們上面把子View的左上角坐標存儲到我們自定義的LayoutParams 的left和top二個字段中,Layout階段會使用,最后我們算得整個ViewGroup的寬高,調(diào)用setMeasuredDimension設(shè)置。  

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字段,也很好對每個子View進行位置排列。然后判斷在圖片未達到最大值9張時,默認最后一張是+號圖片,然后設(shè)置點擊事件,彈出對話框供用戶選擇操作。

Draw

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

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

相關(guān)文章

  • Android控件之使用ListView實現(xiàn)時間軸效果

    Android控件之使用ListView實現(xiàn)時間軸效果

    這篇文章主要介紹了Android基礎(chǔ)控件之使用ListView實現(xiàn)時間軸效果的相關(guān)資料,本文是以查看物流信息為例,給大家介紹了listview時間軸的實現(xiàn)代碼,需要的朋友可以參考下
    2016-11-11
  • 使用Flutter定位包獲取地理位置

    使用Flutter定位包獲取地理位置

    本文詳細講解了使用Flutter定位包獲取地理位置的方法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-11-11
  • Android ViewModel的作用深入講解

    Android ViewModel的作用深入講解

    這篇文章主要介紹了Android ViewModel的作用,ViewModel類旨在以注重生命周期的方式存儲和管理界面相關(guān)數(shù)據(jù),ViewModel類讓數(shù)據(jù)可在發(fā)生屏幕旋轉(zhuǎn)等配置更改后繼續(xù)留存,需要詳細了解可以參考下文
    2023-05-05
  • Android仿淘寶預(yù)訂日歷(18)

    Android仿淘寶預(yù)訂日歷(18)

    這篇文章主要為大家詳細介紹了Android仿淘寶預(yù)訂日歷的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-02-02
  • Android實現(xiàn)屏幕鎖定源碼詳解

    Android實現(xiàn)屏幕鎖定源碼詳解

    本篇文章主要介紹了Android實現(xiàn)屏幕鎖定源碼詳解,屏幕鎖定是一個很有用的功能,有需要的可以了解一下。
    2016-10-10
  • Android 斷點下載和自動安裝的示例代碼

    Android 斷點下載和自動安裝的示例代碼

    本篇文章主要介紹了Android斷點下載和自動安裝的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-01-01
  • Android binder 匿名服務(wù)實現(xiàn)雙向通信的解決方案

    Android binder 匿名服務(wù)實現(xiàn)雙向通信的解決方案

    這篇文章主要介紹了Android binder 匿名服務(wù)實現(xiàn)雙向通信的解決方案,當然,這種方案是可行的,只是需要client和server都向servicemanager注冊一個服務(wù),實現(xiàn)起來有點麻煩,不太建議這么做,需要的朋友可以參考下
    2024-04-04
  • android橫豎屏切換時候Activity的生命周期

    android橫豎屏切換時候Activity的生命周期

    曾經(jīng)遇到過一個面試題,讓你寫出橫屏切換豎屏Activity的生命周期?,F(xiàn)在給大家分析一下他切換時具體的生命周期是怎么樣的
    2013-01-01
  • Android編程設(shè)計模式之策略模式詳解

    Android編程設(shè)計模式之策略模式詳解

    這篇文章主要介紹了Android編程設(shè)計模式之策略模式,結(jié)合實例形式詳細分析了Android策略模式的概念、原理、實現(xiàn)方法及相關(guān)注意事項,需要的朋友可以參考下
    2017-12-12
  • 安卓Android6.0權(quán)限動態(tài)獲取操作示例

    安卓Android6.0權(quán)限動態(tài)獲取操作示例

    這篇文章主要介紹了安卓Android6.0權(quán)限動態(tài)獲取操作,結(jié)合實例形式分析了Android6.0針對權(quán)限的動態(tài)獲取、授權(quán)等相關(guān)操作技巧,需要的朋友可以參考下
    2018-02-02

最新評論