RxRotateBarBasic.java 8.69 KB
Newer Older
konghaorui committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
package com.yidianling.common.view;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.text.TextPaint;

import java.util.ArrayList;

/**
 * Created by DeanGuo on 12/24/15.
 */
public class RxRotateBarBasic {

    private static final int ITEM_OFFSET = 1;

    private static final int SHADOW_ALPHA = (int) (255 * 0.2); // 20%

    private static final int UN_RATING_ALPHA = (int) (255 * 0.3); // 30%

    private static final int RATING_ALPHA = (int) (255 * 1.0); // 100%

    private static final int OUTLINE_ALPHA = (int) (255 * 0.4); // 40%

    private boolean isSingle = false;

    private boolean isShowTitle = true;

    private int outlineWidth, ratingBarWidth, shadowWidth, textWidth;

    private float mStartAngle;

    private float mSweepAngle;

    private int maxRate = 10;

    private int mCurRate;

    private ArrayList<Rate> rates;

    private Paint ratedPaint, unRatedPaint, shadowPaint, outlinePaint;
    private TextPaint titlePaint;

    private int mRadius;

    private String mTitle;

    private int mCenterX, mCenterY;

    private RectF outlineOval, ratingOval, shadowOval;

    public RxRotateBarBasic(int curRate, String title) {
        this.mCurRate = curRate;
        this.mTitle = title;

        outlinePaint = new Paint();
        ratedPaint = new Paint();
        unRatedPaint = new Paint();
        shadowPaint = new Paint();
        titlePaint = new TextPaint();
        // set default color
        setRatingBarColor(Color.WHITE);
    }

    protected void init() {
        // in this order to init
        initRatingBar();
        initOval();
        initPaint();
    }

    private void initOval() {
        mRadius = mCenterX > mCenterY ? mCenterY : mCenterX;

        // text bar : 1/10 of radius
        textWidth = mRadius / 10;
        // rating bar : 1/10 of radius
        ratingBarWidth = mRadius / 10;
        // shadow : 1/3 of rating bar
        shadowWidth = ratingBarWidth / 3;
        // outline : 1/3 of shadow
        outlineWidth = shadowWidth / 3;

        int outlineRadius = mRadius - (textWidth / 2); // outline include text and outline, radius is base on textWidth

        int paddingRadius = outlineRadius - (textWidth / 2); // padding space draw nothing, radius is base on textWidth

        int ratingBarRadius = paddingRadius - (textWidth / 2) - (ratingBarWidth / 2);

        int shadowRadius = ratingBarRadius - (ratingBarWidth / 2) - (shadowWidth / 2);

        outlineOval = new RectF();
        ratingOval = new RectF();
        shadowOval = new RectF();

        outlineOval.left = mCenterX - outlineRadius;
        outlineOval.top = mCenterY - outlineRadius;
        outlineOval.right = mCenterX + outlineRadius;
        outlineOval.bottom = mCenterY + outlineRadius;

        ratingOval.left = mCenterX - ratingBarRadius;
        ratingOval.top = mCenterY - ratingBarRadius;
        ratingOval.right = mCenterX + ratingBarRadius;
        ratingOval.bottom = mCenterY + ratingBarRadius;

        shadowOval.left = mCenterX - shadowRadius;
        shadowOval.top = mCenterY - shadowRadius;
        shadowOval.right = mCenterX + shadowRadius;
        shadowOval.bottom = mCenterY + shadowRadius;
    }

    private void initPaint() {

        outlinePaint.setAntiAlias(true);
        outlinePaint.setStyle(Paint.Style.STROKE);
        outlinePaint.setStrokeWidth(outlineWidth);
        outlinePaint.setAlpha(OUTLINE_ALPHA);

        ratedPaint.setAntiAlias(true);
        ratedPaint.setStyle(Paint.Style.STROKE);
        ratedPaint.setStrokeWidth(ratingBarWidth);
        ratedPaint.setAlpha(RATING_ALPHA);

        unRatedPaint.setAntiAlias(true);
        unRatedPaint.setStyle(Paint.Style.STROKE);
        unRatedPaint.setStrokeWidth(ratingBarWidth);
        unRatedPaint.setAlpha(UN_RATING_ALPHA);

        shadowPaint.setAntiAlias(true);
        shadowPaint.setStyle(Paint.Style.STROKE);
        shadowPaint.setStrokeWidth(shadowWidth);
        shadowPaint.setAlpha(SHADOW_ALPHA);

        titlePaint.setAntiAlias(true);
        titlePaint.setTextSize(textWidth);
        titlePaint.setAlpha(RATING_ALPHA);
    }

    private void initRatingBar() {
        rates = new ArrayList<>();

        float itemSweepAngle;
        if (isSingle) {
            itemSweepAngle = (mSweepAngle - (ITEM_OFFSET * (maxRate))) / maxRate;
        } else {
            itemSweepAngle = (mSweepAngle - (ITEM_OFFSET * (maxRate - 1))) / maxRate;
        }

        for (int i = 0; i < maxRate; i++) {
            float itemStartAngle = mStartAngle + i * (itemSweepAngle + ITEM_OFFSET);
            rates.add(new Rate(itemStartAngle, itemSweepAngle));
        }
    }

    protected void drawUnRate(Canvas canvas) {
        for (Rate arc : rates) {
            arc.drawArc(canvas, ratingOval, unRatedPaint);
        }
    }

    protected void drawRate(Canvas canvas, int index) {
        if (index >= maxRate) {
            return;
        }
        Rate arc = rates.get(index);
        arc.drawArc(canvas, ratingOval, ratedPaint);
    }

    protected void drawShadow(Canvas canvas) {
        for (Rate arc : rates) {
            arc.drawArc(canvas, shadowOval, shadowPaint);
        }
    }

    protected void drawTitle(Canvas canvas, int alpha) {
        if (alpha > 0 && isShowTitle) {
            Path path = new Path();
            float circumference = (float) (Math.PI * (outlineOval.right - outlineOval.left));
            float textAngle = (360 / circumference) * titlePaint.measureText(getTitle());

            float startAngle = mStartAngle + mSweepAngle / 2 - textAngle / 2;

            if (isSingle) {
                // when single, draw 360 the path will be a circle
                path.addArc(outlineOval, startAngle - mSweepAngle / 2, mSweepAngle / 2);
            } else {
                path.addArc(outlineOval, startAngle, mSweepAngle);
            }

            titlePaint.setAlpha(alpha);
            canvas.drawTextOnPath(mTitle, path, 0, textWidth / 3, titlePaint);
        }
    }

    protected void drawOutLine(Canvas canvas) {

        float circumference = (float) (Math.PI * (outlineOval.right - outlineOval.left));
        float textAngle = (360 / circumference) * titlePaint.measureText(getTitle());

        float sweepAngle = (mSweepAngle - textAngle - 1 - 1) / 2;

        if (isShowTitle) {
            // text left
            float leftStartAngle = mStartAngle;
            canvas.drawArc(outlineOval, leftStartAngle, sweepAngle, false, outlinePaint);
            // text right
            float rightStartAngle = mStartAngle + mSweepAngle - sweepAngle;
            canvas.drawArc(outlineOval, rightStartAngle, sweepAngle, false, outlinePaint);
        } else {
            canvas.drawArc(outlineOval, mStartAngle, mSweepAngle, false, outlinePaint);
        }


    }

    public void setMaxRate(int maxRate) {
        this.maxRate = maxRate;
    }

    public int getRate() {
        return mCurRate;
    }

    public void setRate(int curRate) {
        this.mCurRate = curRate;
    }

    protected void setStartAngle(float mStartAngle) {
        this.mStartAngle = mStartAngle;
    }

    protected void setSweepAngle(float mSweepAngle) {
        this.mSweepAngle = mSweepAngle;
    }

    protected void setCenterX(int mCenterX) {
        this.mCenterX = mCenterX;
    }

    protected void setCenterY(int mCenterY) {
        this.mCenterY = mCenterY;
    }

    protected void setIsSingle(boolean isSingle) {
        this.isSingle = isSingle;
    }

    public String getTitle() {
        return mTitle;
    }

    public void setTitle(String title) {
        this.mTitle = title;
    }

    public void setRatedColor(int color) {
        ratedPaint.setColor(color);
    }

    public void setUnRatedColor(int color) {
        unRatedPaint.setColor(color);
    }

    public void setTitleColor(int color) {
        titlePaint.setColor(color);
    }

    public void setOutlineColor(int color) {
        outlinePaint.setColor(color);
    }

    public void setShadowColor(int color) {
        shadowPaint.setColor(color);
    }

    public void isShowTitle(boolean isShow) {
        isShowTitle = isShow;
    }

    public void setRatingBarColor(int color) {
        ratedPaint.setColor(color);
        unRatedPaint.setColor(color);
        titlePaint.setColor(color);
        outlinePaint.setColor(color);
        shadowPaint.setColor(color);
    }

    /**
     * Rate class
     */
    public class Rate {
        private float startAngle, sweepAngle;

        public Rate(float startAngle, float sweepAngle) {
            this.startAngle = startAngle;
            this.sweepAngle = sweepAngle;
        }

        public void drawArc(Canvas canvas, RectF oval, Paint paint) {
            canvas.drawArc(oval, startAngle, sweepAngle, false, paint);
        }
    }

}