效果图

实现

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. Swift - 关键字(typealias、associatedtype)

    Typealias typealias 是用来为已经存在的类型重新定义名字的,通过命名,可以使代码变得更加清晰.使用的语法也很简单,使用typealias 关键字像使用普通的赋值语句一样,可以将某个已 ...

  2. .NET 请求和接收FormData的值

    <body> <div> <!-- 上传单个文件---> <form action="/Home/UpdateFile2" enctype ...

  3. GCJ 2008 Round 1A Minimum Scalar Product( 水 )

    链接:传送门 题意:给两个向量 v1 = { x1 , x2 , x3 , x4 .... } , v2 = { y1 , y2 , y3 , y4 ...... } 允许任意交换 v1 和 v2 各 ...

  4. [洛谷P4887]第十四分块(前体)

    题目大意: 给定一个长度为\(n\)的序列\(a\),\(k\),和\(m\)次询问. 每次询问给定区间\([l,r]\),求满足\(l\leqslant i< j\leqslant r\)且\ ...

  5. UID和GID(详细说明)

    一.UID(User Identify)中文用户ID,相当于身份证一样,在系统中是唯一的. 用户分类centos6超级用户 UID=0 root普通用户 UID=500起 oldboy虚拟用户 UID ...

  6. Nginx 安装配置证书,设置HTTPS站点

    详细配置如下: server { listen 80; server_name shwww.net www.shwww.net; return 301 https://www.shwww.net$re ...

  7. Python for Tkinter

    # tkinter常用组件- 按钮 - button(按钮组件) - RadioButton(单选框组件) - CheckButton(选择按钮组件) - Listbox(列表框组件) - 文本输入组 ...

  8. [Design]制作磨砂效果

    比较适合运用到网页或者APP的设计当中,推荐过来和飞特的朋友们一起分享学习了,先来看看最终的效果图吧 具体的制作步骤如下:

  9. (译)RabbitMQ ——“Hello World”

    原文地址:http://www.rabbitmq.com/tutorials/tutorial-one-dotnet.html 介绍 RabbitMQ是一个消息实体服务(broker):它接收及转发消 ...

  10. exFAT格式

    买了一个64GB的T卡,发如今Windows XP上格式化."文件系统"仅仅有exFAT选项. 用这个exFAT格式化还失败了. 给XP打上KB955704补丁,能够用exFAT格 ...