Android自定義View實(shí)現(xiàn)心形圖案
本文實(shí)例為大家分享了Android自定義View實(shí)現(xiàn)心形的具體代碼,供大家參考,具體內(nèi)容如下
通過(guò)繼承View實(shí)現(xiàn)的❤形
在繪制心形需要Path類(lèi)中的兩個(gè)重要方法分別是:moveTo、cubicTo
moveTo 不會(huì)進(jìn)行繪制,只用于移動(dòng)移動(dòng)畫(huà)筆。
lineTo 用于進(jìn)行直線繪制。
quadTo 用于繪制圓滑曲線,即貝塞爾曲線。
cubicTo 同樣是用來(lái)實(shí)現(xiàn)貝塞爾曲線的。
具體實(shí)現(xiàn):
public class HeartView extends View { private int mMeasureWidth; private int mWidthMode; private int mMeasureHeight; private int mHeightMode; private Paint paint; public HeartView(Context context) { super(context); } public HeartView(Context context, AttributeSet attrs) { super(context, attrs); paint = new Paint();//實(shí)例畫(huà)筆 paint.setAntiAlias(true);//抗鋸齒 paint.setStrokeWidth(2);//畫(huà)筆寬度 paint.setColor(Color.RED);//畫(huà)筆顏色 paint.setStyle(Paint.Style.FILL);//畫(huà)筆樣式 } /** * 測(cè)量 */ @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); mWidthMode = MeasureSpec.getMode(widthMeasureSpec); mHeightMode = MeasureSpec.getMode(heightMeasureSpec); mMeasureWidth = MeasureSpec.getSize(widthMeasureSpec); mMeasureHeight = MeasureSpec.getSize(heightMeasureSpec); if (mWidthMode == MeasureSpec.AT_MOST && mHeightMode == MeasureSpec.AT_MOST) { setMeasuredDimension(200, 200); } else if (mWidthMode == MeasureSpec.AT_MOST) { setMeasuredDimension(200, mMeasureHeight); } else if (mHeightMode == MeasureSpec.AT_MOST) { setMeasuredDimension(mMeasureWidth, 200); } else { setMeasuredDimension(mMeasureWidth, mMeasureHeight); } } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); int width = getWidth();//獲取屏幕寬 int height = getHeight();//獲取屏幕高 /** * 繪制心形 */ //左半面 Path path = new Path(); path.moveTo(width / 2, height / 4); path.cubicTo((width * 6) / 7, height / 9, (width * 12) / 13, (height * 2) / 5, width / 2, (height * 7) / 12); canvas.drawPath(path, paint); //右半面 Path path2 = new Path(); path2.moveTo(width / 2, height / 4); path2.cubicTo(width / 7, height / 9, width / 13, (height * 2) / 5, width / 2, (height * 7) / 12); canvas.drawPath(path2, paint); } }
在布局中引入一下
<com.xxb.cache.weight.HeartView android:layout_width="match_parent" android:layout_height="match_parent" />
實(shí)現(xiàn)效果:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android Activity啟動(dòng)模式之singleTask實(shí)例詳解
這篇文章主要介紹了Android Activity啟動(dòng)模式之singleTask,結(jié)合實(shí)例形式較為詳細(xì)的分析了singleTask模式的功能、使用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-01-01Kotlin?Navigation可視化開(kāi)發(fā)詳解
Navigation?是?JetPack?中的一個(gè)組件,用于方便的實(shí)現(xiàn)頁(yè)面的導(dǎo)航,所以抽象出了一個(gè)?destination?的概念,大部分情況一個(gè)?destination?就表示一個(gè)?Fragment,但是它同樣可以指代?Activity、其它的導(dǎo)航圖2023-02-02Android 軟鍵盤(pán)狀態(tài)并隱藏輸入法的實(shí)例
這篇文章主要介紹了Android 軟鍵盤(pán)狀態(tài)并隱藏輸入法的實(shí)例的相關(guān)資料,這里提供實(shí)例實(shí)現(xiàn)軟鍵盤(pán)切換并隱藏輸入法的鍵盤(pán),需要的朋友可以參考下2017-09-09Android編程中Perferences的用法實(shí)例分析
這篇文章主要介紹了Android編程中Perferences的用法,以實(shí)例形式較為詳細(xì)的分析了配置文件preferences.xml的功能、定義及使用方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11Android中用Builder模式自定義Dialog的方法
在任何軟件操作系統(tǒng)中,Dialog即對(duì)話(huà)框都是一種重要的交互模式與信息載體,而Android系統(tǒng)本身的Dialog擁有固定的樣式,并且在5.0后采用Material Design設(shè)計(jì)風(fēng)格的Dialog美觀大氣。這篇文章將詳細(xì)介紹Android中用Builder模式自定義Dialog的方法,有需要的可以參考借鑒。2016-10-10Android編程之絕對(duì)布局AbsoluteLayout和相對(duì)布局RelativeLayout實(shí)例詳解
這篇文章主要介紹了Android編程之絕對(duì)布局AbsoluteLayout和相對(duì)布局RelativeLayout實(shí)現(xiàn)方法,結(jié)合實(shí)例形式詳細(xì)分析了Android絕對(duì)布局AbsoluteLayout和相對(duì)布局RelativeLayout的原理與使用技巧,需要的朋友可以參考下2015-12-12淺談關(guān)于Android路由的實(shí)現(xiàn)
本篇文章主要介紹了淺談關(guān)于Android路由的實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10剖析Android Activity側(cè)滑返回的實(shí)現(xiàn)原理
在很多的App中,都會(huì)發(fā)現(xiàn)利用手指滑動(dòng)事件,進(jìn)行高效且人性化的交互非常有必要,那么它是怎么實(shí)現(xiàn)的呢,本文給大家解析實(shí)現(xiàn)原理,對(duì)Activity側(cè)滑返回實(shí)現(xiàn)代碼感興趣的朋友一起看看吧2021-06-06Android實(shí)現(xiàn)簡(jiǎn)易的柱狀圖和曲線圖表實(shí)例代碼
柱狀圖是統(tǒng)計(jì)圖表中經(jīng)常用到的一種圖表,比如降雨量之類(lèi)的統(tǒng)計(jì)展示。這篇文章主要給大家介紹了關(guān)于利用Android如何實(shí)現(xiàn)簡(jiǎn)易的柱狀圖和曲線圖表的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。2017-12-12