Android实现渐变色的圆弧虚线效果

Android实现渐变色的圆弧虚线效果,第1张

概述首先来看看效果图:1,SweepGradient(梯度渲染)publicSweepGradient(floatcx,floatcy,int[]colors,float[]positions)

首先来看看效果图:


1,SweepGradIEnt(梯度渲染)

public SweepGradIEnt (float cx,float cy,int[] colors,float[] positions)

扫描渲染,就是以某个点位中心旋转一周所形成的效果!参数依次是:

      cx:扫描的中心x坐标

      cy:扫描的中心y坐标

      colors:梯度渐变的颜色数组

      positions:指定颜色数组的相对位置

public static final int[] SWEEP_GRADIENT_colorS = new int[]{color.GREEN,color.GREEN,color.BLUE,color.RED,color.RED};mcolorShader = new SweepGradIEnt(radius,radius,SWEEP_GRADIENT_colorS,null);

效果图:


SweepGradIEnt

2,DashPathEffect(Path的线段虚线化)

DashPathEffect(float[] intervals,float phase)

      intervals:为虚线的ON和OFF的数组,数组中元素数目需要 >= 2

      phase:为绘制时的偏移量

//计算路径的长度PathMeasure pathMeasure = new PathMeasure(mPath,false);float length = pathMeasure.getLength();float step = length / 60;dashPathEffect = new DashPathEffect(new float[]{step / 3,step * 2 / 3},0);

效果图:


DashPathEffect

3,下面是全部的代码:

package com.example.yyw.xfermodedemo;import androID.animation.ValueAnimator;import androID.content.Context;import androID.graphics.Canvas;import androID.graphics.color;import androID.graphics.DashPathEffect;import androID.graphics.Paint;import androID.graphics.Path;import androID.graphics.PathMeasure;import androID.graphics.RectF;import androID.graphics.SweepGradIEnt;import androID.util.AttributeSet;import androID.vIEw.VIEw;/** * Created by yyw on 2016/10/11. */public class Oiltableline extends VIEw { public static final int[] SWEEP_GRADIENT_colorS = new int[]{color.GREEN,color.RED}; private int tableWIDth = 50; private Paint mPaint; private Path mPath; private RectF mtableRectF; //把路径分成虚线段的 private DashPathEffect dashPathEffect; //给路径上色 private SweepGradIEnt mcolorShader; //指针的路径 private Path mPointerPath; private float mCurrentDegree = 60; public Oiltableline(Context context,AttributeSet attrs) {  super(context,attrs);  mPaint = new Paint();  mPaint.setAntiAlias(true);  mPaint.setDither(true);  mPaint.setcolor(color.BLACK);  mPath = new Path();  mPointerPath = new Path();  startAnimator(); } @OverrIDe protected voID onSizeChanged(int w,int h,int olDW,int oldh) {  super.onSizeChanged(w,h,olDW,oldh);  float size = Math.min(w,h) - tableWIDth * 2;  //油表的位置方框  mtableRectF = new RectF(0,size,size);  mPath.reset();  //在油表路径中增加一个从起始弧度  mPath.addArc(mtableRectF,60,240);  //计算路径的长度  PathMeasure pathMeasure = new PathMeasure(mPath,false);  float length = pathMeasure.getLength();  float step = length / 60;  dashPathEffect = new DashPathEffect(new float[]{step / 3,0);  float radius = size / 2;  mcolorShader = new SweepGradIEnt(radius,null);  //设置指针的路径位置  mPointerPath.reset();  mPointerPath.moveto(radius,radius - 20);  mPointerPath.lineto(radius,radius + 20);  mPointerPath.lineto(radius * 2 - tableWIDth,radius);  mPointerPath.close(); } @OverrIDe protected voID onDraw(Canvas canvas) {  super.onDraw(canvas);  float dx = (getWIDth() - mtableRectF.wIDth()) / 2;  float dy = (getHeight() - mtableRectF.height()) / 2;  //把油表的方框平移到正中间  canvas.translate(dx,dy);  canvas.save();  //旋转画布  canvas.rotate(90,mtableRectF.wIDth() / 2,mtableRectF.height() / 2);  mPaint.setStyle(Paint.Style.stroke);  mPaint.setstrokeWIDth(tableWIDth);  mPaint.setPathEffect(dashPathEffect);  mPaint.setShader(mcolorShader);  canvas.drawPath(mPath,mPaint);  canvas.restore();  //还原画笔  mPaint.setPathEffect(null);  mPaint.setShader(null);  mPaint.setStyle(Paint.Style.FILL);  mPaint.setstrokeWIDth(tableWIDth / 10);  canvas.save();  canvas.rotate(150 + mCurrentDegree,mtableRectF.height() / 2);  canvas.drawPath(mPointerPath,mPaint);  canvas.restore(); } public voID startAnimator() {  ValueAnimator animator = ValueAnimator.offloat(0,240);  animator.setDuration(40000);  animator.setRepeatCount(ValueAnimator.INFINITE);  animator.setRepeatMode(ValueAnimator.RESTART);  animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {   @OverrIDe   public voID onAnimationUpdate(ValueAnimator animation) {    mCurrentDegree = (int) (0 + (float) animation.getAnimatedValue());    invalIDate();   }  });  animator.start(); }}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。

总结

以上是内存溢出为你收集整理的Android实现渐变色圆弧虚线效果全部内容,希望文章能够帮你解决Android实现渐变色的圆弧虚线效果所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1147895.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存