比分显示控件,自带两种动画

1.效果图

这是静态的,有两种动画,运行代码后能看到

2.代码

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

/**
 * 使用说明 <br/>
 * 1.XML定义<br/>
 * <com.longyuan.sdkdemo.CirclePieView <br/>
 * android:id="@+id/cpv" <br/>
 * android:layout_width="100dp" <br/>
 * android:layout_height="100dp" <br/>
 * android:layout_marginLeft="10dp" <br/>
 * android:layout_marginTop="10dp" > <br/>
 * </com.longyuan.sdkdemo.CirclePieView> <br/>
 * 2.设置比分<br/>
 * CirclePieView.setScore(4, 3, true, 1);<br/>
 * @author niexiaoqiang
 */
public class CirclePieView extends View {
	public int animationType = 1;
	public static final int E_C = 10;
	private int mWidth = 0;
	private int mHeight = 0;
	private int leftColor = Color.rgb(255, 255, 0);
	private int rightColor = Color.rgb(255, 0, 255);
	private int textBgColor = Color.rgb(255, 255, 255);
	private int bgColor = Color.rgb(0, 0, 0);
	//不能大于99
	private int leftScore = 0;
	//不能大于99
	private int rightScore = 0;
	private Paint textPaint;
	private Paint leftPaint;
	private Paint rightPaint;
	private Paint textBgPaint;
	private Paint bgPaint;
	private RectF drawRect = new RectF();
	private int circleWidth = 10;
	private int leftCircle = 180;
	private int rightCircle = 180;
	private int currentC = 360;
	//是否已经设置比分如果没有设置,则不绘制文字以及圆环
	private boolean initedScrore = false;
	private boolean stopped = true;

	private void init(Context context) {
		textPaint = new Paint();
		textPaint.setAntiAlias(true);
		textPaint.setColor(Color.BLACK);
		textPaint.setFakeBoldText(true);
		leftPaint = new Paint();
		leftPaint.setAntiAlias(true);
		leftPaint.setColor(leftColor);
		rightPaint = new Paint();
		rightPaint.setAntiAlias(true);
		rightPaint.setColor(rightColor);
		textBgPaint = new Paint();
		textBgPaint.setAntiAlias(true);
		textBgPaint.setColor(textBgColor);
		bgPaint = new Paint();
		bgPaint.setAntiAlias(true);
		bgPaint.setColor(bgColor);
		circleWidth = dip2px(context, 10);
	}

	public static int dip2px(Context context, float dipValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dipValue * scale + 0.5f);
	}

	/**
	 * 如果inited==false将不绘制比分,以及圆环
	 * @param initedScrore
	 */
	public void setInitedScrore(boolean initedScrore) {
		this.initedScrore = initedScrore;
		this.invalidate();
	}

	/**
	 * 调用该方法设置比分
	 * @param leftScore 左边的比分
	 * @param rightScore 右边的比分
	 * @param needAnimation 是否需要动画
	 * @param animationType 0: 旋转,1:展开
	 */
	public void setScore(int leftScore, int rightScore, boolean needAnimation, int animationType) {
		this.leftScore = leftScore;
		this.rightScore = rightScore;
		this.animationType = animationType;
		//计算占比
		if (leftScore == 0 && rightScore == 0) {
			leftCircle = 180;
		} else {
			leftCircle = (int) (((float) leftScore / (float) (leftScore + rightScore)) * (float) 360);
			leftCircle = leftCircle % 2 == 0 ? leftCircle : leftCircle - 1;
		}
		rightCircle = 360 - leftCircle;
		if (needAnimation) {
			currentC = 0;
			stopped = false;
		}
		setInitedScrore(true);
	}

	@Override
	public void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		//背景
		canvas.drawCircle(mWidth / 2, mHeight / 2, (mWidth - 2) / 2, bgPaint);
		if (initedScrore) {
			//画圆
			drawRect.set(0, 0, mWidth, mHeight);
			int currentRightCircle = rightCircle * currentC / 360;
			if (animationType == 0) {
				canvas.drawArc(drawRect, 180 - leftCircle / 2, currentC, true, leftPaint);
				if (360 - currentC <= currentRightCircle) {
					canvas.drawArc(drawRect, 0 - rightCircle / 2, currentRightCircle, true, rightPaint);
				}
			} else {
				//画左边时以180°为中心,上下分别进行一次
				canvas.drawArc(drawRect, 180, leftCircle / 2 * currentC / 360, true, leftPaint);
				canvas.drawArc(drawRect, 180, -leftCircle / 2 * currentC / 360, true, leftPaint);
				//画右边时候以0°为中心,上下分别进行一次
				canvas.drawArc(drawRect, 0, rightCircle / 2 * currentC / 360, true, rightPaint);
				canvas.drawArc(drawRect, 0, -rightCircle / 2 * currentC / 360, true, rightPaint);
			}
		}
		//绘制文字背景
		canvas.drawCircle(mWidth / 2, mHeight / 2, (mWidth - circleWidth * 2) / 2, textBgPaint);
		if (initedScrore) {
			drawText(canvas);
		}
		currentC += E_C;
		if (currentC < 360) {
			postInvalidate();
			return;
		}
		if (currentC > 360) {
			currentC = 360;
		}
		if (!stopped) {
			postInvalidate();
			stopped = true;
		}
	}

	/**
	 * 画出文字
	 * @param canvas
	 */
	private void drawText(Canvas canvas) {
		/最后画出文字//
		int allTextWidth = mWidth - circleWidth * 2;
		textPaint.setTextSize(allTextWidth / 4 * currentC / 360);
		FontMetrics fontMetrics = textPaint.getFontMetrics();
		float textBaseLine = mHeight / 2 - ((fontMetrics.top - fontMetrics.bottom) / 2 + fontMetrics.bottom);
		//冒号
		String mindleString = ":";
		float mindleTextWidth = textPaint.measureText(mindleString);
		float midleTextleft = (allTextWidth - mindleTextWidth) / 2 + circleWidth;
		canvas.drawText(mindleString, midleTextleft, textBaseLine, textPaint);
		//标线
		//		canvas.drawLine(midleTextleft, 0, midleTextleft, mHeight, rightPaint);
		//		canvas.drawLine(midleTextleft + mindleTextWidth, 0, midleTextleft + mindleTextWidth, mHeight, rightPaint);
		//左边文字
		String leftString = getLeftString();
		float leftTextWidth = textPaint.measureText(leftString);
		float leftTextLeft = (midleTextleft - leftTextWidth) / 2 + circleWidth / 2;
		canvas.drawText(leftString, leftTextLeft, textBaseLine, textPaint);
		//标线
		//		canvas.drawLine(leftTextLeft, 0, leftTextLeft, mHeight, rightPaint);
		//		canvas.drawLine(leftTextLeft + leftTextWidth, 0, leftTextLeft + leftTextWidth, mHeight, rightPaint);
		//右边的文字
		String rightString = getRightString();
		float rightTextWidth = textPaint.measureText(rightString);
		float rightTextLeft = (midleTextleft + mindleTextWidth) + (allTextWidth - (midleTextleft + mindleTextWidth) - rightTextWidth) / 2 + circleWidth / 2;
		canvas.drawText(rightString, rightTextLeft, textBaseLine, textPaint);
		//标线
		//		canvas.drawLine(rightTextLeft, 0, rightTextLeft, mHeight, rightPaint);
		//		canvas.drawLine(rightTextLeft + rightTextWidth, 0, rightTextLeft + rightTextWidth, mHeight, rightPaint);
	}

	private String getLeftString() {
		if (leftScore < 10) {
			return "0" + leftScore;
		}
		return leftScore + "";
	}

	private String getRightString() {
		if (rightScore < 10) {
			return "0" + rightScore;
		}
		return rightScore + "";
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		mHeight = getMeasuredHeight();
		mWidth = getMeasuredWidth();
	}

	public CirclePieView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		this.init(context);
	}

	public CirclePieView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.init(context);
	}

	public CirclePieView(Context context) {
		super(context);
		this.init(context);
	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
足球资讯网站源码可应用于世界杯新闻资讯、足球、体育赛事、美女图片等图片、文字、视频、音频类型的网站建设,搜索引擎排名效果好,承载数据量大,承载访问量大,安全性高,功能和模板可通过插件形式扩展。 程序特点: 1、与世界杯配套的功能模块: 世界杯直播地址列表 详细的赛程分析 实时比分调用 32强队伍列表 美女图片自动采集更新(高级功能) 世界杯新闻自动采集更新(高级功能) 可封装为APP(高级功能) 世界杯赛事结束之后依然可以作为足球、体育资讯、篮球、美女图片等类型的网站继续运行 2、自动化程度高: 自动设置文章第一张图片为缩略图 自动检测首次安装环境 自动补充页面空缺,文章列表条目自动伸缩 自动适配手机与电脑版,并可微信访问 自动生成导航栏二级下拉菜单 自动采集文章、自动发布文章、自动生成静态(高级功能) 自动定时发布(高级功能) 自动搜索引擎推送(高级功能) 自动图片本地保存(高级功能) 自动过滤危险提交(高级功能) 会员充值自动到账(高级功能) 2、一键更换的功能与模板 高级功能插件带来丰富的扩展,在后台一键安装 模板可随意切换更换,在后台一键安装 3、支持微信小程序、APP、百度MIP、微站 可扩展出微信小程序、APP、百度MIP、微站,一个后台进行管理(高级功能) 4、搜索引擎友好 整站具备静态化、动态化、伪静态化的功能 具备搜索引擎主动提交、自动提交、sitemap生成(高级功能) 整站写入标准的html标签,有利于SEO 整站可生成全局内链关键词 源码为试用版,正式使用请购买授权,购买授权即可解锁各种高级自动化的功能和模板。否则过段时间会提示“试用结束”的字样, 更多功能请亲自体验吧 后台登录地址:http://你的网址/admin 登录账号:admin 登录密码:admin

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值