├── .gitignore ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── cn │ │ └── gavinliu │ │ └── demo │ │ └── beautifulofroundedbitmap │ │ └── ApplicationTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── cn │ │ │ └── gavinliu │ │ │ └── demo │ │ │ └── beautifulofroundedbitmap │ │ │ └── MainActivity.java │ └── res │ │ ├── drawable │ │ └── an.jpg │ │ ├── layout │ │ └── activity_main.xml │ │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ └── ic_launcher.png │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── cn │ └── gavinliu │ └── demo │ └── beautifulofroundedbitmap │ └── ExampleUnitTest.java ├── build.gradle └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | #Android generated 2 | bin 3 | gen 4 | gen* 5 | 6 | #Eclipse 7 | .project 8 | .classpath 9 | .settings 10 | 11 | #IntelliJ IDEA 12 | .idea 13 | *.iml 14 | *.ipr 15 | *.iws 16 | out 17 | 18 | #Maven 19 | target 20 | release.properties 21 | pom.xml.* 22 | 23 | #Ant 24 | build.xml 25 | local.properties 26 | proguard.cfg 27 | 28 | #Gradle 29 | .gradle 30 | build 31 | 32 | #OSX 33 | .DS_Store 34 | 35 | #Personal Files 36 | signing. 37 | 38 | 39 | # Built application files 40 | *.apk 41 | *.ap_ 42 | 43 | # Files for the Dalvik VM 44 | *.dex 45 | 46 | # Java class files 47 | *.class 48 | 49 | # Generated files 50 | bin/ 51 | gen/ 52 | 53 | # Gradle files 54 | .gradle/ 55 | build/ 56 | 57 | # Local configuration file (sdk path, etc) 58 | local.properties 59 | 60 | # Proguard folder generated by Eclipse 61 | proguard/ 62 | 63 | # Log Files 64 | *.log 65 | 66 | gradle.properties 67 | gradle/ 68 | gradlew 69 | gradlew.bat -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # BeautifulOfRoundedBitmap 2 | 3 | [Android - 实现图片圆角显示的几种方式](http://gavinliu.cn/2016/04/12/Android-实现图片圆角显示的几种方式/)博客 Demo 4 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 23 5 | buildToolsVersion "23.0.3" 6 | 7 | defaultConfig { 8 | applicationId "cn.gavinliu.demo.beautifulofroundedbitmap" 9 | minSdkVersion 8 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 | } 27 | -------------------------------------------------------------------------------- /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 /home/gavin/Develop/android-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 | -------------------------------------------------------------------------------- /app/src/androidTest/java/cn/gavinliu/demo/beautifulofroundedbitmap/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package cn.gavinliu.demo.beautifulofroundedbitmap; 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 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /app/src/main/java/cn/gavinliu/demo/beautifulofroundedbitmap/MainActivity.java: -------------------------------------------------------------------------------- 1 | package cn.gavinliu.demo.beautifulofroundedbitmap; 2 | 3 | import android.graphics.Bitmap; 4 | import android.graphics.BitmapFactory; 5 | import android.graphics.Canvas; 6 | import android.graphics.Paint; 7 | import android.graphics.Path; 8 | import android.graphics.PorterDuff; 9 | import android.graphics.PorterDuffXfermode; 10 | import android.graphics.Rect; 11 | import android.graphics.RectF; 12 | import android.graphics.Region; 13 | import android.os.AsyncTask; 14 | import android.os.Bundle; 15 | import android.support.v7.app.AppCompatActivity; 16 | import android.widget.ImageView; 17 | 18 | public class MainActivity extends AppCompatActivity { 19 | 20 | int imageSize, radius; 21 | 22 | ImageView imageView; 23 | ImageView imageView2; 24 | 25 | @Override 26 | protected void onCreate(Bundle savedInstanceState) { 27 | super.onCreate(savedInstanceState); 28 | setContentView(R.layout.activity_main); 29 | 30 | imageView = (ImageView) findViewById(R.id.image); 31 | imageView2 = (ImageView) findViewById(R.id.image2); 32 | 33 | imageSize = getResources().getDimensionPixelSize(R.dimen.image_size); 34 | radius = getResources().getDimensionPixelSize(R.dimen.radius); 35 | 36 | new LoadTask1().execute(); 37 | new LoadTask2().execute(); 38 | } 39 | 40 | class LoadTask1 extends AsyncTask { 41 | 42 | @Override 43 | protected Bitmap doInBackground(Void... params) { 44 | Bitmap result = Bitmap.createBitmap(imageSize, imageSize, Bitmap.Config.ARGB_8888); 45 | Canvas canvas = new Canvas(result); 46 | 47 | Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.an); 48 | 49 | final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 50 | final RectF rectF = new RectF(0, 0, imageSize, imageSize); 51 | 52 | Paint paint = new Paint(); 53 | paint.setAntiAlias(true); 54 | paint.setFilterBitmap(true); 55 | 56 | paint.setXfermode(null); 57 | canvas.drawRoundRect(rectF, radius, radius, paint); 58 | paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); 59 | 60 | canvas.drawBitmap(bitmap, rect, rectF, paint); 61 | return result; 62 | } 63 | 64 | @Override 65 | protected void onPostExecute(Bitmap bitmap) { 66 | imageView.setImageBitmap(bitmap); 67 | } 68 | } 69 | 70 | class LoadTask2 extends AsyncTask { 71 | 72 | @Override 73 | protected Bitmap doInBackground(Void... params) { 74 | Bitmap result = Bitmap.createBitmap(imageSize, imageSize, Bitmap.Config.ARGB_8888); 75 | Canvas canvas = new Canvas(result); 76 | 77 | Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.an); 78 | 79 | final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 80 | final RectF rectF = new RectF(0, 0, imageSize, imageSize); 81 | 82 | Paint paint = new Paint(); 83 | paint.setAntiAlias(true); 84 | paint.setFilterBitmap(true); 85 | 86 | 87 | Path path = new Path(); 88 | path.addRoundRect(rectF, radius, radius, Path.Direction.CW); 89 | canvas.clipPath(path, Region.Op.INTERSECT); 90 | 91 | canvas.drawBitmap(bitmap, rect, rectF, paint); 92 | 93 | return result; 94 | } 95 | 96 | @Override 97 | protected void onPostExecute(Bitmap bitmap) { 98 | imageView2.setImageBitmap(bitmap); 99 | } 100 | } 101 | 102 | } 103 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/an.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gavinliu/BeautifulOfRoundedBitmap/7c4fc7bc0e4c02847aeae40226d2caa483f75181/app/src/main/res/drawable/an.jpg -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 13 | 14 | 19 | 20 | 27 | 28 | 29 | 30 | 34 | 35 | 39 | 40 | 45 | 46 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gavinliu/BeautifulOfRoundedBitmap/7c4fc7bc0e4c02847aeae40226d2caa483f75181/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gavinliu/BeautifulOfRoundedBitmap/7c4fc7bc0e4c02847aeae40226d2caa483f75181/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gavinliu/BeautifulOfRoundedBitmap/7c4fc7bc0e4c02847aeae40226d2caa483f75181/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gavinliu/BeautifulOfRoundedBitmap/7c4fc7bc0e4c02847aeae40226d2caa483f75181/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gavinliu/BeautifulOfRoundedBitmap/7c4fc7bc0e4c02847aeae40226d2caa483f75181/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /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 | 20dp 7 | 200dp 8 | 9 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | BeautifulOfRoundedBitmap 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/test/java/cn/gavinliu/demo/beautifulofroundedbitmap/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package cn.gavinliu.demo.beautifulofroundedbitmap; 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 | } -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | repositories { 5 | jcenter() 6 | } 7 | dependencies { 8 | classpath 'com.android.tools.build:gradle:2.1.0-alpha5' 9 | 10 | // NOTE: Do not place your application dependencies here; they belong 11 | // in the individual module build.gradle files 12 | } 13 | } 14 | 15 | allprojects { 16 | repositories { 17 | jcenter() 18 | } 19 | } 20 | 21 | task clean(type: Delete) { 22 | delete rootProject.buildDir 23 | } 24 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | --------------------------------------------------------------------------------