简体   繁体   English

Android RatingBar大小

[英]Android RatingBar size

I want to make a rating bar that can work and display properly at any dimentions given. 我想制作一个可以在任何给定尺寸下正常显示的评分栏。

Android default RatingBar does not scale and any other solution i saw was to reduce or increase the RatingBar size with prededined drawables. Android默认的RatingBar不能缩放,我看到的任何其他解决方案都是使用预定的可绘制对象来减小或增加RatingBar的大小。

Is there any way to have a RatingBar that adapts to the dimentions givens and adapts to any dimentions? 有什么办法可以让RatingBar适应给定的尺寸并适应任何尺寸的尺寸?

Yes there is by creating a new widget that acts like a RatingBar. 是的,可以通过创建一个充当RatingBar的新小部件来实现。 在此处输入图片说明

There are some issues. 有一些问题。

  • Clipping path doesn't have AA 剪切路径没有AA
  • And the coords of the stars are not ideal 而且星星的坐标并不理想

but these issues can be fixed quite easily. 但是这些问题很容易解决。 Here is the code of the new RatingBar widget. 这是新的RatingBar小部件的代码。

You can create as many starNumber as you want and they will adapt to it's layout dimentions. 您可以根据需要创建任意数量的starNumber,它们将适应其布局尺寸。

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Path;
import android.util.AttributeSet;
import android.widget.SeekBar;

public class PRatingBar extends SeekBar {

    private int starNumber = 5;
    private float space;
    private int rating;
    private boolean firstTime = true;

    public PRatingBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        _init();
    }

    public PRatingBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        _init();
    }

    public PRatingBar(Context context) {
        super(context);
        _init();

    }

    /**
     * @return the rating
     */
    public int getRating() {
        return rating;
    }

    /**
     * Sets the number of stars
     * 
     * @param starNumber
     */
    public void setStarNumber(int starNumber) {
        this.starNumber = starNumber;
    }

    private void _init() {
        setThumb(null);
        setOnSeekBarChangeListener(sbcl);
        post(new Runnable() {

            @Override
            public void run() {
                setProgress((int) space);
            }
        });
    }

    OnSeekBarChangeListener sbcl = new OnSeekBarChangeListener() {

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            rating = (int) (getProgress() / space) + 1;
            setProgress((int) (rating * space));
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
        }
    };

    @Override
    protected synchronized void onDraw(Canvas canvas) {

        setMax(getWidth());
        canvas.clipPath(calculateStarPath());

        // TODO C'mon you can do better than that
        if (firstTime) {
            setProgress((int) space);
            firstTime = false;
        }

        super.onDraw(canvas);

    }

    private Path calculateStarPath() {
        Path path = new Path();

        space = getWidth() / starNumber;
        float centerY = (getHeight() / 2f) - (space / 2f);
        float step = space / 10;
        float startX;
        for (int i = 0; i < starNumber; i++) {
            startX = i * space;

            path.moveTo(startX + (step * 5), centerY + 0);

            path.lineTo(startX + (step * 3.3f), centerY + (step * 3.3f));

            path.lineTo(startX, centerY + (step * 4));

            path.lineTo(startX + (step * 2.3f), centerY + (step * 6.3f));

            path.lineTo(startX + +(step * 2), centerY + space);

            path.lineTo(startX + (step * 5), centerY + (step * 8.1f));

            path.lineTo(startX + +(step * 8), centerY + space);

            path.lineTo(startX + (step * 7.6f), centerY + (step * 6.3f));

            path.lineTo(startX + (step * 10), centerY + (step * 4));

            path.lineTo(startX + (step * 6.7f), centerY + (step * 3.3f));

            path.close();
        }
        return path;
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM