Android自定義View實現(xiàn)心形圖案
本文實例為大家分享了Android自定義View實現(xiàn)心形的具體代碼,供大家參考,具體內(nèi)容如下
通過繼承View實現(xiàn)的❤形
在繪制心形需要Path類中的兩個重要方法分別是:moveTo、cubicTo
moveTo 不會進(jìn)行繪制,只用于移動移動畫筆。
lineTo 用于進(jìn)行直線繪制。
quadTo 用于繪制圓滑曲線,即貝塞爾曲線。
cubicTo 同樣是用來實現(xiàn)貝塞爾曲線的。
具體實現(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();//實例畫筆 paint.setAntiAlias(true);//抗鋸齒 paint.setStrokeWidth(2);//畫筆寬度 paint.setColor(Color.RED);//畫筆顏色 paint.setStyle(Paint.Style.FILL);//畫筆樣式 } /** * 測量 */ @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" />
實現(xiàn)效果:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android Activity啟動模式之singleTask實例詳解
這篇文章主要介紹了Android Activity啟動模式之singleTask,結(jié)合實例形式較為詳細(xì)的分析了singleTask模式的功能、使用方法與相關(guān)注意事項,需要的朋友可以參考下2016-01-01Android中用Builder模式自定義Dialog的方法
在任何軟件操作系統(tǒng)中,Dialog即對話框都是一種重要的交互模式與信息載體,而Android系統(tǒng)本身的Dialog擁有固定的樣式,并且在5.0后采用Material Design設(shè)計風(fēng)格的Dialog美觀大氣。這篇文章將詳細(xì)介紹Android中用Builder模式自定義Dialog的方法,有需要的可以參考借鑒。2016-10-10Android編程之絕對布局AbsoluteLayout和相對布局RelativeLayout實例詳解
這篇文章主要介紹了Android編程之絕對布局AbsoluteLayout和相對布局RelativeLayout實現(xiàn)方法,結(jié)合實例形式詳細(xì)分析了Android絕對布局AbsoluteLayout和相對布局RelativeLayout的原理與使用技巧,需要的朋友可以參考下2015-12-12剖析Android Activity側(cè)滑返回的實現(xiàn)原理
在很多的App中,都會發(fā)現(xiàn)利用手指滑動事件,進(jìn)行高效且人性化的交互非常有必要,那么它是怎么實現(xiàn)的呢,本文給大家解析實現(xiàn)原理,對Activity側(cè)滑返回實現(xiàn)代碼感興趣的朋友一起看看吧2021-06-06Android實現(xiàn)簡易的柱狀圖和曲線圖表實例代碼
柱狀圖是統(tǒng)計圖表中經(jīng)常用到的一種圖表,比如降雨量之類的統(tǒng)計展示。這篇文章主要給大家介紹了關(guān)于利用Android如何實現(xiàn)簡易的柱狀圖和曲線圖表的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。2017-12-12