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使alertDialog.builder不會點(diǎn)擊外面和按返回鍵消失的方法
本篇文章主要介紹了Android使alertDialog.builder不會點(diǎn)擊外面和按返回鍵消失的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-01-01Android 中TabLayout自定義選擇背景滑塊的實例代碼
TabLayout是Android 的Material Design包中的一個控件,可以和V4包中的ViewPager搭配產(chǎn)生一個聯(lián)動的效果。接下來通過本文給大家分享TabLayout自定義選擇背景滑塊的實例代碼,感興趣的朋友一起學(xué)習(xí)吧2016-10-10Android開發(fā)學(xué)習(xí)之WallPaper設(shè)置壁紙詳細(xì)介紹與實例
這篇文章主要介紹了Android開發(fā)學(xué)習(xí)之WallPaper設(shè)置壁紙詳細(xì)介紹與實例,有需要的朋友可以參考一下2013-12-12詳解Android開發(fā)錄音和播放音頻的步驟(動態(tài)獲取權(quán)限)
這篇文章主要介紹了詳解Android開發(fā)錄音和播放音頻的步驟(動態(tài)獲取權(quán)限),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08Android自定義TextView實現(xiàn)文字傾斜效果
有時候Android自帶的控件無法滿足我們的某些要求,這時就需要我們自定義控件來實現(xiàn)這些功能。比如在實際開發(fā)應(yīng)用中,我們有時需要將TextView的文字傾斜一定的角度,就需要自定義TextView。下面這篇文章就給大家介紹了利用Android TextView如何實現(xiàn)文字傾斜效果。2016-11-11