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: ...
随机推荐
- angular 常用写法
1.ng-repeat 数组数据中,不允许数组中有相同的两个数据,这个时候用下标去管理数据便可以解决这个问题 ng-repeat="item in list track by $index& ...
- mac 安装卸载python
第 1 步,删除框架: sudo rm -rf /Library/Frameworks/Python.framework/Versions/x.x第 2步,删除应用目录: sudo rm -rf &q ...
- Airtest多设备跑
一. 一个脚本对应一台设备 核心点:组织运行命令:将组织好的命令传到pool进程池(注意:是进程池,不是线程池,python的线程池不是同步执行,是按序执行) 以下不需要看,为私人项目备份目的. ...
- babel把ES6转化为ES5的时候报错
Module not found: Error: Can't resolve '@babel/runtime/helpers/asyncToGenerator' in 'e:\Node.js\Node ...
- 01.Python基础-1.Python简介及基础
python简介 python简介 python是一种面向对象的解释型计算机程序设计语言,由荷兰人Guido van Rossum(吉多·范罗苏姆)于1989年发明,第一个公开发行版发行于1991年. ...
- Vue接口日常学习
最近使用uni.app 进行app的开发 页面搭完之后,发现不会调接口,今天学习了下 各个程序运行时,都会发起网络请求,网络相关的API在使用之前都会在使用前配置域名白名单 首先 现在中间件上一 ...
- 用chrony代替ntpd时间同步服务器
Chrony是一个开源的自由软件,它能保持系统时钟与时钟服务器(NTP)同步,让时间保持精确. 它由两个程序组成:chronyd和chronyc. chronyd是一个后台运行的守护进程,用于调整内核 ...
- RobotFrameWork+APPIUM实现对安卓APK的自动化测试----第五篇【AppiumLibrary校验函数介绍】
http://blog.csdn.net/deadgrape/article/details/50619050 以上连作者先跪一下方便面,在上一篇中,作者遗漏了两个常用的函数: 1.长按 Long P ...
- 判断webservice是否可用
在.net中验证WebService的Url有效并且验证服务可用: 需要用到win32下的组件,比如Microsoft XML, v5.0 测试程序具体如下:建一个项目,在你的引用中添加COM---找 ...
- WinSCP介绍、安装、使用
前言 如果说XManager通过Xshell.Xftp可以很方便的进行远程管理,那么PuTTY显然不能满足我们的需求,所以这也是今天要介绍的另外一个工具-WinSCP. 简介 WinSCP是一个Win ...