Android實(shí)現(xiàn)圓角圖片
本文實(shí)例為大家分享了Android實(shí)現(xiàn)圓角圖片的具體代碼,供大家參考,具體內(nèi)容如下
效果圖:

快速開始
activity_main.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/iv_img"
android:layout_width="300dp"
android:layout_height="200dp"
android:layout_marginTop="30dp"
android:src="@mipmap/image_bg"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<ImageView
android:id="@+id/iv_rect_img"
android:layout_width="300dp"
android:layout_height="200dp"
android:layout_marginTop="30dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/iv_img"/>
<ImageView
android:id="@+id/iv_circle_img"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginTop="30dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/iv_rect_img"/>
</android.support.constraint.ConstraintLayout>
MainActivity.class文件:
public class MainActivity extends AppCompatActivity {
private ImageView ivRectImg, ivCircleImg;
private Bitmap bitmap;
private int width;
private int height;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ivRectImg = findViewById(R.id.iv_rect_img);
ivCircleImg = findViewById(R.id.iv_circle_img);
bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.image_bg);
width = bitmap.getWidth();
height = bitmap.getHeight();
rectRoundBitmap();
circleBitmap();
}
// 圓角矩形
private void rectRoundBitmap() {
RoundedBitmapDrawable bitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), bitmap);
bitmapDrawable.setAntiAlias(true);
bitmapDrawable.setCornerRadius(50);
ivRectImg.setImageDrawable(bitmapDrawable);
}
// 把bitmap圖片進(jìn)行剪切成正方形, 然后再設(shè)置圓角半徑為正方形邊長(zhǎng)的一半即可
private void circleBitmap() {
Bitmap circle = null;
int min = Math.min(width, height);
int max = Math.max(width, height);
if (width == height) {
circle = Bitmap.createBitmap(bitmap, 0, 0, width, height);
} else {
// 居中裁剪
if (width > height) {
circle = Bitmap.createBitmap(bitmap, (max - min) / 2, 0, min, min);
} else {
circle = Bitmap.createBitmap(bitmap, 0, (max - min) / 2, min, min);
}
}
RoundedBitmapDrawable bitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), circle);
bitmapDrawable.setCornerRadius(min / 2);
bitmapDrawable.setAntiAlias(true);
ivCircleImg.setImageDrawable(bitmapDrawable);
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解Android Service 使用時(shí)的注意事項(xiàng)
這篇文章主要介紹了詳解Android Service 使用時(shí)的注意事項(xiàng),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10
Android貝塞爾曲線實(shí)現(xiàn)加入購物車拋物線動(dòng)畫
這篇文章主要為大家詳細(xì)介紹了Android貝塞爾曲線實(shí)現(xiàn)加入購物車拋物線動(dòng)畫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06
淺談android獲取設(shè)備唯一標(biāo)識(shí)完美解決方案
本篇文章主要介紹了淺談android獲取設(shè)備唯一標(biāo)識(shí)完美解決方案,具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08
Android使用addView動(dòng)態(tài)添加組件的方法
這篇文章主要為大家詳細(xì)介紹了Android使用addView動(dòng)態(tài)添加組件的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
Android開發(fā)實(shí)現(xiàn)的圖片瀏覽功能示例【放大圖片】
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)的圖片瀏覽功能,結(jié)合實(shí)例形式分析了Android針對(duì)圖片的切換顯示、透明度、大小調(diào)整等相關(guān)操作技巧,需要的朋友可以參考下2019-04-04
Android撥號(hào)盤 支持T9搜索和號(hào)碼搜索等撥號(hào)盤案例
之前做通訊錄軟件,其中在做撥號(hào)盤的時(shí)候一直為怎么實(shí)現(xiàn)T9輸入煩惱,不過最后終于是實(shí)現(xiàn)了,看社區(qū)內(nèi)好像也有不少朋友需要,在此分享一下2012-12-12
Android音視頻開發(fā)之VideoView使用指南
VideoView組件內(nèi)部同樣是使用MediaPlayer+SurfaceView的形式控制MediaPlayer對(duì)視頻文件進(jìn)行播放,本文就來詳細(xì)講講它的使用方法,需要的可以參考一下2022-04-04
Android?常見獲取設(shè)備標(biāo)識(shí)方法總結(jié)
隨著Android系統(tǒng)版本更新,Google對(duì)用戶隱私保護(hù)增強(qiáng),限制獲取設(shè)備標(biāo)識(shí),文中測(cè)試DeviceID、ANDROID_ID、Serial、MAC地址等方法在不同API級(jí)別的表現(xiàn),感興趣的朋友跟隨小編一起看看吧2024-09-09

