效果图

实现

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. 【原创】IBM Websphere 报错:JSPG0120E: 为 pageEncoding 属性和匹配 URI 模式的配置元素指定不同的值是非法的。

    websphere中间件,在打开一个jsp页面时报: IBM Websphere 报错:JSPG0120E: 为 pageEncoding 属性和匹配 URI 模式的配置元素指定不同的值是非法的. . ...

  2. Caffe+UbuntuKylin14.04_X64+CUDA 6.5配置

    在编译Caffe的漫长过程中,经过了一个又一个坑,掉进去再爬出来,挺有趣的.对比原文有修改! LInux下配置安装:(本文档使用同一块NVIDIA显卡进行显示与计算, 如分别使用不同的显卡进行显示和计 ...

  3. 8 Python+Selenium操作测试对象

    [环境信息] Python3.6+selenium3.0.2+Firefox50.0+win7 [操作方法] 1.清除输入框内容:clear() 2.单击一个按钮:click() 3.返回元素尺寸:s ...

  4. 基于Nginx服务的用户认证

    通过Nginx实现web页面的用户认证,用户名为:admin,密码为:654321 1.修改Nginx配置文件 # vim /usr/local/nginx/conf/nginx.conf ..... ...

  5. BZOJ 1266: [AHOI2006]上学路线route Floyd_最小割

    十分简单的一道题. 图这么小,跑一边 Floyd 就得到第一问最短路径的答案. 考虑第二问怎么求:我们可以先将最短路径组成的图从原图中抽离出来,构成新图 $G$. 我们发现,只要 $G$ 的起点与终点 ...

  6. Angular之constructor和ngOnInit差异及适用场景(转)

    原始地址:https://blog.csdn.net/u010730126/article/details/64486997 Angular中根据适用场景定义了很多生命周期函数,其本质上是事件的响应函 ...

  7. 【BZOJ1367】【Baltic2004】sequence - 可合并堆

    题意: 题解: 其实这是道水题啦……只不过我没做过而已 先考虑构造不严格递增序列,考虑原序列中的一段下降区间,显然区间中的$z$全取中位数最优: 那么可以把原序列拆成很多个下降序列,从头到尾加入原序列 ...

  8. nyoj314-斐波那契数列四吧

    斐波那契数列四吧 时间限制:3000 ms  |  内存限制:65535 KB 难度:2 描述 斐波那契数列为:0,1,1,2,3,5,8,13....,常规递推公式为f(n)=f(n-1)+f(n- ...

  9. Linux磁盘分区--GPT分区

    MBR分区表有一定的局限性,最大支持2.1tb硬盘,单块硬盘最多4个主分区. 这里就要引入GPT分区表,可以支持最大18EB的卷,最多支持128个主分区,所以如果使用大于2tb的卷,就必须使用GTP分 ...

  10. C#使用 ComboBox 控件

    Combox控件是一个下拉选择的控件,再做上位机的时候会经常用到,这里记录一下我是怎么用. 1.拉出一个combox控件 2.控件属性选为不可编辑,可编辑的话,你选择下拉框的内容后可以改下拉框里的内容 ...