Android實現圓角圖片
更新時間:2021年01月26日 12:01:24 作者:tracydragonlxy
這篇文章主要為大家詳細介紹了Android實現圓角圖片,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Android實現圓角圖片的具體代碼,供大家參考,具體內容如下
效果圖:

快速開始
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圖片進行剪切成正方形, 然后再設置圓角半徑為正方形邊長的一半即可
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);
}
}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android使用addView動態(tài)添加組件的方法
這篇文章主要為大家詳細介紹了Android使用addView動態(tài)添加組件的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09
Android開發(fā)實現的圖片瀏覽功能示例【放大圖片】
這篇文章主要介紹了Android開發(fā)實現的圖片瀏覽功能,結合實例形式分析了Android針對圖片的切換顯示、透明度、大小調整等相關操作技巧,需要的朋友可以參考下2019-04-04
Android音視頻開發(fā)之VideoView使用指南
VideoView組件內部同樣是使用MediaPlayer+SurfaceView的形式控制MediaPlayer對視頻文件進行播放,本文就來詳細講講它的使用方法,需要的可以參考一下2022-04-04

