├── .idea ├── .name ├── copyright │ └── profiles_settings.xml ├── encodings.xml ├── vcs.xml ├── modules.xml ├── runConfigurations.xml ├── gradle.xml ├── compiler.xml └── misc.xml ├── app ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── values │ │ │ │ ├── strings.xml │ │ │ │ ├── colors.xml │ │ │ │ ├── dimens.xml │ │ │ │ └── styles.xml │ │ │ ├── mipmap-hdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-mdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xxxhdpi │ │ │ │ ├── ic_error.PNG │ │ │ │ ├── ic_holding.png │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xhdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xxhdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── values-w820dp │ │ │ │ ├── attrs.xml │ │ │ │ └── dimens.xml │ │ │ └── layout │ │ │ │ └── activity_main.xml │ │ ├── java │ │ │ └── com │ │ │ │ └── loften │ │ │ │ └── groupiconsample │ │ │ │ ├── groupimageview │ │ │ │ ├── NineGridImageViewAdapter.java │ │ │ │ └── NineGridImageView.java │ │ │ │ ├── utils │ │ │ │ └── CircleImageTransformation.java │ │ │ │ └── MainActivity.java │ │ └── AndroidManifest.xml │ ├── test │ │ └── java │ │ │ └── com │ │ │ └── loften │ │ │ └── groupiconsample │ │ │ └── ExampleUnitTest.java │ └── androidTest │ │ └── java │ │ └── com │ │ └── loften │ │ └── groupiconsample │ │ └── ApplicationTest.java ├── proguard-rules.pro └── build.gradle ├── settings.gradle ├── groupicon.png ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .gitignore ├── README.md ├── gradle.properties ├── gradlew.bat └── gradlew /.idea/.name: -------------------------------------------------------------------------------- 1 | GroupIconSample -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | -------------------------------------------------------------------------------- /groupicon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/myloften/GroupIconSample/HEAD/groupicon.png -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | GroupIconSample 3 | 4 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/myloften/GroupIconSample/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # GroupIconSample 2 | 仿微信群聊头像
3 | 效果如下:
4 | ![image](https://github.com/myloften/GroupIconSample/blob/master/groupicon.png) 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/myloften/GroupIconSample/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/myloften/GroupIconSample/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_error.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/myloften/GroupIconSample/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_error.PNG -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/myloften/GroupIconSample/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/myloften/GroupIconSample/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_holding.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/myloften/GroupIconSample/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_holding.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/myloften/GroupIconSample/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/values-w820dp/attrs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Wed Jul 26 19:50:36 CST 2017 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip 7 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /app/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /app/src/test/java/com/loften/groupiconsample/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.loften.groupiconsample; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * To work on unit tests, switch the Test Artifact in the Build Variants view. 9 | */ 10 | public class ExampleUnitTest { 11 | @Test 12 | public void addition_isCorrect() throws Exception { 13 | assertEquals(4, 2 + 2); 14 | } 15 | } -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/loften/groupiconsample/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.loften.groupiconsample; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 18 | -------------------------------------------------------------------------------- /app/src/main/java/com/loften/groupiconsample/groupimageview/NineGridImageViewAdapter.java: -------------------------------------------------------------------------------- 1 | package com.loften.groupiconsample.groupimageview; 2 | 3 | import android.content.Context; 4 | import android.widget.ImageView; 5 | 6 | /** 7 | * Created by Jaeger on 16/2/24. 8 | * 9 | * Email: chjie.jaeger@gamil.com 10 | * GitHub: https://github.com/laobie 11 | */ 12 | public abstract class NineGridImageViewAdapter { 13 | protected abstract void onDisplayImage(Context context, ImageView imageView, T t); 14 | 15 | protected ImageView generateImageView(Context context){ 16 | ImageView imageView = new ImageView(context); 17 | imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 18 | return imageView; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/loften/Desktop/adt-bundle-mac-x86_64-20140702/sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 23 5 | buildToolsVersion '25.0.0' 6 | 7 | defaultConfig { 8 | applicationId "com.loften.groupiconsample" 9 | minSdkVersion 14 10 | targetSdkVersion 23 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | testCompile 'junit:junit:4.12' 25 | compile 'com.android.support:appcompat-v7:23.3.0' 26 | compile 'com.squareup.picasso:picasso:2.3.2' 27 | } 28 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m 13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 14 | 15 | # When configured, Gradle will run in incubating parallel mode. 16 | # This option should only be used with decoupled projects. More details, visit 17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 18 | # org.gradle.parallel=true -------------------------------------------------------------------------------- /app/src/main/java/com/loften/groupiconsample/utils/CircleImageTransformation.java: -------------------------------------------------------------------------------- 1 | package com.loften.groupiconsample.utils; 2 | 3 | import android.graphics.Bitmap; 4 | import android.graphics.BitmapShader; 5 | import android.graphics.Canvas; 6 | import android.graphics.Matrix; 7 | import android.graphics.Paint; 8 | import android.graphics.RectF; 9 | import android.graphics.Shader; 10 | 11 | import com.squareup.picasso.Transformation; 12 | 13 | /** 14 | * Created by loften on 2017/7/26. 15 | */ 16 | 17 | public class CircleImageTransformation implements Transformation { 18 | 19 | /** 20 | * A unique key for the transformation, used for caching purposes. 21 | */ 22 | private static final String KEY = "circleImageTransformation"; 23 | 24 | @Override 25 | public Bitmap transform(Bitmap source) { 26 | 27 | int minEdge = Math.min(source.getWidth(), source.getHeight()); 28 | int dx = (source.getWidth() - minEdge) / 2; 29 | int dy = (source.getHeight() - minEdge) / 2; 30 | 31 | // Init shader 32 | Shader shader = new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); 33 | Matrix matrix = new Matrix(); 34 | matrix.setTranslate(-dx, -dy); // Move the target area to center of the source bitmap 35 | shader.setLocalMatrix(matrix); 36 | 37 | // Init paint 38 | Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); 39 | paint.setShader(shader); 40 | 41 | // Create and draw circle bitmap 42 | Bitmap output = Bitmap.createBitmap(minEdge, minEdge, source.getConfig()); 43 | Canvas canvas = new Canvas(output); 44 | canvas.drawOval(new RectF(0, 0, minEdge, minEdge), paint); 45 | 46 | // Recycle the source bitmap, because we already generate a new one 47 | source.recycle(); 48 | 49 | return output; 50 | } 51 | 52 | @Override 53 | public String key() { 54 | return KEY; 55 | } 56 | } -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 46 | 47 | 48 | 49 | 50 | 1.8 51 | 52 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 68 | 69 | 70 | 71 | 72 | 73 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 15 | 22 | 29 | 36 | 44 | 52 | 59 | 67 | 75 | 76 | -------------------------------------------------------------------------------- /app/src/main/java/com/loften/groupiconsample/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.loften.groupiconsample; 2 | 3 | import android.content.Context; 4 | import android.support.v7.app.AppCompatActivity; 5 | import android.os.Bundle; 6 | import android.widget.ImageView; 7 | 8 | import com.loften.groupiconsample.groupimageview.NineGridImageView; 9 | import com.loften.groupiconsample.groupimageview.NineGridImageViewAdapter; 10 | import com.loften.groupiconsample.utils.CircleImageTransformation; 11 | import com.squareup.picasso.Picasso; 12 | 13 | import java.util.ArrayList; 14 | import java.util.Arrays; 15 | import java.util.List; 16 | 17 | public class MainActivity extends AppCompatActivity { 18 | 19 | private NineGridImageView groudIcon1,groudIcon2,groudIcon3,groudIcon4,groudIcon5,groudIcon6,groudIcon7,groudIcon8,groudIcon9; 20 | private List mPostList1,mPostList2,mPostList3,mPostList4,mPostList5,mPostList6,mPostList7,mPostList8,mPostList9; 21 | private String[] IMG_URL_LIST = { 22 | "https://pic4.zhimg.com/02685b7a5f2d8cbf74e1fd1ae61d563b_xll.jpg", 23 | "https://pic4.zhimg.com/fc04224598878080115ba387846eabc3_xll.jpg", 24 | "https://pic3.zhimg.com/d1750bd47b514ad62af9497bbe5bb17e_xll.jpg", 25 | "https://pic4.zhimg.com/da52c865cb6a472c3624a78490d9a3b7_xll.jpg", 26 | "https://pic3.zhimg.com/0c149770fc2e16f4a89e6fc479272946_xll.jpg", 27 | "https://pic1.zhimg.com/76903410e4831571e19a10f39717988c_xll.png", 28 | "https://pic3.zhimg.com/33c6cf59163b3f17ca0c091a5c0d9272_xll.jpg", 29 | "https://pic4.zhimg.com/52e093cbf96fd0d027136baf9b5cdcb3_xll.png", 30 | "https://pic3.zhimg.com/f6dc1c1cecd7ba8f4c61c7c31847773e_xll.jpg", 31 | }; 32 | 33 | @Override 34 | protected void onCreate(Bundle savedInstanceState) { 35 | super.onCreate(savedInstanceState); 36 | setContentView(R.layout.activity_main); 37 | 38 | groudIcon1 = (NineGridImageView)findViewById(R.id.groudIcon1); 39 | groudIcon2 = (NineGridImageView)findViewById(R.id.groudIcon2); 40 | groudIcon3 = (NineGridImageView)findViewById(R.id.groudIcon3); 41 | groudIcon4 = (NineGridImageView)findViewById(R.id.groudIcon4); 42 | groudIcon5 = (NineGridImageView)findViewById(R.id.groudIcon5); 43 | groudIcon6 = (NineGridImageView)findViewById(R.id.groudIcon6); 44 | groudIcon7 = (NineGridImageView)findViewById(R.id.groudIcon7); 45 | groudIcon8 = (NineGridImageView)findViewById(R.id.groudIcon8); 46 | groudIcon9 = (NineGridImageView)findViewById(R.id.groudIcon9); 47 | 48 | 49 | mPostList1 = new ArrayList<>(); 50 | mPostList2 = new ArrayList<>(); 51 | mPostList3 = new ArrayList<>(); 52 | mPostList4 = new ArrayList<>(); 53 | mPostList5 = new ArrayList<>(); 54 | mPostList6 = new ArrayList<>(); 55 | mPostList7 = new ArrayList<>(); 56 | mPostList8 = new ArrayList<>(); 57 | mPostList9 = new ArrayList<>(); 58 | 59 | for(int i=0;i<1;i++){ 60 | mPostList1.add(IMG_URL_LIST[i]); 61 | } 62 | for(int i=0;i<2;i++){ 63 | mPostList2.add(IMG_URL_LIST[i]); 64 | } 65 | for(int i=0;i<3;i++){ 66 | mPostList3.add(IMG_URL_LIST[i]); 67 | } 68 | for(int i=0;i<4;i++){ 69 | mPostList4.add(IMG_URL_LIST[i]); 70 | } 71 | for(int i=0;i<5;i++){ 72 | mPostList5.add(IMG_URL_LIST[i]); 73 | } 74 | for(int i=0;i<6;i++){ 75 | mPostList6.add(IMG_URL_LIST[i]); 76 | } 77 | for(int i=0;i<7;i++){ 78 | mPostList7.add(IMG_URL_LIST[i]); 79 | } 80 | for(int i=0;i<8;i++){ 81 | mPostList8.add(IMG_URL_LIST[i]); 82 | } 83 | for(int i=0;i<9;i++){ 84 | mPostList9.add(IMG_URL_LIST[i]); 85 | } 86 | 87 | 88 | NineGridImageViewAdapter mAdapter = new NineGridImageViewAdapter() { 89 | @Override 90 | protected void onDisplayImage(Context context, ImageView imageView, String s) { 91 | //显示图片 92 | Picasso.with(context).load(s).placeholder(R.mipmap.ic_holding).error(R.mipmap.ic_error).into(imageView); 93 | //显示圆形图片 94 | //Picasso.with(context).load(s).transform(new CircleImageTransformation()).placeholder(R.mipmap.ic_holding).error(R.mipmap.ic_error).into(imageView); 95 | } 96 | 97 | @Override 98 | protected ImageView generateImageView(Context context) { 99 | return super.generateImageView(context); 100 | } 101 | }; 102 | groudIcon1.setAdapter(mAdapter); 103 | groudIcon1.setImagesData(mPostList1); 104 | 105 | groudIcon2.setAdapter(mAdapter); 106 | groudIcon2.setImagesData(mPostList2); 107 | 108 | groudIcon3.setAdapter(mAdapter); 109 | groudIcon3.setImagesData(mPostList3); 110 | 111 | groudIcon4.setAdapter(mAdapter); 112 | groudIcon4.setImagesData(mPostList4); 113 | 114 | groudIcon5.setAdapter(mAdapter); 115 | groudIcon5.setImagesData(mPostList5); 116 | 117 | groudIcon6.setAdapter(mAdapter); 118 | groudIcon6.setImagesData(mPostList6); 119 | 120 | groudIcon7.setAdapter(mAdapter); 121 | groudIcon7.setImagesData(mPostList7); 122 | 123 | groudIcon8.setAdapter(mAdapter); 124 | groudIcon8.setImagesData(mPostList8); 125 | 126 | groudIcon9.setAdapter(mAdapter); 127 | groudIcon9.setImagesData(mPostList9); 128 | } 129 | } 130 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /app/src/main/java/com/loften/groupiconsample/groupimageview/NineGridImageView.java: -------------------------------------------------------------------------------- 1 | package com.loften.groupiconsample.groupimageview; 2 | 3 | import android.content.Context; 4 | import android.content.res.TypedArray; 5 | import android.util.AttributeSet; 6 | import android.util.Log; 7 | import android.view.ViewGroup; 8 | import android.widget.ImageView; 9 | 10 | import com.loften.groupiconsample.R; 11 | 12 | import java.util.ArrayList; 13 | import java.util.List; 14 | 15 | /** 16 | * Created by Jaeger on 16/2/24. 17 | * 18 | * Email: chjie.jaeger@gamil.com 19 | * GitHub: https://github.com/laobie 20 | */ 21 | /** 22 | * update by loften on 16/4/21. 23 | */ 24 | public class NineGridImageView extends ViewGroup{ 25 | 26 | private int mRowCount; //行数 27 | private int mColumnCount; //列数 28 | 29 | private int mMaxSize = 9; //最大图片数 30 | private int mGap; //宫格间距 31 | 32 | private int parentWidth;//父组件宽 33 | private int parentHeight;//父组件高 34 | 35 | private List mImageViewList = new ArrayList<>(); 36 | private List mImgDataList; 37 | 38 | private NineGridImageViewAdapter mAdapter; 39 | 40 | public NineGridImageView(Context context) { 41 | this(context,null); 42 | } 43 | 44 | public NineGridImageView(Context context, AttributeSet attrs) { 45 | this(context, attrs, 0); 46 | } 47 | 48 | public NineGridImageView(Context context, AttributeSet attrs, int defStyleAttr) { 49 | super(context, attrs, defStyleAttr); 50 | TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.NineGridImageView); 51 | this.mGap = (int) typedArray.getDimension(R.styleable.NineGridImageView_imgGap, 8); 52 | typedArray.recycle(); 53 | } 54 | 55 | /** 56 | * 设定宽高 57 | */ 58 | @Override 59 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 60 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); 61 | parentWidth = measureWidth(widthMeasureSpec); 62 | parentHeight = measureHeight(heightMeasureSpec); 63 | 64 | setMeasuredDimension(parentWidth,parentHeight); 65 | } 66 | 67 | @Override 68 | protected void onLayout(boolean changed, int l, int t, int r, int b) { 69 | layoutChildrenView(); 70 | } 71 | 72 | /** 73 | * 为子ImageView布局 74 | */ 75 | private void layoutChildrenView(){ 76 | if(mImgDataList == null){ 77 | return; 78 | } 79 | int childrenCount = mImgDataList.size(); 80 | for(int i = 0; i < childrenCount; i++){ 81 | ImageView childrenView = (ImageView)getChildAt(i); 82 | if(mAdapter != null){ 83 | mAdapter.onDisplayImage(getContext(), childrenView, mImgDataList.get(i)); 84 | } 85 | int rowNum = i / mColumnCount;//当前行数 86 | int columnNum = i % mColumnCount;//当前列数 87 | 88 | int mImageSize = (parentWidth-(mColumnCount+1)*mGap)/mColumnCount;//图片尺寸 89 | 90 | int t_center = (parentHeight + mGap)/2;//中间位置以下的顶点(有宫格间距) 91 | int b_center = (parentHeight - mGap)/2;//中间位置以上的底部(有宫格间距) 92 | int l_center = (parentWidth + mGap)/2;//中间位置以右的左部(有宫格间距) 93 | int r_center = (parentWidth - mGap)/2;//中间位置以左的右部(有宫格间距) 94 | int center = (parentHeight - mImageSize)/2;//中间位置以上顶部(无宫格间距) 95 | 96 | int left = mImageSize * columnNum + mGap * (columnNum + 1); 97 | int top = mImageSize * rowNum + mGap * (rowNum + 1); 98 | int right = left + mImageSize; 99 | int bottom = top + mImageSize; 100 | 101 | /** 102 | * 不同子view情况下的不同显示 103 | */ 104 | if(childrenCount == 1){ 105 | childrenView.layout(left, top, right, bottom); 106 | }else if(childrenCount == 2){ 107 | childrenView.layout(left, center, right, center + mImageSize); 108 | }else if(childrenCount == 3){ 109 | if(i == 0){ 110 | childrenView.layout(center, top, center+mImageSize, bottom); 111 | }else { 112 | childrenView.layout(mGap * i +mImageSize * (i - 1), t_center, mGap * i +mImageSize * i, t_center+mImageSize); 113 | } 114 | }else if(childrenCount == 4){ 115 | childrenView.layout(left, top, right, bottom); 116 | }else if(childrenCount == 5){ 117 | if(i == 0){ 118 | childrenView.layout(r_center - mImageSize, r_center - mImageSize, r_center, r_center); 119 | }else if(i == 1){ 120 | childrenView.layout(l_center , r_center - mImageSize, l_center + mImageSize, r_center); 121 | }else{ 122 | childrenView.layout(mGap * (i - 1) + mImageSize * (i - 2),t_center,mGap * (i - 1) + mImageSize * (i - 1),t_center+mImageSize); 123 | } 124 | }else if(childrenCount == 6){ 125 | if(i < 3) { 126 | childrenView.layout(mGap * (i + 1) +mImageSize * i, b_center - mImageSize, mGap * (i + 1) + mImageSize * (i+1), b_center); 127 | }else{ 128 | childrenView.layout(mGap * (i - 2) + mImageSize * (i - 3),t_center,mGap * (i - 2) + mImageSize * (i - 2),t_center+mImageSize); 129 | } 130 | }else if(childrenCount == 7){ 131 | if(i == 0){ 132 | childrenView.layout(center,mGap,center+mImageSize,mGap+mImageSize); 133 | }else if(i > 0 && i < 4){ 134 | childrenView.layout(mGap * i +mImageSize * (i - 1),center,mGap * i +mImageSize * i,center+mImageSize); 135 | }else{ 136 | childrenView.layout(mGap * (i - 3) + mImageSize * (i - 4),t_center+mImageSize/2,mGap * (i - 3) + mImageSize * (i - 3),t_center+mImageSize/2+mImageSize); 137 | } 138 | }else if(childrenCount == 8){ 139 | if(i == 0){ 140 | childrenView.layout(r_center - mImageSize,mGap,r_center,mGap+mImageSize); 141 | }else if(i == 1){ 142 | childrenView.layout(l_center,mGap,l_center+mImageSize,mGap+mImageSize); 143 | }else if(i > 1 && i < 5){ 144 | childrenView.layout(mGap * (i - 1) +mImageSize * (i - 2), center, mGap * (i - 1) +mImageSize * (i - 1), center+mImageSize); 145 | }else{ 146 | childrenView.layout(mGap * (i - 4) + mImageSize * (i - 5), t_center+mImageSize/2, mGap * (i - 4) + mImageSize * (i - 4), t_center+mImageSize/2+mImageSize); 147 | } 148 | }else if(childrenCount == 9){ 149 | childrenView.layout(left, top, right, bottom); 150 | } 151 | } 152 | } 153 | 154 | /** 155 | * 设置图片数据 156 | * 157 | * @param lists 图片数据集合 158 | */ 159 | public void setImagesData(List lists){ 160 | if(lists == null || lists.isEmpty()){ 161 | this.setVisibility(GONE); 162 | return; 163 | }else { 164 | this.setVisibility(VISIBLE); 165 | } 166 | 167 | if(mMaxSize > 0 && lists.size() > mMaxSize){ 168 | lists = lists.subList(0, mMaxSize); 169 | } 170 | 171 | int[] gridParam = calculateGridParam(lists.size()); 172 | mRowCount = gridParam[0]; 173 | mColumnCount = gridParam[1]; 174 | if(mImgDataList == null){ 175 | int i = 0; 176 | while (i < lists.size()){ 177 | ImageView iv = getImageView(i); 178 | if(iv == null){ 179 | return; 180 | } 181 | addView(iv,generateDefaultLayoutParams()); 182 | i++; 183 | } 184 | }else { 185 | int oldViewCount = mImgDataList.size(); 186 | int newViewCount = lists.size(); 187 | if(oldViewCount > newViewCount){ 188 | removeViews(newViewCount, oldViewCount - newViewCount); 189 | }else if(oldViewCount < newViewCount){ 190 | for(int i = oldViewCount; i < newViewCount; i++){ 191 | ImageView iv = getImageView(i); 192 | if(iv == null){ 193 | return; 194 | } 195 | addView(iv, generateDefaultLayoutParams()); 196 | } 197 | } 198 | } 199 | mImgDataList = lists; 200 | requestLayout(); 201 | } 202 | 203 | /** 204 | * 获得 ImageView 205 | * 保证了 ImageView的重用 206 | * 207 | * @param position 位置 208 | */ 209 | private ImageView getImageView(final int position){ 210 | if(position < mImageViewList.size()){ 211 | return mImageViewList.get(position); 212 | }else{ 213 | if(mAdapter != null){ 214 | ImageView imageView = mAdapter.generateImageView(getContext()); 215 | mImageViewList.add(imageView); 216 | return imageView; 217 | }else{ 218 | Log.e("NineGirdImageView", "Your must set a NineGridImageViewAdapter for NineGirdImageView"); 219 | return null; 220 | } 221 | } 222 | } 223 | 224 | /** 225 | * 设置宫格参数 226 | * 227 | * @param imagesSize 图片数量 228 | * @return 宫格参数 gridParam[0] 宫格行数 gridParam[1] 宫格列数 229 | */ 230 | protected static int[] calculateGridParam(int imagesSize){ 231 | int[] gridParam = new int[2]; 232 | if(imagesSize < 3){ 233 | gridParam[0] = 1; 234 | gridParam[1] = imagesSize; 235 | }else if(imagesSize <= 4){ 236 | gridParam[0] = 2; 237 | gridParam[1] = 2; 238 | }else{ 239 | gridParam[0] = imagesSize/3 + (imagesSize % 3 == 0?0:1); 240 | gridParam[1] = 3; 241 | } 242 | return gridParam; 243 | } 244 | 245 | /** 246 | * 设置适配器 247 | * 248 | * @param adapter 适配器 249 | */ 250 | public void setAdapter(NineGridImageViewAdapter adapter){ 251 | mAdapter = adapter; 252 | } 253 | 254 | /** 255 | * 设置宫格间距 256 | * 257 | * @param gap 宫格间距 px 258 | */ 259 | public void setGap(int gap){ 260 | mGap = gap; 261 | } 262 | 263 | /** 264 | * 对宫格的宽高进行重新定义 265 | */ 266 | private int measureWidth(int measureSpec){ 267 | int result = 0; 268 | int specMode = MeasureSpec.getMode(measureSpec); 269 | int specSize = MeasureSpec.getSize(measureSpec); 270 | 271 | if(specMode == MeasureSpec.EXACTLY){ 272 | result = specSize; 273 | }else{ 274 | result = 200; 275 | if(specMode == MeasureSpec.AT_MOST){ 276 | result = Math.min(result,specSize); 277 | } 278 | } 279 | return result; 280 | } 281 | 282 | private int measureHeight(int measureSpec){ 283 | int result = 0; 284 | 285 | int specMode = MeasureSpec.getMode(measureSpec); 286 | int specSize = MeasureSpec.getSize(measureSpec); 287 | 288 | if(specMode == MeasureSpec.EXACTLY){ 289 | result = specSize; 290 | }else{ 291 | result = 200; 292 | if(specMode == MeasureSpec.AT_MOST){ 293 | result = Math.min(result,specSize); 294 | } 295 | } 296 | return result; 297 | } 298 | } 299 | --------------------------------------------------------------------------------