Android實現(xiàn)圓角圖片
更新時間:2021年01月26日 12:01:24 作者:tracydragonlxy
這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)圓角圖片,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Android實現(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圖片進行剪切成正方形, 然后再設(shè)置圓角半徑為正方形邊長的一半即可
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);
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android miniTwitter登錄界面開發(fā)實例
這篇文章主要為大家詳細(xì)介紹了Android miniTwitter登錄界面開發(fā)實例,感興趣的小伙伴們可以參考一下2016-04-04
Android自定義ViewGroup嵌套與交互實現(xiàn)幕布全屏滾動
這篇文章主要為大家介紹了Android自定義ViewGroup嵌套與交互實現(xiàn)幕布全屏滾動效果示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-01-01
Android編程實現(xiàn)使用webView打開本地html文件的方法
這篇文章主要介紹了Android編程實現(xiàn)使用webView打開本地html文件的方法,結(jié)合實例形式分析了Android中webview布局及打開HTML文件的功能實現(xiàn)技巧,需要的朋友可以參考下2017-02-02
Android編程實現(xiàn)自定義輸入法功能示例【輸入密碼時防止第三方竊取】
這篇文章主要介紹了Android編程實現(xiàn)自定義輸入法功能,可實習(xí)輸入密碼時防止第三方竊取的效果,結(jié)合實例形式詳細(xì)分析了Android布局、控件及輸入法相關(guān)操作技巧,需要的朋友可以參考下2017-01-01

