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

創(chuàng)建類CustomRoundAngleImageView
public class CustomRoundAngleImageView extends AppCompatImageView {
float width, height;
public CustomRoundAngleImageView(Context context) {
this(context, null);
init(context, null);
}
public CustomRoundAngleImageView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
init(context, attrs);
}
public CustomRoundAngleImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
if (Build.VERSION.SDK_INT < 18) {
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
width = getWidth();
height = getHeight();
}
@Override
protected void onDraw(Canvas canvas) {
if (width >= 20 && height > 20) {
Path path = new Path();
//四個圓角
path.moveTo(20, 0);
path.lineTo(width - 20, 0);
path.quadTo(width, 0, width, 20);
path.lineTo(width, height - 20);
path.quadTo(width, height, width - 20, height);
path.lineTo(20, height);
path.quadTo(0, height, 0, height - 20);
path.lineTo(0, 20);
path.quadTo(0, 0, 20, 0);
canvas.clipPath(path);
}
super.onDraw(canvas);
}
}
布局代碼
<com.example.myapplication.CustomRoundAngleImageView
android:id="@+id/we_image"
android:layout_width="42dp"
android:layout_height="42dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="10dp"
android:src="@mipmap/ic_launcher"
android:layout_marginBottom="10dp"
android:layout_gravity="center"
android:scaleType="centerCrop" />
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解決Android Studio 代碼自動提示突然失效的問題
這篇文章主要介紹了解決Android Studio 代碼自動提示突然失效的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Android中設(shè)置只有程序第一次運行才顯示的界面實現(xiàn)思路
如何實現(xiàn)程序第一次運行才顯示的界面,下面是具體的實現(xiàn)思路及步驟,有類似需求的朋友可以參考下哈2013-06-06
Android Studio 實現(xiàn)將support庫改成Androidx
這篇文章主要介紹了Android Studio 實現(xiàn)將support庫改成Androidx,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04
Android中Glide加載到RelativeLayout背景圖方法示例
Glide框架大家應(yīng)該都很熟悉,我們可以使用Glide加載網(wǎng)絡(luò)圖片、加載gif圖片,使用簡單。下面這篇文章主要給大家介紹了關(guān)于Android中Glide加載到RelativeLayout背景圖的相關(guān)資料,需要的朋友可以參考下。2017-12-12
Android上實現(xiàn)easyconfig(airkiss)方法
本篇文章主要給大家講解了在Android系統(tǒng)上實現(xiàn)easyconfig(airkiss)的方法,有這方面需要的朋友參考學(xué)習(xí)下吧。2018-01-01
學(xué)習(xí)Android自定義Spinner適配器
這篇文章主要為大家詳細(xì)介紹了學(xué)習(xí)Android自定義Spinner適配器的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-05-05
Android實現(xiàn)簡單C/S聊天室應(yīng)用
這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)簡單C/S聊天室應(yīng)用,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-01-01

