zoukankan      html  css  js  c++  java
  • 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);
  • 相关阅读:
    基于按annotation的hibernate主键生成策略,(本文copy的 七郎's Blog的博客,觉的不错)
    sql server与oracle常用函数对比
    如何将jar包关联到javadoc文档??
    在struts2中,每次修改了struts.xml都要重启tomcat服务器,那么怎么样设置才能修改了struts.xml而不需要重启tomcat的服务器呢??
    单链表的就地逆置
    读书笔记——尽量将引用参数设置为const类型
    二进制中1的个数
    反转单向链表
    二叉树的深度
    C/C++参数入栈顺序
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/7492660.html
Copyright © 2011-2022 走看看