前言
随着google宣布kotlin作为官方开发语言,在Android中使用kotlin的趋势也越来越明显,最近被kotlin的文章轰炸了,所以决定上手试一下,试过之后,感觉靠它灵简直有魔性。特别是一句话写出一个复杂的循环的时候,简直被惊呆。而且使用AS,Java代码可以直接转成Kotlin。
效果图如下:
首先是这次自定义View的效果图,是一张饼图。如果是用java写的话也就几十行,觉得换成Kotlin的话可能会更少。
示例代码
主要的功能是可以任设定数据的个数,我这里是4个数据,可以任意设定每个数据的颜色。
#####首先上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() } //长宽一致 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) } /** * 设置数据 */ 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 //此处亮点 mDatas.forEach { total += it } var rf = RectF(0f, 0f, width.toFloat(), height.toFloat()) var startAngle = -90f//起点 var i = 0 mDatas.forEach { mPaint.color = mColors[i] var sweepAngle = it * 360 / total canvas?.drawArc(rf, startAngle, sweepAngle, true, mPaint) startAngle += sweepAngle i++ } }}
设置数据
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>();//数据 List<Integer> mColors = new ArrayList<Integer>();//数据对应的颜色 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;//起点 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++; } }}
说说Kotlin和Java感觉差异比较大的地方。首先是变量的生命,Kotlin声明时必须赋值或者初始化,java则不用,开始有点不习惯。Kotlin不需要分号结尾,Kotlin的循环用起来简直爽YY。
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。