国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

Android 使用Kotlin自定義View的方法教程

2019-10-22 18:20:15
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

前言

隨著google宣布kotlin/281032.html">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。

效果圖如下:

kotlin,自定義view,android,view,android自定義view

首先是這次自定義View的效果圖,是一張餅圖。如果是用java寫(xiě)的話也就幾十行,覺(jué)得換成Kotlin的話可能會(huì)更少。

示例代碼

主要的功能是可以任設(shè)定數(shù)據(jù)的個(gè)數(shù),我這里是4個(gè)數(shù)據(jù),可以任意設(shè)定每個(gè)數(shù)據(jù)的顏色。

#####首先上Kotlin代碼#####

package top.greendami.mykotlinappimport android.content.Contextimport android.graphics.*import android.util.AttributeSetimport 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.mykotlinappimport android.os.Bundleimport android.support.v7.app.AppCompatActivityimport 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ì)VEVB武林網(wǎng)的支持。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到Android開(kāi)發(fā)頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 商丘市| 巴林左旗| 鄄城县| 衡东县| 潍坊市| 乌海市| 乌海市| 双鸭山市| 钦州市| 泗洪县| 固始县| 斗六市| 忻城县| 岳阳县| 平罗县| 乌苏市| 仁寿县| 连州市| 疏附县| 济宁市| 化德县| 仙游县| 西林县| 开远市| 新和县| 普兰店市| 麻栗坡县| 治多县| 姚安县| 汕头市| 襄城县| 铜陵市| 包头市| 东山县| 彰化市| 和硕县| 仁怀市| 荔浦县| 盐池县| 岗巴县| 垫江县|