Android 自定义简单控件--星级评价
效果图
实现
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 自定义简单控件--星级评价的更多相关文章
- Android自定义日历控件(继承系统控件实现)
Android自定义日历控件(继承系统控件实现) 主要步骤 编写布局 继承LinearLayout设置子控件 设置数据 继承TextView实现有圆圈背景的TextView 添加Attribute 添 ...
- Android自定义用户控件简单范例(二)
对于完全由后台定制的控件,并不是很方便其他人的使用,因为我们常常需要看到控件放到xml界面上的效果,并根据效果进行布局的调整,这就需要一个更加标准的控件制作流程: 我们的自定义控件和其他的控件一样,应 ...
- Android 开源简单控件
Android开源系列分类 查看 CircleImageView 自定义圆形控件的使用 添加依赖 ‘de.hdodenhof:circleimageview:2.1.0' 作用:无论你设置的图片是什么 ...
- android自定义倒计时控件示例
这篇文章主要介绍了Android秒杀倒计时自定义TextView示例,大家参考使用吧 自定义TextView控件TimeTextView代码: 复制代码 代码如下: import android.co ...
- Android自定义组合控件详细示例 (附完整源码)
在我们平时的Android开发中,有时候原生的控件无法满足我们的需求,或者经常用到几个控件组合在一起来使用.这个时候,我们就可以根据自己的需求创建自定义的控件了,一般通过继承View或其子类来实现. ...
- 014 Android 自定义组合控件
1.需求介绍 将已经编写好的布局文件,抽取到一个类中去做管理,下次还需要使用类似布局时,直接使用该组合控件的对象. 优点:可复用. 例如要重复利用以下布局: <RelativeLayout an ...
- Android自定义用户控件简单范例(一)
一款优秀的移动应用需要具有自己独特统一的风格,通常情况下UI设计师会根据产品需求和使用人群的特点,设计整体的风格,界面的元素和控件的互效果.而原生态的Android控件为开发人员提供的是最基本的积木元 ...
- (转)android自定义组合控件
原文地址:http://mypyg.iteye.com/blog/968646 目标:实现textview和ImageButton组合,可以通过Xml设置自定义控件的属性. 1.控件布局:以Linea ...
- Android自定义评分控件:RatingStarView
RatingStarView Android自定义的评分控件,类似ProgressBar那样的,使用星星图标(full.half.empty)作为progress标识的评分/打分控件. 效果图 图1: ...
随机推荐
- AngularJS指令进阶 -- ngModelController详解
大家都知道AngularJS中的指令是其尤为复杂的一个部分,但是这也是其比较好玩的地方.这篇文章我们就来说一说如何在我们自定义的指令中,利用ngModel的controller来做双向数据绑定,本文对 ...
- RabbitMQ学习之spring-amqp的重要类的认识
对于大多数应用来说都做了与spring整合,对于rabbitmq来说.也有与spring的整合.可能通过spring的官网找到spring-amqp项目下载.spring-amqp项目包括三个子项目: ...
- 路飞学城Python-Day137
django项目二 个人博客系统 github地址:https://github.com/pandaboy1123/cnblog
- node——express实现hello world
创建文件夹,在文件夹内再创建index.js 1.package.json npm init -y 2.安装 npm install express ---save 3.index.js //入口文件 ...
- sh脚本文件
echo `date +%F` 打印当前日期 用于写备份脚本按照名字命名
- python面向对象的三大特性之一多态
多态 多态的特性是调用不同的子类将会产生不同的行为,而无需明确知道这个子类实际上是什么 说白了就是,不同的对象调用相同的方法,产生不同的行为 例如:s1是字符串类型,w1是列表,两个完全不同的对象,他 ...
- nyoj2-吝啬的国度
吝啬的国度 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 在一个吝啬的国度里有N个城市,这N个城市间只有N-1条路把这个N个城市连接起来.现在,Tom在第S号城市,他有 ...
- 解决SpringBoot+JPA中使用set方法时自动更新数据库问题
项目进行了三分之二了,突然出现一个很诡异的bug,数据库存储的用户表中密码信息总是自动消失一部分,头疼了几天后突然想起同事有个对低权限用户查询的用户信息向前台传送时会把密码设成null后再传输,心想是 ...
- java-org.dom4j常用api介绍
//导入必要的包 import org.dom4j.Document;//Document文档类 import org.dom4j.Element//元素节点类 import org.dom4j.QN ...
- 敏捷开发-srcum
SCRUM框架包括3个角色.3个工件.5个活动.5个价值 3个角色 1.产品负责人(Product Owner) 2.Scrum Master 3.Scrum团队 3个工具 1.Product Bac ...