Android实现根据评分添加星级条
作者:摇啊摇曳瑶 时间:2021-10-30 13:19:57
简述
在仿写豆瓣的时候,发现了根据评分不同,星级数也不同的星级条。
百度一搜,发现Android有自带控件UIRatingBar,而iOS得要自己写…好吧,那就写吧。
图片素材
首先,要准备三张图片,图片如下:
空星,半星,全星
因为我们可以看到,在豆瓣的评分星级条里,只有空、半、全星,所以只需要准备这3种图片。
思路
豆瓣的星级条中既有图片,又有文字,所以我们自定义一个继承于UIView的starView。
初始化方法
因为星级条要根据评分的数据来决定星的颗数,所以我们要重新创建一个初始化方法:
//在starView.m中写
- (instancetype)initWithFrame:(CGRect)frame score:(double)score;
//在starView.h中对其进行操作实现
- (instancetype)initWithFrame:(CGRect)frame score:(double)score{
self = [super initWithFrame:frame];
//记得把传过来的score赋值给全局变量_starScore
_starScore = score;
return self;
}
这样,我们就可以在ViewController.m中利用此方法初始化一个星级条视图:
starView *star = [[CJTStarView alloc] initWithFrame:CGRectMake(100, 100, 200, 50) score:6.8];
此处的score可以改成根据网络请求得到的评分数据。
根据添加星星图片
在这里,我设置的分数与星星的对应关系如下:
4.6-5.5 2.5颗星
5.6-6.5 3颗星
6.6-7.5 3.5颗星
7.6-8.5 4颗星
8.6-9.5 4.5颗星
而因为我们只有5颗星,所以对分数做如下处理:
_starScore = (_starScore / 2 - 0.3);
接下来就是用循环添加图片到view上,因为我们有三种图片,所以在循环中还要加判断,代码如下:
for (int count = 0; count < 5; count++) {
UIImageView *starImageView = [[UIImageView alloc] init];
starImageView.frame = CGRectMake(count * self.frame.size.height, 0, self.frame.size.height, self.frame.size.height);
[self addSubview:starImageView];
if (count <= _starScore - 0.5) {
starImageView.image = [UIImage imageNamed:@"stars_full"];
} else {
if (_starScore - count >= 0 && _starScore - count < 0.5) {
starImageView.image = [UIImage imageNamed:@"stars_half"];
} else {
starImageView.image = [UIImage imageNamed:@"stars_empty"];
}
}
}
这里的判断条件是数学问题,就不详细讲了。
当然,如果分数与星星的对应规则和我不同,那么就要适当修正这里的判断条件。
在星级条后添加分数
在豆瓣到星级条后面还有分数,因此我们在view中添加一个UILabel对象。
UILabel *scoreLabel = [[UILabel alloc] init];
scoreLabel.frame = CGRectMake( 5 * self.frame.size.height + 10, 8, self.frame.size.width - 5 * self.frame.size.height - 10, self.frame.size.height - 8);
scoreLabel.text = [NSString stringWithFormat:@"%.1f", _starScore];
scoreLabel.textColor = [UIColor grayColor];
scoreLabel.font = [UIFont systemFontOfSize:25];
这里要注意,因为我们在设置星级图的时候会修改_starScore的值,所以要在添加星星图片之前设置UILabel。
效果图
最后做出来到效果如下:
来源:https://blog.csdn.net/weixin_43900761/article/details/102644783
标签:Android,评分,星级条
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Spring事务失效问题分析及解决方案
2023-07-09 11:08:15
详解使用JavaCV/OpenCV抓取并存储摄像头图像
2023-05-08 07:23:31
Android自定义TextView跑马灯效果
2023-08-07 01:14:57
在IDEA中安装MyBatis Log Plugin插件,执行mybatis的sql语句(推荐)
2022-02-17 08:04:43
![](https://img.aspxhome.com/file/2023/9/128929_0s.jpg)
java 较大数据量取差集,list.removeAll性能优化详解
2022-11-06 16:52:15
OpenXml读写Excel实例代码
2023-09-10 18:11:22
SpringBoot的@Value注解如何设置默认值
2023-09-03 14:32:05
![](https://img.aspxhome.com/file/2023/9/97989_0s.png)
Android编程实现画板功能的方法总结【附源码下载】
2023-08-05 10:45:07
![](https://img.aspxhome.com/file/2023/6/126736_0s.png)
Servlet中/和/*的区别详解
2022-07-11 03:21:33
![](https://img.aspxhome.com/file/2023/2/65772_0s.png)
Java基于swing实现的弹球游戏代码
2023-09-15 05:13:49
![](https://img.aspxhome.com/file/2023/8/116198_0s.jpg)
设置JavaScript自动提示-Eclipse/MyEclipse
2022-06-15 12:41:05
![](https://img.aspxhome.com/file/2023/8/84628_0s.jpg)
c#文件的复制,移动,创建(实例代码)
2023-05-29 21:49:14
Selenium+Tesseract-OCR智能识别验证码爬取网页数据的实例
2023-04-11 19:43:01
SpringMVC实现文件上传与下载
2021-11-02 11:53:38
SpringBoot浅析安全管理之OAuth2框架
2022-10-12 17:17:04
![](https://img.aspxhome.com/file/2023/7/61847_0s.png)
C#中ListView控件实现窗体代码
2023-02-04 02:55:06
Spring Cache和EhCache实现缓存管理方式
2023-01-17 01:44:40
![](https://img.aspxhome.com/file/2023/7/64927_0s.png)
在Android界面上显示和获取Logcat日志输出的方法
2023-02-19 20:02:19
![](https://img.aspxhome.com/file/2023/7/106017_0s.png)
Java实现二叉树的深度优先遍历和广度优先遍历算法示例
2021-07-16 12:37:17
![](https://img.aspxhome.com/file/2023/7/84177_0s.jpg)
Spring BeanPostProcessor接口使用详解
2022-12-18 09:19:36