效果图

实现

package com.easypass.carstong.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View; import com.easypass.carstong.R; /**
* Created by huangbo on 2017/8/1.
*/ public class ViewStar extends View {
public static final int MAX_STAR = ; public ViewStar(@NonNull Context context) {
this(context, null);
} public ViewStar(@NonNull Context context, @Nullable AttributeSet attrs) {
this(context, attrs, );
} public ViewStar(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.StarView);
mRating = typedArray.getFloat(R.styleable.StarView_rating, );
typedArray.recycle();
init();
} Paint paint;
Bitmap starYellow;
Bitmap starGray;
float mRating;
int starWidth;
int starHeight;
int gap; private void init() {
paint = new Paint();
starYellow = BitmapFactory.decodeResource(getResources(), R.mipmap.rating_star_yellow);
starGray = BitmapFactory.decodeResource(getResources(), R.mipmap.rating_star);
starWidth = starYellow.getWidth();
starHeight = starYellow.getHeight();
gap = ;
invalidate();
} public void setRating(float rating) {
this.mRating = rating;
invalidate();
} public void setGrayStar(int resId, int alpha) {
starGray = BitmapFactory.decodeResource(getResources(), resId);
invalidate();
} @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int width = (getPaddingLeft() + (starWidth + gap) * MAX_STAR + getPaddingRight());
int height = (getPaddingTop() + starHeight + getPaddingBottom());
setMeasuredDimension(widthMode == MeasureSpec.EXACTLY ? widthSize : width,
heightMode == MeasureSpec.EXACTLY ? heightSize : height); } @Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
float currentRating = mRating < ? : (mRating > MAX_STAR ? MAX_STAR : mRating);
int mLeft = ;
int mTop = ;
int full = (int) currentRating;/*整个星星的数量*/
/**
* 画黄色的整颗星
*/
for (int i = ; i < full; i++) {
canvas.drawBitmap(starYellow, mLeft, mTop, paint);
mLeft = mLeft + starWidth + gap;
} if (currentRating == MAX_STAR) {
return;
}
/**
* 画灰色的整颗星
*/
for (int i = full; i < MAX_STAR; i++) {
canvas.drawBitmap(starGray, mLeft, mTop, paint);
mLeft = mLeft + starWidth + gap;
} /**
* 画小数点部分的星
*/
float part = mRating - full;
if (part > ) {
int w = (int) (part * starWidth);
Bitmap partBitmap = Bitmap.createBitmap(starYellow, , , w, starYellow.getHeight());
canvas.drawBitmap(partBitmap, full * (starWidth + gap), mTop, paint);
} }
}

使用方法

1.在布局文件中使用

<your.package.name.ViewStar
android:layout_width="wrap_content"
app:rating="2.5"
android:layout_height="wrap_content"/>

2.在代码中使用

ViewStar star=new ViewStar(this);
star.setRating(1.5);

Android 自定义简单控件--星级评价的更多相关文章

  1. Android自定义日历控件(继承系统控件实现)

    Android自定义日历控件(继承系统控件实现) 主要步骤 编写布局 继承LinearLayout设置子控件 设置数据 继承TextView实现有圆圈背景的TextView 添加Attribute 添 ...

  2. Android自定义用户控件简单范例(二)

    对于完全由后台定制的控件,并不是很方便其他人的使用,因为我们常常需要看到控件放到xml界面上的效果,并根据效果进行布局的调整,这就需要一个更加标准的控件制作流程: 我们的自定义控件和其他的控件一样,应 ...

  3. Android 开源简单控件

    Android开源系列分类 查看 CircleImageView 自定义圆形控件的使用 添加依赖 ‘de.hdodenhof:circleimageview:2.1.0' 作用:无论你设置的图片是什么 ...

  4. android自定义倒计时控件示例

    这篇文章主要介绍了Android秒杀倒计时自定义TextView示例,大家参考使用吧 自定义TextView控件TimeTextView代码: 复制代码 代码如下: import android.co ...

  5. Android自定义组合控件详细示例 (附完整源码)

    在我们平时的Android开发中,有时候原生的控件无法满足我们的需求,或者经常用到几个控件组合在一起来使用.这个时候,我们就可以根据自己的需求创建自定义的控件了,一般通过继承View或其子类来实现. ...

  6. 014 Android 自定义组合控件

    1.需求介绍 将已经编写好的布局文件,抽取到一个类中去做管理,下次还需要使用类似布局时,直接使用该组合控件的对象. 优点:可复用. 例如要重复利用以下布局: <RelativeLayout an ...

  7. Android自定义用户控件简单范例(一)

    一款优秀的移动应用需要具有自己独特统一的风格,通常情况下UI设计师会根据产品需求和使用人群的特点,设计整体的风格,界面的元素和控件的互效果.而原生态的Android控件为开发人员提供的是最基本的积木元 ...

  8. (转)android自定义组合控件

    原文地址:http://mypyg.iteye.com/blog/968646 目标:实现textview和ImageButton组合,可以通过Xml设置自定义控件的属性. 1.控件布局:以Linea ...

  9. Android自定义评分控件:RatingStarView

    RatingStarView Android自定义的评分控件,类似ProgressBar那样的,使用星星图标(full.half.empty)作为progress标识的评分/打分控件. 效果图 图1: ...

随机推荐

  1. Nagios Windows客户端NSClient++ 0.4.x安装配置

    NSClient++ 0.3.x和NSClient++ 0.4.x的配置完全不一样,官方的文档也没有全部更新.我记录下自己的一些操作.   一.下载安装NSClient++ 1.到http://nsc ...

  2. 科学存储数据格式-HDF5

    HDF数据格式 Hierarchical Data Format,可以存储不同类型的图像和数码数据的文件格式,并且可以在不同类型的机器上传输,同时还有统一处理这种文件格式的函数库.大多数普通计算机都支 ...

  3. 文件系统VFS数据结构(超级块 inode dentry file)(收集整理)

    Linux虚拟文件系统四大对象: 1)超级块(super block) 2)索引节点(inode) 3)目录项(dentry) 4)文件对象(file) 一个进程在对一个文件进行操作时各种对象的引用过 ...

  4. PhotoZoom Classic 7有什么用?高品质的放大模糊图片!

    PhotoZoom Classic 7专门用于放大照片,同时保持质量.该软件配备了BenVista独特的S-Spline技术,可轻松超越Photoshop的双三次插值等替代解决方案. PhotoZoo ...

  5. 一文带您了解5G的价值与应用

    一文带您了解5G的价值与应用 5G最有趣的一点是:大多数产品都是先有明确应用场景而后千呼万唤始出来.而5G则不同,即将到来的5G不仅再一次印证了科学技术是第一生产力还给不少用户带来了迷茫——我们为什么 ...

  6. CF div2 499 A. Stages

    Code: #include<cstdio> #include<algorithm> #include<iostream> using namespace std; ...

  7. 网络教程(7)OSI模型的低层模型

    OSI Model——Open System Interconnection Model 开放系统互联模型

  8. vue 函数配置项watch以及函数 $watch 源码分享

    Vue双向榜单的原理     大家都知道Vue采用的是MVVM的设计模式,采用数据驱动实现双向绑定,不明白双向绑定原理的需要先补充双向绑定的知识,在watch的处理中将运用到Vue的双向榜单原理,所以 ...

  9. Python 安装 numpy 以及 matplotlib 的过程

    系统:ubuntu 16.04 版本:Python3.5 步骤: 安装 pip sudo apt install python3-pip 查看 pip list 是否有 numpy 以及 matplo ...

  10. XPath语法简明介绍

    简介: XPath 是一门在 XML 文档中查找信息的语言.XPath 用于在 XML 文档中通过元素和属性进行导航. XPath 路径表达式: XPath 使用路径表达式来选取 XML 文档中的节点 ...