Libgdx学习笔记:UI之评分组件
2015-12-30 14:48
741 查看
评分组件。
使用方法:
// 星星上限,亮图,背景图 CHRatingBar chRatingBar = new CHRatingBar(5, CHRes.asset.starsp_png, CHRes.asset.starspgray_png); addActor(chRatingBar); // 星星进度 chRatingBar.setRating(1.5f);
代码展示;
package com.oahcfly.chgame.core.ui; import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.Batch; import com.badlogic.gdx.scenes.scene2d.Actor; import com.oahcfly.chgame.core.Chao; import com.oahcfly.chgame.core.map.CHTiledActor; /** * * 用星型来显示等级评定 * @author haocao * */ public class CHRatingBar extends Actor { private float numStars; private float rating; private CHTiledActor bgTiledActor; private Texture lightTexture, bgTexture; /** * * @param max 最大数量 * @param lightPngPath * @param bgPngPath */ public CHRatingBar (float max, String lightPngPath, String bgPngPath) { this.lightTexture = Chao.game.getTexture(lightPngPath); this.bgTexture = Chao.game.getTexture(bgPngPath); setNumStars(max); } public void setNumStars (float numStars) { this.numStars = numStars; bgTiledActor = new CHTiledActor(this.bgTexture, bgTexture.getWidth() * numStars, bgTexture.getHeight()); setSize(bgTexture.getWidth() * numStars, bgTexture.getHeight()); } @Override public void draw (Batch batch, float parentAlpha) { Color color = getColor(); batch.setColor(color.r, color.g, color.b, color.a); bgTiledActor.setPosition(getX(), getY()); bgTiledActor.draw(batch, parentAlpha); float tmpx = getX(); float tmpy = getY(); // 进度 if (rating > 0) { // 有几个完整star int fullx = (int)rating; for (int i = 0; i < fullx; i++) { batch.draw(lightTexture, tmpx, tmpy); tmpx += lightTexture.getWidth(); } float remainX = rating - fullx; if (remainX > 0) { float remainW = remainX * lightTexture.getWidth(); float u = 0, v = 1; float u2 = remainX, v2 = 0; batch.draw(lightTexture, getX() + fullx * lightTexture.getWidth(), getY(), remainW, lightTexture.getHeight(), u, v, u2, v2); } } } public float getMax () { return numStars; } public float getRating () { return rating; } public void setRating (float rating) { if (rating > numStars) rating = numStars; this.rating = rating; } }
基于Libgdx开发的开源游戏框架CHGame:
http://git.oschina.net/oahcfly/CHGameFrame
相关文章推荐
- ASIHTTPRequest源码简单分析
- iOS开发——Autolayout的Top Layout guide问题
- Your build settings specify a provisioning profile with the UUID, no provisioning profile was
- UIButton系统方法详解
- 去除UITableView中多余的分割线或者隐藏cell间的分割线
- UIActivityIndicatorView,一个名字长到令人发指的控件
- iOS UITableView 便捷编写
- Builder模式在Java中的应用
- UIProgressView
- [LeetCode]Distinct Subsequences
- 建造者模式【Builder Pattern
- UIViewController和UIView的关系
- UISegmentedControl详解
- UITextView富文本
- iOS UI-AlertView(警示框)和ActionSheet(选择框、操作表单)
- [ios]纯代码实现UITableViewCell的自定义扩展
- EasyUI form ajax submit到MVC后,在IE下提示下载内容的解决办法
- iOS UI-三种简单的动画设置
- iOS UI-IOS开发中Xcode的一些使用技巧
- UITableViewCell自适应高度