欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

Android自定義View繪制彩色圓弧

本文實例為大家分享了Android自定義View繪制彩色圓弧的具體代碼,供大家參考,具體內(nèi)容如下

讓客戶滿意是我們工作的目標,不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價值的長期合作伙伴,公司提供的服務(wù)項目有:域名注冊虛擬主機、營銷軟件、網(wǎng)站建設(shè)、平陰網(wǎng)站維護、網(wǎng)站推廣。

效果如下:

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;

 /**
 * 直接在代碼中調(diào)用時,使用該函數(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ù)中手動調(diào)用
 *
 * @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);//設(shè)置畫筆為無鋸齒
 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);//設(shè)置畫筆為無鋸齒
 paintFull.setShader(s);
 textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
 textPaint.setTextSize(dip2px(context, 22));//設(shè)置字體大小
 textPaint.setColor(0xFFFFFFFF);
 valuePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
 valuePaint.setAntiAlias(true);//設(shè)置畫筆為無鋸齒
 }

 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轉(zhuǎn)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();
 }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

網(wǎng)站欄目:Android自定義View繪制彩色圓弧
網(wǎng)站路徑:http://aaarwkj.com/article24/pdehje.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營銷型網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)、定制網(wǎng)站、軟件開發(fā)、建站公司網(wǎng)站維護

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護公司
99久久久久国产精品免费| 成人午夜激情福利动态| 一级片高清在线观看国产| 青青草网站在线观看视频| 国产亚洲成人精品一区| 日韩欧美精品另类在线| 久久精品一品二品三品| 亚洲欧美日韩国产亚洲欧美日韩国产| 欧美精品一区二区三区在线| 九九在线视频免费观看精品视频| 日韩一卡一卡在线观看| 欧美亚洲另类日韩综合网| 91亚洲蜜臀精品国产| 国产亚洲综合精品综合区| 免费观看国内性生活大片| 四虎永久播放地址免费| 国产精品乱码一区二区视频| 成人一区二区三区乱码| 欧美精品国产精品久久| 日本一区二区最新免费| 熟女另类视频在线观看| 久久精品国产亚洲av久| 中文字幕中文字幕乱码| 免费观看欧美日韩论理电影| 精品毛片av一区二区三区| 亚洲欧美成人免费视频| 蜜桃午夜精品一区二区三区| 亚洲日本韩国福利久久| 国产精品久久av高潮呻吟| 亚洲国产欧美日韩国产| 热九九这里只有热九九 | 丰满熟妇久久人妻同堂av| 午夜少妇诱惑一区二区三区| 久久国产精品亚洲熟女66r| 人妻少妇精品视频二区| 香蕉伊蕉伊中文在线视频| 五月综合丁香婷婷久久| 精品人妻va人妻中文字幕麻豆| 欧美大片高清在线观看| 激情欧美精品桃桃激情| 一区二区三区乱码国产在线|