自定義ProgressBar(圓)

2019獨角獸企業重金招聘Python工程師標準>>> hot3.png

<lib.view.progressbar.ColorArcProgressBar
    android:layout_width="match_parent"
    android:layout_height="220dip"
    android:id="@+id/barInterest"
    android:layout_centerInParent="true"
    android:layout_gravity="center_horizontal"
    app:back_color="@android:color/darker_gray"
    app:back_width="8dp"
    app:current_value="66"
    app:front_color2="@color/common_gray_fa"
    app:front_color1="@color/common_gray_fa"
    app:front_color3="@color/common_orange_m"
    app:front_width="8dp"
    app:is_need_content="true"
    app:is_need_title="true"
    app:is_need_unit="true"
    app:max_value="100"
    app:string_title="年化收益(%)"
    app:string_unit=""/>
package lib.view.progressbar;

import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.RectF;
import android.graphics.SweepGradient;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;

import com.lsy.hebao.R;

import lib.util.MathUtil;
public class ColorArcProgressBar extends View {private int diameter = 500;  //直徑
    private float centerX;  //圓心X坐標
    private float centerY;  //圓心Y坐標

    private Paint allArcPaint;
    private Paint progressPaint;
    private Paint vTextPaint;
    private Paint hintPaint;
    private Paint degreePaint;
    private Paint curSpeedPaint;

    private RectF bgRect;

    private ValueAnimator progressAnimator;
    private PaintFlagsDrawFilter mDrawFilter;

    private float startAngle = 150;
    private float sweepAngle = 240;
    private float currentAngle = 0;
    private float lastAngle;
    private float maxValues = 60;
    private float curValues = 0;
    private float bgArcWidth = dipToPx(2);
    private float progressWidth = dipToPx(10);
    private float textSize = dipToPx(60);
    private float hintSize = dipToPx(15);
    private float curSpeedSize = dipToPx(13);
    private int aniSpeed = 1000;
    private float longdegree = dipToPx(13);
    private float shortdegree = dipToPx(5);
    private final int DEGREE_PROGRESS_DISTANCE = dipToPx(18);

    private String longDegreeColor = "#111111";
    private String shortDegreeColor = "#111111";
    private String bgArcColor = "#88F0f0f0";
    private String currentArcColor = "#FFB700";
    private String titleString;
    private String hintString;

    private boolean isNeedTitle;
    private boolean isNeedUnit;
    private boolean isNeedDial;
    private boolean isNeedContent;

    // sweepAngle / maxValues 的值
    private float k;
    private int color1,color2,color3;

    public ColorArcProgressBar(Context context) {super(context, null);
        initView();
    }public ColorArcProgressBar(Context context, AttributeSet attrs) {super(context, attrs, 0);
        initCofig(context, attrs);
        initView();
    }public ColorArcProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);
        initCofig(context, attrs);
        initView();
    }/**
     * 初始化布局配置
     * @param context
     * @param attrs
     */
    private void initCofig(Context context, AttributeSet attrs) {TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ColorArcProgressBar);
        color1 = a.getColor(R.styleable.ColorArcProgressBar_front_color1, Color.GREEN);
        color2 = a.getColor(R.styleable.ColorArcProgressBar_front_color2, color1);
        color3 = a.getColor(R.styleable.ColorArcProgressBar_front_color3, color1);

        sweepAngle = a.getInteger(R.styleable.ColorArcProgressBar_total_engle, 240);
        bgArcWidth = a.getDimension(R.styleable.ColorArcProgressBar_back_width, dipToPx(2));
        progressWidth = a.getDimension(R.styleable.ColorArcProgressBar_front_width, dipToPx(10));
        isNeedTitle = a.getBoolean(R.styleable.ColorArcProgressBar_is_need_title, false);
        isNeedContent = a.getBoolean(R.styleable.ColorArcProgressBar_is_need_content, false);
        isNeedUnit = a.getBoolean(R.styleable.ColorArcProgressBar_is_need_unit, false);
        isNeedDial = a.getBoolean(R.styleable.ColorArcProgressBar_is_need_dial, false);
        hintString = a.getString(R.styleable.ColorArcProgressBar_string_unit);
        titleString = a.getString(R.styleable.ColorArcProgressBar_string_title);
        curValues = a.getFloat(R.styleable.ColorArcProgressBar_current_value, 0);
        maxValues = a.getFloat(R.styleable.ColorArcProgressBar_max_value, 60);
        setCurrentValues(curValues);
        setMaxValues(maxValues);
        a.recycle();

    }@Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {int width = (int) (2 * longdegree + progressWidth + diameter + 2 * DEGREE_PROGRESS_DISTANCE);
        int height= (int) (2 * longdegree + progressWidth + diameter + 2 * DEGREE_PROGRESS_DISTANCE);
        setMeasuredDimension(width, height);
    }private void initView() {diameter = 3 * getScreenWidth() / 5;
        //弧形的矩陣區域
        bgRect = new RectF();
        bgRect.top = longdegree + progressWidth/2 + DEGREE_PROGRESS_DISTANCE;
        bgRect.left = longdegree + progressWidth/2 + DEGREE_PROGRESS_DISTANCE;
        bgRect.right = diameter + (longdegree + progressWidth/2 + DEGREE_PROGRESS_DISTANCE);
        bgRect.bottom = diameter + (longdegree + progressWidth/2 + DEGREE_PROGRESS_DISTANCE);

        //圓心
        centerX = (2 * longdegree + progressWidth + diameter + 2 * DEGREE_PROGRESS_DISTANCE)/2;
        centerY = (2 * longdegree + progressWidth + diameter + 2 * DEGREE_PROGRESS_DISTANCE)/2;

        //外部刻度線
        degreePaint = new Paint();
        degreePaint.setColor(Color.parseColor(longDegreeColor));

        //整個弧形
        allArcPaint = new Paint();
        allArcPaint.setAntiAlias(true);
        allArcPaint.setStyle(Paint.Style.STROKE);
        allArcPaint.setStrokeWidth(bgArcWidth);
        allArcPaint.setColor(Color.parseColor(bgArcColor));
        allArcPaint.setStrokeCap(Paint.Cap.ROUND);

        //當前進度的弧形
        progressPaint = new Paint();
        progressPaint.setAntiAlias(true);
        progressPaint.setStyle(Paint.Style.STROKE);
        progressPaint.setStrokeCap(Paint.Cap.ROUND);
        progressPaint.setStrokeWidth(progressWidth);
        progressPaint.setColor(Color.parseColor(currentArcColor));

        //內容顯示文字
        vTextPaint = new Paint();
        vTextPaint.setTextSize(textSize);
        vTextPaint.setColor(color2);
        vTextPaint.setTextAlign(Paint.Align.CENTER);

        //顯示單位文字
        hintPaint = new Paint();
        hintPaint.setTextSize(hintSize);
        hintPaint.setColor(color3);
        hintPaint.setTextAlign(Paint.Align.CENTER);

        //顯示標題文字
        curSpeedPaint = new Paint();
        curSpeedPaint.setTextSize(curSpeedSize);
        curSpeedPaint.setColor(color1);
        curSpeedPaint.setTextAlign(Paint.Align.CENTER);

        mDrawFilter = new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
    }@Override
    protected void onDraw(Canvas canvas) {//抗鋸齒
        canvas.setDrawFilter(mDrawFilter);

        if (isNeedDial) {//畫刻度線
            for (int i = 0; i < 40; i++) {if (i > 15 && i < 25) {canvas.rotate(9, centerX, centerY);
                    continue;
                }if (i % 5 == 0) {degreePaint.setStrokeWidth(dipToPx(2));
                    degreePaint.setColor(Color.parseColor(longDegreeColor));
                    canvas.drawLine(centerX, centerY - diameter / 2 - progressWidth / 2 - DEGREE_PROGRESS_DISTANCE,
                            centerX, centerY - diameter / 2 - progressWidth / 2 - DEGREE_PROGRESS_DISTANCE - longdegree, degreePaint);
                } else {degreePaint.setStrokeWidth(dipToPx(1.4f));
                    degreePaint.setColor(Color.parseColor(shortDegreeColor));
                    canvas.drawLine(centerX, centerY - diameter / 2 - progressWidth / 2 - DEGREE_PROGRESS_DISTANCE - (longdegree - shortdegree) / 2,
                            centerX, centerY - diameter / 2 - progressWidth / 2 - DEGREE_PROGRESS_DISTANCE - (longdegree - shortdegree) / 2 - shortdegree, degreePaint);
                }canvas.rotate(9, centerX, centerY);
            }}//整個弧
        canvas.drawArc(bgRect, startAngle, sweepAngle, false, allArcPaint);
        //當前進度
        canvas.drawArc(bgRect, startAngle, currentAngle, false, progressPaint);

        if (isNeedContent) {canvas.drawText(MathUtil.mathTodecimaltwo(curValues/maxValues*10.88), centerX, centerY ,
                    vTextPaint);
        }if (isNeedUnit) {canvas.drawText(hintString, centerX, centerY+textSize/2, hintPaint);
        }if (isNeedTitle) {canvas.drawText(titleString, centerX, centerY -  textSize , curSpeedPaint);
        }invalidate();

    }/**
     * 設置最大值
     * @param maxValues
     */
    public void setMaxValues(float maxValues) {this.maxValues = maxValues;
        k = sweepAngle/maxValues;
    }/**
     * 設置當前值
     * @param currentValues
     */
    public void setCurrentValues(float currentValues) {if (currentValues > maxValues) {currentValues = maxValues;
        }if (currentValues < 0) {currentValues = 0;
        }this.curValues = currentValues;
        lastAngle = currentAngle;
        setAnimation(lastAngle, currentValues * k, aniSpeed);
    }/**
     * 設置整個圓弧寬度
     * @param bgArcWidth
     */
    public void setBgArcWidth(int bgArcWidth) {this.bgArcWidth = bgArcWidth;
    }/**
     * 設置進度寬度
     * @param progressWidth
     */
    public void setProgressWidth(int progressWidth) {this.progressWidth = progressWidth;
    }/**
     * 設置速度文字大小
     * @param textSize
     */
    public void setTextSize(int textSize) {this.textSize = textSize;
    }/**
     * 設置單位文字大小
     * @param hintSize
     */
    public void setHintSize(int hintSize) {this.hintSize = hintSize;
    }/**
     * 設置單位文字
     * @param hintString
     */
    public void setUnit(String hintString) {this.hintString = hintString;
        invalidate();
    }/**
     * 設置直徑大小
     * @param diameter
     */
    public void setDiameter(int diameter) {this.diameter = dipToPx(diameter);
    }/**
     * 設置標題
     * @param title
     */
    private void setTitle(String title){this.titleString = title;
    }/**
     * 設置是否顯示標題
     * @param isNeedTitle
     */
    private void setIsNeedTitle(boolean isNeedTitle) {this.isNeedTitle = isNeedTitle;
    }/**
     * 設置是否顯示單位文字
     * @param isNeedUnit
     */
    private void setIsNeedUnit(boolean isNeedUnit) {this.isNeedUnit = isNeedUnit;
    }/**
     * 設置是否顯示外部刻度盤
     * @param isNeedDial
     */
    private void setIsNeedDial(boolean isNeedDial) {this.isNeedDial = isNeedDial;
    }/**
     * 為進度設置動畫
     * @param last
     * @param current
     */
    private void setAnimation(float last, float current, int length) {progressAnimator = ValueAnimator.ofFloat(last, current);
        progressAnimator.setDuration(length);
        progressAnimator.setTarget(currentAngle);
        progressAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {@Override
            public void onAnimationUpdate(ValueAnimator animation) {currentAngle= (float) animation.getAnimatedValue();
                curValues = currentAngle/k;
            }});
        progressAnimator.start();
    }/**
     * dip 轉換成px
     * @param dip
     * @return
     */
    private int dipToPx(float dip) {float density = getContext().getResources().getDisplayMetrics().density;
        return (int)(dip * density + 0.5f * (dip >= 0 ? 1 : -1));
    }/**
     * 得到屏幕寬度
     * @return
     */
    private int getScreenWidth() {WindowManager windowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics displayMetrics = new DisplayMetrics();
        windowManager.getDefaultDisplay().getMetrics(displayMetrics);
        return displayMetrics.widthPixels;
    }
}

在mainActivity里面

private ColorArcProgressBar bar2;
bar2= (ColorArcProgressBar) view.findViewById(R.id.barInterest);
bar2.setCurrentValues(40);

就這樣完成了 哈哈哈!!!!

轉載于:https://my.oschina.net/u/3407708/blog/887914

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/282257.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/282257.shtml
英文地址,請注明出處:http://en.pswp.cn/news/282257.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

C# Task用法詳解

概述Task是微軟在.Net 4.0時代推出來的&#xff0c;Task看起來像一個Thread&#xff0c;實際上&#xff0c;它是在ThreadPool的基礎上進行的封裝&#xff0c;Task的控制和擴展性很強&#xff0c;在線程的延續、阻塞、取消、超時等方面遠勝于Thread和ThreadPool&#xff0c;所以…

函數調用堆棧圖

轉載于:https://www.cnblogs.com/DeeLMind/p/7617972.html

jquery運動

在前面封裝的move.js框架&#xff0c;在jquery中有同樣封裝好的功能animate()。使用方法非常類似&#xff0c;下面我們看看animate的使用方法&#xff0c;有了原生的運動方法&#xff0c;然后再使用jquery的運動方法就會變得非常簡單。 animate()語法 $(selector).animate({par…

Session的原理,大型網站中Session方面應注意什么?

一、Session和Cookie的區別 Session是在服務器端保持會話數據的一種方法&#xff08;通常用于pc端網站保持登錄狀態&#xff0c;手機端通常會使用token方式實現&#xff09;&#xff0c;存儲在服務端。 Cookie是在客戶端保持用戶數據&#xff0c;存儲位置是客戶端&#xff08…

MySQL5.5讀寫分離之mysql-proxy

通常一個網站在初期訪問量都比較小&#xff0c;所以一般的小架構足以支撐。但是&#xff0c;當網站逐漸發展起來后&#xff0c;隨之而來的是大量的訪問&#xff0c;這時候最先出現的瓶頸就是數據庫了。因為數據的寫入讀取操作&#xff08;I/O&#xff09;是集群中響應速度最慢的…

兩圓相交求面積 hdu5120

轉載 兩圓相交分如下集中情況&#xff1a;相離、相切、相交、包含。 設兩圓圓心分別是O1和O2&#xff0c;半徑分別是r1和r2&#xff0c;設d為兩圓心距離。又因為兩圓有大有小&#xff0c;我們設較小的圓是O1。 相離相切的面積為零&#xff0c;代碼如下&#xff1a; [cpp] view …

Python_list部分功能介紹

x.append():在列表尾部添加一個元素 x.clear():把列表清空 x.count():判斷某個元素出現的次數 x.extend():合并兩個列表&#xff0c;或者一個元組 x.index():獲取元素下標 x.insert():指定下標添加元素 x.pop():移除某一元素&#xff0c;移除的元素可獲取 x.remove():移除指定的…

一招解決開發環境問題 —— 遠程容器開發指南

前言使用C作為主要開發語言的程序猿們應該會認同搭建開發環境是一件煩人的事情。為了編譯一個程序不僅需要下載各種依賴包&#xff0c;還可能面臨本地系統不兼容、編譯器版本不一致、包版本沖突等各種問題。筆者在運營iLogtail開源社區的過程中發現開發和調試環境問題也是成員問…

php中常用的加密方式

一、md5 要說php中的最常用的加密方式&#xff0c;md5可以說是當仁不讓。 使用起來也很簡單便捷。 注&#xff1a;使用方式請看 六、md5加密方式的漏洞及解決方案 二、password_hash 作為php5.5以上版本專門用于加密的方式&#xff0c;自然有其獨到之處。 使用方式鏈接&a…

解決問題的策略-分而治之

一個宏偉的目標看上去遙不可及&#xff0c;這怎么可能做成呢&#xff1f;但是你把這些目標分解成一個一個的小目標&#xff0c;小目標再往下分解&#xff0c;分解到最后&#xff0c;分解成細枝末節時你會發現&#xff0c;這事其實是可以做的。這個做成了再往下走一步&#xff0…

UITabBarController的基本原理及使用(一)

前言 UITabBarController在iOS開發中是一個高頻使用的控制器&#xff0c;典型的案例如QQ、微信均使用UITabBarController布局。本文將從一個新建工程&#xff0c;和大家一起了解UITabBarController的基本原理和使用方法。 基本概念 UITabBarController能夠方便地管理多個控制器…

word-vba-microsoft(中英文)

中文 https://msdn.microsoft.com/zh-cn/vba/word-vba/articles/view-displaypageboundaries-property-word 英文 https://msdn.microsoft.com/en-us/vba/word-vba/articles/view-displaypageboundaries-property-word轉載于:https://www.cnblogs.com/itzxy/p/7625915.html

C# 多線程ThreadPool用法舉例

概述ThreadPool是.Net Framework 2.0版本中出現的。自從Task出來以后&#xff0c;ThreadPool已經很少用了&#xff0c;但是一些老的代碼或者一些古老的程序猿還是會用到他&#xff0c;所以我們可以不用它&#xff0c;但是還是有必須學習和了解他.ThreadPool用法舉例static void…

Mysql實現主從復制(一主雙從)

一、環境介紹 LNMP&#xff08;centos7&#xff0c;mysql5.6&#xff09; vmware workstation pro配置了3個虛擬機&#xff0c;均安裝了LNMP環境&#xff1a; master&#xff1a; 192.168.0.105 slave&#xff1a; 192.168.0.106 、192.168.0.107 二、原理 &a…

Elasticsearch學習筆記-04.3批處理

除了創建、更新和刪除個別文檔&#xff0c;Elasticsearch還提供了使用_bulk API的上述操作的批量操作方法。這個功能很重要&#xff0c;因為他提供了一種有效的機制來在盡可能少的網絡傳輸過程中執行多次操作。 作為一個快速示例&#xff0c;下面的命令在一次批量操作中索引了兩…

接口文檔神器Swagger(下篇)

本文來自網易云社區作者&#xff1a;李哲二、Swagger-springmvc原理解析上面介紹了如何將springmvc和springboot與swagger結合&#xff0c;通過簡單配置生成接口文檔&#xff0c;以及介紹了swagger提供的一些注解。下面將介紹swagger是如何做到與springmvc結合&#xff0c;自動…

php實現mysql分表

一、場景說明 1、為什么要進行分表 隨著數據量的不斷增大&#xff0c;一張表中的數據肯定也會越來越多&#xff0c;甚至達到百萬甚至千萬級。我們通常會通過搭建mysql集群&#xff08;主從同步&#xff09;&#xff0c;讀寫分離來實現優化數據庫查詢執行效率。 但是由于數據…

利用python進行數據分析D1——ch02引言

基礎的課程還沒學完&#xff0c;就來這本了&#xff0c;因為我平時的研究還是以數據的處理為主。把自己的事做好做細致讀了一下介紹部分&#xff0c;下載書里用到的數據&#xff0c;下載地址&#xff1a;https://github.com/wesm/pydata-book 如果你需要完成以下幾大類任務&…

記一次Memory Leak分析

起因&#xff1a;最近公司的一個web產品遇到了內存溢出&#xff0c;于是開始著手調查。調查&#xff1a;首先當務之急是找到那個或那些API導致Memory Leak&#xff0c;這個應該不難&#xff0c;根據監控分析&#xff0c;在內存上升時間段內有哪些API被訪問&#xff0c;再就是根…

【t057】任務分配

Time Limit: 1 second Memory Limit: 128 MB 【問題描述】 現有n個任務,要交給A和B完成。每個任務給A或給B完成&#xff0c;所需的時間分別為ai和bi。問他們完成所有的任務至少要多少時間。 【輸入格式】 第一行一個正整數n&#xff0c;表示有n個任務。 接下來有n行&#xf…