android自定义View之(六)------高仿华为荣耀3C的圆形刻度比例图(ShowPercentView)

为什么写这篇文章:

显示当前的容量所占的比例,表现当前计划的进度,一般都会采用百分比的方式,而图形显示,以其一目了然的直观性和赏心悦目的美观形成为了我们的当然的首选。

在图形表示百分比的方法中,我们有用画圆的圆形进度条的方法<<android自定义View之(二)------简单进度条显示样例篇>>,也有用画弧形的进度条的方法<<android自定义View之(三)------视频音量调控样例>>,今天看到华为荣耀3C的一个界面:

个人觉得这个表示比例的圆形刻度圆有一种小清新的感觉,也觉得不难,所以就把他实现了:

效果图:

详细代码:

(1)定义属性(res/values/attr.xml)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="ShowPercentView">
        <attr name="percent" format="integer"></attr>
        <attr name="allLineColor" format="color" />
        <attr name="percentLineColorLow" format="color" />
        <attr name="percentLineColorHight" format="color" />
    </declare-styleable>
</resources>

(2)自定义圆形刻度比例图(ShowPercentView)

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class ShowPercentView extends View{

	private Paint percentPaint;

	private Paint textPaint;
	private int textSize = 30;

	private int percent;
	private int allLineColor;
	private int percentLineColorLow;
	private int percentLineColorHight;

	private int allLineWidth = 2;
	private int percentLineWidth = 4;
	private int lineHeight = 10;

	public ShowPercentView(Context context) {
		super(context);
		init(null, 0);
	}  

	public ShowPercentView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs, 0);
    }  

    public ShowPercentView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs, defStyle);
    }  

	private void init(AttributeSet attrs, int defStyle) {
		// TODO Auto-generated method stub
		final TypedArray a = getContext().obtainStyledAttributes(
                attrs, R.styleable.ShowPercentView, defStyle, 0);
		percent = a.getInt(R.styleable.ShowPercentView_percent, 0);
		allLineColor = a.getColor(R.styleable.ShowPercentView_allLineColor, Color.GRAY);
		percentLineColorLow = a.getColor(R.styleable.ShowPercentView_percentLineColorLow, Color.GREEN);
		percentLineColorHight = a.getColor(R.styleable.ShowPercentView_percentLineColorHight, Color.RED);

        a.recycle();  

        percentPaint = new Paint();
        percentPaint.setAntiAlias(true);

        textPaint = new Paint();
        textPaint.setTextSize(textSize);
        textPaint.setAntiAlias(true);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);

		int width = getMeasuredWidth();
        int height = getMeasuredHeight();
        int pointX =  width/2;
        int pointY = height/2; 		

		float textWidth = textPaint.measureText(percent + "%");
        if(percent < 50){
            //textPaint.setColor(oxbf3800);
            textPaint.setColor(Color.BLACK);
        }else{
            //textPaint.setColor(new Color(ox6ec705));
            textPaint.setColor(Color.RED);
        }
        canvas.drawText(percent+"%",pointX - textWidth/2,pointY + textPaint.getTextSize()/2 ,textPaint);  

        percentPaint.setColor(allLineColor);
        percentPaint.setStrokeWidth(allLineWidth);

        float degrees = (float) (320.0/100);

    	canvas.save();
    	canvas.translate(0,pointY);
    	canvas.rotate(-70, pointX, 0);
        for(int i = 0;i<100;i++){
        	canvas.drawLine(0, 0, lineHeight, 0, percentPaint);
        	canvas.rotate(degrees, pointX, 0);
        }
        canvas.restore(); 

        if(percent<60){
        	percentPaint.setColor(percentLineColorLow);
        }else{
        	percentPaint.setColor(percentLineColorHight);
        }

        percentPaint.setStrokeWidth(percentLineWidth);
    	canvas.save();
    	canvas.translate(0,pointY);
    	canvas.rotate(-70, pointX, 0);
        for(int i = 0;i<percent;i++){
        	canvas.drawLine(0, 0, lineHeight, 0, percentPaint);
        	canvas.rotate(degrees, pointX, 0);
        }
        canvas.restore();
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
		//super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        int d = (width >= height) ? height : width;
        setMeasuredDimension(d,d);
	}

	public void setPercent(int percent) {
		// TODO Auto-generated method stub
		this.percent = percent;
		postInvalidate();
	}
}

(3)activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <com.example.showpercentview.ShowPercentView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:id="@+id/myShowPercentView"
        android:background="#fdda6f"
        app:percent="82"
        app:allLineColor="#ebebeb"
        app:percentLineColorLow="#8acb55"
        app:percentLineColorHight="#8acb55"
    /> 

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="set_percent_40"
        android:id="@+id/set_percent_40"
        android:layout_below="@+id/myShowPercentView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />  

 	<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="set_percent_80"
        android:id="@+id/set_percent_80"
        android:layout_below="@+id/set_percent_40"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />  

</RelativeLayout>

(4)MainActivity.java

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.Activity;

public class MainActivity extends Activity implements OnClickListener {

	private ShowPercentView myShowPercentView;
    private Button set_percent_40;
    private Button set_percent_80;  

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		init();
	}

	private void init() {
		// TODO Auto-generated method stub
		myShowPercentView = (ShowPercentView) findViewById(R.id.myShowPercentView);
		set_percent_40 = (Button) findViewById(R.id.set_percent_40);
		set_percent_40.setOnClickListener(this);
		set_percent_80 = (Button) findViewById(R.id.set_percent_80);
		set_percent_80.setOnClickListener(this);
	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		 if(view == set_percent_40){
			 myShowPercentView.setPercent(40);
		 }else if(view == set_percent_80){
			 myShowPercentView.setPercent(80);
		 }
	}
}

源码下载:

时间: 2024-08-02 11:02:46

android自定义View之(六)------高仿华为荣耀3C的圆形刻度比例图(ShowPercentView)的相关文章

【Android自定义View实战】之仿百度加载动画,一种优雅的Loading方式

转载请注明出处:http://blog.csdn.net/linglongxin24/article/details/53470872 本文出自[DylanAndroid的博客] Android自定义View实战之仿百度加载动画一种优雅的Loading方式 第一个仿百度加载动画用ObjectAnimator属性动画操作ImageView的属性方法实现 第二个仿百度加载动画第二种实现方式用ValueAnimator原生的ondraw方法实现 第三个扔球动画-水平旋转动画 第四个扔球动画-垂直旋转动

【Android自定义View实战】之仿QQ运动步数圆弧及动画,Dylan计步中的控件StepArcView

转载请注明出处:http://blog.csdn.net/linglongxin24/article/details/52936609[DylanAndroid的csdn博客] 在之前的Android超精准计步器开发-Dylan计步中的首页用到了一个自定义控件,和QQ运动的界面有点类似,还有动画效果,下面就来讲一下这个View是如何绘制的. 1.先看效果图 2.效果图分析 功能说明:黄色的代表用户设置的总计划锻炼步数,红色的代表用户当前所走的步数. 初步分析:完全自定义View重写onDraw(

【朝花夕拾】Android自定义View篇之(六)Android事件分发机制(中)从源码分析事件分发逻辑及经常遇到的一些“诡异”现象

前言 转载请注明,转自[https://www.cnblogs.com/andy-songwei/p/11039252.html]谢谢! 在上一篇文章[[朝花夕拾]Android自定义View篇之(五)Android事件分发机制(上)Touch三个重要方法的处理逻辑][下文简称(五),请先阅读完(五)再阅读本文],我们通过示例和log来分析了Android的事件分发机制.这些,我们只是看到了现象,如果要进一步了解事件分发机制,这是不够的,我们还需要透过现象看本质,去研究研究源码.本文将从源码(基

android自定义View之仿通讯录侧边栏滑动,实现A-Z字母检索

我们的手机通讯录一般都有这样的效果,如下图: OK,这种效果大家都见得多了,基本上所有的Android手机通讯录都有这样的效果.那我们今天就来看看这个效果该怎么实现. 一.概述 1.页面功能分析 整体上来说,左边是一个ListView,右边是一个自定义View,但是左边的ListView和我们平常使用的ListView还有一点点不同,就是在ListView中我对所有的联系人进行了分组,那么这种效果的实现最常见的就是两种思路: 1.使用ExpandableListView来实现这种分组效果 2.使

Android特效专辑(六)——仿QQ聊天撒花特效,无形装逼,最为致命

Android特效专辑(六)--仿QQ聊天撒花特效,无形装逼,最为致命 我的关于特效的专辑已经在CSDN上申请了一个专栏--http://blog.csdn.net/column/details/liuguilin.html 日后我所写的特效专辑也会以一添加在这个专栏上,今天写的这个特效,是关于聊天的,你肯定遇到过,就是你跟人家聊天的时候,比如发送应(么么哒),然后屏幕上全部就是表情了,今天我们就是做这个,撒花的特效,国际惯例,上图 截图 实现这样的效果,你要知道贝塞尔曲线,何谓贝塞尔曲线?其实

Android ActionBar应用实战,高仿微信主界面的设计

转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/26365683 经过前面两篇文章的学习,我想大家对ActionBar都已经有一个相对较为深刻的理解了.唯一欠缺的是,前面我们都只是学习了理论知识而已,虽然知识点已经掌握了,但是真正投入到项目实战当中时会不会掉链子还很难说.那么不用担心,本篇文章我就将带领大家一起进入ActionBar的应用实战,将理论和实践完美结合到一起. 如果你还没有看过我的前两篇文章,建议先去阅读一下 Andr

Android 自定义View合集

自定义控件学习 https://github.com/GcsSloop/AndroidNote/tree/master/CustomView 小良自定义控件合集 https://github.com/Mr-XiaoLiang 自定义控件三部曲 http://blog.csdn.net/harvic880925?viewmode=contents Android 从0开始自定义控件之View基础知识与概念 http://blog.csdn.net/airsaid/article/details/5

android 自定义view 前的基础知识

本篇文章是自己自学自定义view前的准备,具体参考资料来自 Android LayoutInflater原理分析,带你一步步深入了解View(一) Android视图绘制流程完全解析,带你一步步深入了解View(二) Android视图状态及重绘流程分析,带你一步步深入了解View(三) Android自定义View的实现方法,带你一步步深入了解View(四) 这位大哥的系列博文,相当于自己看这些的一个思考吧. 一.首先学layoutInflater. 相信接触Android久一点的朋友对于La

【朝花夕拾】Android自定义View篇之(八)多点触控(上)基础知识

前言 转载请声明,转自[https://www.cnblogs.com/andy-songwei/p/11155259.html],谢谢! 在前面的文章中,介绍了不少触摸相关的知识,但都是基于单点触控的,即一次只用一根手指.但是在实际使用App中,常常是多根手指同时操作,这就需要用到多点触控相关的知识了.多点触控是在Android2.0开始引入的,在现在使用的Android手机上都是支持多点触控的.本系列文章将对常见的多点触控相关的重点知识进行总结,并使用多点触控来实现一些常见的效果,从而达到将