Android 使用Kotlin自定義View的方法教程
前言
隨著google宣布kotlin作為官方開(kāi)發(fā)語(yǔ)言,在Android中使用kotlin的趨勢(shì)也越來(lái)越明顯,最近被kotlin的文章轟炸了,所以決定上手試一下,試過(guò)之后,感覺(jué)靠它靈簡(jiǎn)直有魔性。特別是一句話寫(xiě)出一個(gè)復(fù)雜的循環(huán)的時(shí)候,簡(jiǎn)直被驚呆。而且使用AS,Java代碼可以直接轉(zhuǎn)成Kotlin。
效果圖如下:
首先是這次自定義View的效果圖,是一張餅圖。如果是用java寫(xiě)的話也就幾十行,覺(jué)得換成Kotlin的話可能會(huì)更少。
示例代碼
主要的功能是可以任設(shè)定數(shù)據(jù)的個(gè)數(shù),我這里是4個(gè)數(shù)據(jù),可以任意設(shè)定每個(gè)數(shù)據(jù)的顏色。
#####首先上Kotlin代碼#####
package top.greendami.mykotlinapp import android.content.Context import android.graphics.* import android.util.AttributeSet import android.view.View /** * Created by GreendaMi on 2017/4/10. */ class PPCircle : View { var mDatas = ArrayList<Float>() var mColors = ArrayList<Int>(4) var mPaint: Paint = Paint() constructor(mContext: Context) : super(mContext) { val context = mContext } constructor(mContext: Context, mAttributeSet: AttributeSet) : super(mContext, mAttributeSet) { initPaint() val context = mContext } fun initPaint() { mPaint.isAntiAlias = true mPaint.style = Paint.Style.FILL_AND_STROKE mPaint.color = 0xff44b391.toInt() } //長(zhǎng)寬一致 override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { super.onMeasure(widthMeasureSpec, heightMeasureSpec) val widthSpecSize = View.MeasureSpec.getSize(widthMeasureSpec) val heightSpecSize = View.MeasureSpec.getSize(heightMeasureSpec) val mLayoutSize = Math.min(widthSpecSize, heightSpecSize) setMeasuredDimension(mLayoutSize, mLayoutSize) } /** * 設(shè)置數(shù)據(jù) */ fun setData(data: ArrayList<Float>, colors: ArrayList<Int>) { mDatas = data mColors = colors invalidate() } override fun onDraw(canvas: Canvas?) { super.onDraw(canvas) if (mDatas.size == 0) { return } //切掉圓心 var mPath = Path() mPath.addCircle(width / 2f, height / 2f, width / 2f * 0.4f,Path.Direction.CW) mPath.close() canvas?.clipPath(mPath, Region.Op.XOR) var total = 0f //此處亮點(diǎn) mDatas.forEach { total += it } var rf = RectF(0f, 0f, width.toFloat(), height.toFloat()) var startAngle = -90f//起點(diǎn) var i = 0 mDatas.forEach { mPaint.color = mColors[i] var sweepAngle = it * 360 / total canvas?.drawArc(rf, startAngle, sweepAngle, true, mPaint) startAngle += sweepAngle i++ } } }
設(shè)置數(shù)據(jù)
package top.greendami.mykotlinapp import android.os.Bundle import android.support.v7.app.AppCompatActivity import kotlinx.android.synthetic.main.activity_main2.* class Main2Activity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main2) var mDatas = ArrayList<Float>() mDatas.add(1f) mDatas.add(2f) mDatas.add(4f) mDatas.add(2f) var mColors = ArrayList<Int>() mColors.add(0xff83ccd2.toInt()) mColors.add(0xffc0e1ce.toInt()) mColors.add(0xfffac55e.toInt()) mColors.add(0xffef805f.toInt()) ppCircle.setData(mDatas,mColors) } }
<?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="top.greendami.mykotlinapp.Main2Activity"> <top.greendami.mykotlinapp.PPCircle android:id="@+id/ppCircle" android:layout_width="300dp" android:layout_height="300dp" app:layout_constraintBottom_toBottomOf="parent" android:layout_marginBottom="8dp" android:layout_marginRight="8dp" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" android:layout_marginTop="8dp" android:layout_marginLeft="8dp" app:layout_constraintLeft_toLeftOf="parent" /> </android.support.constraint.ConstraintLayout>
#####相同功能Java代碼#####
package com.allrun.arsmartelevatorformanager.widget; import android.content.Context; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Path; import android.graphics.RectF; import android.graphics.Region; import android.support.annotation.Nullable; import android.util.AttributeSet; import android.view.View; import java.util.ArrayList; import java.util.List; /** * Created by GreendaMi on 2017/4/11. */ public class PPCircle extends View { Context mContext; List<Float> mData = new ArrayList<Float>();//數(shù)據(jù) List<Integer> mColors = new ArrayList<Integer>();//數(shù)據(jù)對(duì)應(yīng)的顏色 Paint mPaint = new Paint(); public PPCircle(Context context) { super(context); } public PPCircle(Context context, @Nullable AttributeSet attrs) { super(context, attrs); mContext = context; initPaint(); } private void initPaint() { mPaint.setAntiAlias(true); mPaint.setStyle(Paint.Style.FILL_AND_STROKE); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int widthSpecSize = View.MeasureSpec.getSize(widthMeasureSpec); int heightSpecSize = View.MeasureSpec.getSize(heightMeasureSpec); int mLayoutSize = Math.min(widthSpecSize, heightSpecSize); setMeasuredDimension(mLayoutSize, mLayoutSize); } public void setData(List<Float> mData, List<Integer> mColors) { this.mData = mData; this.mColors = mColors; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (mData.size() == 0) { return; } //切掉圓心 Path mPath =new Path(); mPath.addCircle(getWidth()/2,getWidth()/2,getWidth()/2* 0.4f ,Path.Direction.CW); canvas.clipPath(mPath, Region.Op.XOR); float total = 0; for(float temp : mData){ total = total + temp; } RectF rf = new RectF(0f, 0f, getWidth(), getHeight()); float startAngle = -90f;//起點(diǎn) int i = 0; for(float temp : mData){ mPaint.setColor(mColors.get(i)); float sweepAngle = temp * 360 / total; canvas.drawArc(rf, startAngle, sweepAngle, true, mPaint); startAngle += sweepAngle; i++; } } }
說(shuō)說(shuō)Kotlin和Java感覺(jué)差異比較大的地方。首先是變量的生命,Kotlin聲明時(shí)必須賦值或者初始化,java則不用,開(kāi)始有點(diǎn)不習(xí)慣。Kotlin不需要分號(hào)結(jié)尾,Kotlin的循環(huán)用起來(lái)簡(jiǎn)直爽YY。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
- Android 自定義View的使用介紹
- Android自定義View實(shí)現(xiàn)搜索框(SearchView)功能
- Android開(kāi)發(fā)使用自定義View將圓角矩形繪制在Canvas上的方法
- Android自定義View設(shè)定到FrameLayout布局中實(shí)現(xiàn)多組件顯示的方法 分享
- Android自定義View實(shí)現(xiàn)廣告信息上下滾動(dòng)效果
- Android自定義View實(shí)現(xiàn)繪制虛線的方法詳解
- Android自定義View之自定義評(píng)價(jià)打分控件RatingBar實(shí)現(xiàn)自定義星星大小和間距
- Android自定義View實(shí)現(xiàn)loading動(dòng)畫(huà)加載效果
- Android自定義View實(shí)現(xiàn)漸變色進(jìn)度條
- Android?自定義view中根據(jù)狀態(tài)修改drawable圖片
相關(guān)文章
android項(xiàng)目實(shí)現(xiàn)帶進(jìn)度條的系統(tǒng)通知欄消息
本篇文章主要介紹了android項(xiàng)目實(shí)現(xiàn)帶進(jìn)度條的系統(tǒng)通知欄消息,就是實(shí)現(xiàn)在通知欄看到下載進(jìn)度。具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-10-10Android實(shí)現(xiàn)GPS定位代碼實(shí)例
這篇文章主要介紹了Android實(shí)現(xiàn)GPS定位實(shí)例,對(duì)關(guān)鍵操作部份給出代碼示例并做了一定的注釋,需要的朋友可以參考下2014-07-07Android實(shí)現(xiàn)二級(jí)購(gòu)物車(chē)的全選加反選、總價(jià)功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)二級(jí)購(gòu)物車(chē)的全選加反選、總價(jià)功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04Android官方下拉刷新控件SwipeRefreshLayout使用詳解
這篇文章主要為大家詳細(xì)介紹了Android官方下拉刷新控件SwipeRefreshLayout使用方法,實(shí)例展示如何使用下拉刷新控件,感興趣的小伙伴們可以參考一下2016-07-07Android實(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-12Android編程實(shí)現(xiàn)自動(dòng)調(diào)整TextView字體大小以適應(yīng)文字長(zhǎng)度的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)自動(dòng)調(diào)整TextView字體大小以適應(yīng)文字長(zhǎng)度的方法,涉及Android基于TextView類(lèi)的繼承及Paint屬性操作實(shí)現(xiàn)字體大小自適應(yīng)的相關(guān)技巧,需要的朋友可以參考下2016-01-01Android Scroll滑動(dòng)效果實(shí)例
這篇文章主要為大家分享了Android Scroll滑動(dòng)效果實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-04-04