您好,登錄后才能下訂單哦!
本文實例為大家分享了Android自定義View繪制彩色圓弧的具體代碼,供大家參考,具體內容如下
效果如下:
自定義View代碼如下:
package com.example.yan; import android.content.Context; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Rect; import android.graphics.RectF; import android.graphics.Shader; import android.graphics.SweepGradient; import android.util.AttributeSet; import android.view.View; /** * Created by xiaoyanzi on 2016/3/18. * 漸變圓弧 */ public class GradualView extends View { private Context context; private Paint paint;//畫筆 private Paint paintFull;//實心圓畫筆 private Paint textPaint;//標識字畫筆 private Paint valuePaint;//移動小球畫筆 private int[] mColors;//漸變色數(shù)組 private int centerX;//中心X private int centerY;//中心Y private int srcH;//控件高度 private float startAngle = 110;//圓弧起始角度 private float sweepAngle = 320;//圓弧所占度數(shù) private float airValue = 66; /** * 直接在代碼中調用時,使用該函數(shù) * * @param context */ public GradualView(Context context) { super(context); initData(context); } /** * 在xml中使用自定義view時,使用這個函數(shù) * * @param context * @param attrs */ public GradualView(Context context, AttributeSet attrs) { super(context, attrs); initData(context); } /** * 可以由上一個函數(shù)中手動調用 * * @param context * @param atrrs * @param defStyle 自定義函數(shù)中的attrs表示view的屬性集,defStyle表示默認的屬性資源集的id */ public GradualView(Context context, AttributeSet atrrs, int defStyle) { super(context, atrrs, defStyle); } /** * 初始化 * @param context */ private void initData(Context context) { this.context = context; paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setStyle(Paint.Style.STROKE); mColors = new int[]{ 0xFF660099,//紫色 0xFF330033,//褐色 0xFF99FF00,//草綠色 0xFFFFFF00,//黃色 0xFFFF6600,//橘色 0xFFFF0000,//紅色 0xFF660099,//紫色 }; Shader s = new SweepGradient(0, 0, mColors, null); paint.setAntiAlias(true);//設置畫筆為無鋸齒 paint.setStrokeWidth(dip2px(context, 14));//線寬 paint.setShader(s); paintFull = new Paint(Paint.ANTI_ALIAS_FLAG); paintFull.setStyle(Paint.Style.FILL_AND_STROKE); paintFull.setAntiAlias(true);//設置畫筆為無鋸齒 paintFull.setShader(s); textPaint = new Paint(Paint.ANTI_ALIAS_FLAG); textPaint.setTextSize(dip2px(context, 22));//設置字體大小 textPaint.setColor(0xFFFFFFFF); valuePaint = new Paint(Paint.ANTI_ALIAS_FLAG); valuePaint.setAntiAlias(true);//設置畫筆為無鋸齒 } public void setAirValue(float airValue) { this.airValue = airValue; } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); srcH = h; centerX = w / 2; centerY = h / 2; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); float r = srcH / 2 - paint.getStrokeWidth() * 0.5f - dip2px(context, 200); //移動中心 canvas.translate(centerX, centerY); RectF oval = new RectF(); oval.left = -r;//左邊 oval.top = -r;//上邊 oval.right = r;//右邊 oval.bottom = r;//下邊 canvas.drawArc(oval, startAngle, sweepAngle, false, paint); //繪制圓弧 //繪制圓弧兩頭的小圓 float mr = dip2px(context, 7); float x1 = (float) (-r * Math.sin((360 - sweepAngle) / 2 * Math.PI / 180)); float y1 = (float) (r * Math.cos((360 - sweepAngle) / 2 * Math.PI / 180)); canvas.drawCircle(x1, y1, mr, paintFull); float x2 = (float) (r * Math.sin((360 - sweepAngle) / 2 * Math.PI / 180)); float y2 = (float) (r * Math.cos((360 - sweepAngle) / 2 * Math.PI / 180)); canvas.drawCircle(x2, y2, mr, paintFull); //小圓離球的距離 float range = r + dip2px(context, 30); float ar = 12f; //畫周圍小球和數(shù)字 float ax1 = (float) (-range * Math.sin(45 * Math.PI / 180)); float ay1 = (float) (range * Math.cos(45 * Math.PI / 180)); canvas.drawCircle(ax1, ay1, ar, paintFull); canvas.drawText("0", ax1 - getTextW() * 3, ay1 + getTextH() / 2, textPaint); float ax2 = -range; float ay2 = 0; canvas.drawCircle(ax2, ay2, ar, paintFull); canvas.drawText("50", ax2 - getTextW() * 5, ay2 + getTextH() / 2, textPaint); float ax3 = (float) (-range * Math.sin(45 * Math.PI / 180)); float ay3 = (float) (-range * Math.cos(45 * Math.PI / 180)); canvas.drawCircle(ax3, ay3, ar, paintFull); canvas.drawText("100", ax3 - getTextW() * 7, ay3 + getTextH() / 2, textPaint); float ax4 = 0; float ay4 = -range; canvas.drawCircle(ax4, ay4, ar, paintFull); canvas.drawText("150", ax4 - getTextW() * 3, ay4 - getTextH(), textPaint); float ax5 = (float) (range * Math.sin(45 * Math.PI / 180)); float ay5 = (float) (-range * Math.cos(45 * Math.PI / 180)); canvas.drawCircle(ax5, ay5, ar, paintFull); canvas.drawText("200", ax5 + getTextW(), ay5 + getTextH() / 2, textPaint); float ax6 = range; float ay6 = 0; canvas.drawCircle(ax6, ay6, ar, paintFull); canvas.drawText("300", ax6 + getTextW(), ay6 + getTextH() / 2, textPaint); float ax7 = (float) (range * Math.sin(45 * Math.PI / 180)); float ay7 = (float) (range * Math.cos(45 * Math.PI / 180)); canvas.drawCircle(ax7, ay7, ar, paintFull); canvas.drawText("500", ax7 + getTextW(), ay7 + getTextH() / 2, textPaint); //畫標識小球 valuePaint.setColor(0xFFFFFFFF); float cx; float cy; if (airValue >= 0 && airValue <= 50) { cx = (float) (-r * Math.cos((45 - airValue * 0.9) * Math.PI / 180)); cy = (float) (r * Math.sin((45 - airValue * 0.9) * Math.PI / 180)); } else if (airValue > 50 && airValue <= 150) { cx = (float) (-r * Math.cos((airValue * 0.9 - 45) * Math.PI / 180)); cy = (float) (-r * Math.sin((airValue * 0.9 - 45) * Math.PI / 180)); } else if (airValue > 150 && airValue <= 200) { cx = (float) (-r * Math.cos((airValue * 0.9 - 45) * Math.PI / 180)); cy = (float) (-r * Math.sin((airValue * 0.9 - 45) * Math.PI / 180)); } else if (airValue > 200 && airValue <= 300) { cx = (float) (-r * Math.cos((airValue * 0.45 + 45) * Math.PI / 180)); cy = (float) (-r * Math.sin((airValue * 0.45 + 45) * Math.PI / 180)); } else if (airValue > 300 && airValue <= 500) {//此處有問題 cx = (float) (r * Math.cos(((airValue - 300) * 0.225) * Math.PI / 180)); cy = (float) (r * Math.sin(((airValue - 300) * 0.225) * Math.PI / 180)); } else { cx = (float) (-r * Math.cos(45 * Math.PI / 180)); cy = (float) (r * Math.sin(45 * Math.PI / 180)); } canvas.drawCircle(cx, cy, dip2px(context, 11), valuePaint); canvas.drawCircle(cx, cy, dip2px(context, 4), paintFull); } /** * dip轉px * @param context * @param dpValue * @return */ private int dip2px(Context context, float dpValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); } /** * 獲取"正"的高度 * * @return */ private float getTextH() { Paint pFont = new Paint(); Rect rect = new Rect(); //返回包圍整個字符串的最小的一個Rect區(qū)域 pFont.getTextBounds("正", 0, 1, rect); return rect.height(); } /** * 獲取"正"的寬度 * * @return */ private float getTextW() { Paint pFont = new Paint(); Rect rect = new Rect(); //返回包圍整個字符串的最小的一個Rect區(qū)域 pFont.getTextBounds("正", 0, 1, rect); return rect.width(); } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。