【Android 自定义简单控件--星级评价】知是行的主意,行是知的功夫。这篇文章主要讲述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 = 5; public ViewStar(@NonNull Context context) { this(context, null); }public ViewStar(@NonNull Context context, @Nullable AttributeSet attrs) { this(context, attrs, 0); }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, 0); 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 = 5; 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 < 0 ? 0 : (mRating > MAX_STAR ? MAX_STAR : mRating); int mLeft = 0; int mTop = 0; int full = (int) currentRating; /*整个星星的数量*/ /** * 画黄色的整颗星 */ for (int i = 0; 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 > 0) { int w = (int) (part * starWidth); Bitmap partBitmap = Bitmap.createBitmap(starYellow, 0, 0, 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 ——网络编程
- 微信浏览器安卓手机video浮在最上层问题
- app与后台的tokensessionIdRSA加密登录认证与安全解决方案
- Android自定义评分控件(RatingStarView)
- XmlException: Text node canot appear in this state
- 玖哥乱弹Android初学路上会遇到的瓶颈
- app自动化环境搭建之Appium
- Ubuntu+NDK编译openssl(为了Android上使用libcurl且支持HTTPS协议)
- android socket 通讯(客户端) 发送数据