服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android评分RationBar控件使用详解

Android评分RationBar控件使用详解

2022-11-11 15:53最爱爬爬虾 Android

这篇文章主要为大家详细介绍了Android评分RationBar控件的使用方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

Android评分RationBar控件,供大家参考,具体内容如下

主要是不想用太多三方的控件,所以决定尽可能自己写,最近有写一个评分的页面,废话不多说直接上图

Android评分RationBar控件使用详解

我觉得嘛 这个东西用ViewGroup包起来感觉会写很多View 于是我决定使用之定义控件 直接上代码

 

?
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
/**
 * 评论专用星星
 * <p>
 * 宽高都不能用wrap_content 必须使用固定值或者match_parent
 * <p>
 * MIXED : 在控件的宽度范围内等分星星
 * <p>
 * SCROLL:根据 星星的宽度和每个星星之间的间距画星星
 */
public class SuperRationBar extends View implements View.OnTouchListener {
 
  final public static int MIXED = 0;
 
  final public static int SCROLL = 1;
 
  //不传默认为 MIXED
  private int mode = MIXED;
  // 需要建立多少星星 不传 默认为5
  private int number = 5;
  // 单个星星的宽度 这里宽度和高度相等 必传
  private int startWidth = 50;
  // 每个星星之间的间距 默认20 (mode == MIXED 用不到)
  private int startPadding = 10;
 
 
  //是否已经初始化试图
  private boolean isInit = false;
  //被选中的个数
  private int selectNumber = 0;
  //选中的样式
  private Bitmap bmSel;
  //未选中的样式
  private Bitmap bmNol;
  //记录每个星星的位置 用 , 分割
  private List<String> pointList;
  // 画笔
  private Paint mPaint;
 
  public SuperRationBar(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context, attrs);
    init(context);
  }
 
  private void init(Context context) {
    mPaint = new Paint();
    setOnTouchListener(this);
  }
 
  private void init(Context context, AttributeSet attrs) {
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SuperRationBar);
    mode = a.getInteger(R.styleable.SuperRationBar_mode, MIXED);
    number = a.getInteger(R.styleable.SuperRationBar_SuperRationBar_number, 5);
    startWidth = (int) a.getDimension(R.styleable.SuperRationBar_SuperRationBar_startWidth, 50);
    startPadding = (int) a.getDimension(R.styleable.SuperRationBar_SuperRationBar_startPadding, 10);
    a.recycle();
  }
 
  @Override
  public void draw(Canvas canvas) {
    super.draw(canvas);
    if (!isInit) {
      return;
    }
    {//记录每个星星的位置 用 , 分割
      pointList = new ArrayList<>();
    }
    if (mode == MIXED) {
      //单个星星的宽度
      int itemWidth = getWidth() / number;
      //根据每个星星之间的间距画星星
      for (int i = 0; i < number; i++) {
        int left = i == 0 ? 0 : itemWidth * i;
        int height = getHeight();
        int bmHeight = bmSel.getHeight();
        int top = (getHeight() - startWidth) / 2;
        pointList.add(left + "," + top + "," + (left + itemWidth) + "," + (top + itemWidth));
        if (i < selectNumber) {
          canvas.drawBitmap(bmSel, left, top, mPaint);
        } else {
          canvas.drawBitmap(bmNol, left, top, mPaint);
        }
      }
    } else if (mode == SCROLL) {
      int totalWidth = (startWidth + startPadding) * (number - 1) + startWidth;
      //单个星星的宽度
      int itemWidth = totalWidth / number;
      //根据每个星星之间的间距画星星
      for (int i = 0; i < number; i++) {
        int left = i == 0 ? 0 : itemWidth * i;
        int top = (getHeight() - startWidth) / 2;
        pointList.add(left + "," + top + "," + (left + itemWidth) + "," + (top + itemWidth));
        if (i < selectNumber) {
          canvas.drawBitmap(bmSel, left, top, mPaint);
        } else {
          canvas.drawBitmap(bmNol, left, top, mPaint);
        }
      }
    }
  }
 
  @Override
  protected void onFinishInflate() {
    super.onFinishInflate();
    isInit = true;
  }
 
  /**
   * 设置三种图片样式的id
   *
   * @param selId
   * @param nolId
   */
  public SuperRationBar setImageResIds(int selId, int nolId) {
    bmSel = BitmapFactory.decodeResource(getResources(), selId);
    bmNol = BitmapFactory.decodeResource(getResources(), nolId);
    bmSel = zoomBitmap(bmSel, startWidth);
    bmNol = zoomBitmap(bmNol, startWidth);
    return this;
  }
 
  /**
   * 调用这个方法刷新页面
   */
  public void launcher() {
    if (isInit) {
      postInvalidate();
    } else {
      post(new Runnable() {
        @Override
        public void run() {
          postInvalidate();
        }
      });
    }
  }
 
  @Override
  public boolean onTouch(View v, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN
        || event.getAction() == MotionEvent.ACTION_MOVE) {
      if (pointList != null) {
        int num = contain((int) event.getX(), (int) event.getY());
        if (num != -1) {
          selectNumber = num + 1;
        }
        postInvalidate();
      }
      if (event.getAction() == MotionEvent.ACTION_DOWN) {
        return true;
      }
    }
    return false;
  }
 
  /**
   * 判断点击的位置是不是在星星上边 并返回星星的下标 错误 返回-1
   *
   * @param x
   * @param y
   * @return
   */
  private int contain(int x, int y) {
    int size = pointList.size();
    for (int i = 0; i < size; i++) {
      String[] pointArray = pointList.get(i).split(",");
      int rl = Integer.parseInt(pointArray[0]);
      int rt = Integer.parseInt(pointArray[1]);
      int rr = Integer.parseInt(pointArray[2]);
      int rb = Integer.parseInt(pointArray[3]);
      if (x > rl && x < rr) {
        //在范围内 返回下标
        return i;
      }
    }
    return -1;
  }
 
  public int getSelectNumber() {
    return selectNumber;
  }
 
 
  /**
   * 等比例缩放bitmap图片
   *
   * @param bitmap
   * @param reqWidth
   * @return
   */
  public Bitmap zoomBitmap(Bitmap bitmap, float reqWidth) {
    if (bitmap == null) {
      return null;
    }
    final int width = bitmap.getWidth();
    Matrix matrix = new Matrix();
    float scale = reqWidth / width;
    matrix.setScale(scale, scale);
    bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
        bitmap.getHeight(), matrix, true);
    return bitmap;
  }
 
}
?
1
2
3
4
5
6
7
8
9
<declare-styleable name="SuperRationBar">
    <attr name="SuperRationBar_number" format="integer" />
    <attr name="SuperRationBar_startWidth" format="dimension" />
    <attr name="SuperRationBar_startPadding" format="dimension" />
    <attr name="mode">
      <enum name="fixed" value="0" />
      <enum name="scroll" value="1" />
    </attr>
  </declare-styleable>

注释得还是挺详细的 这里直接上使用代码

?
1
2
3
4
5
6
7
8
9
10
11
12
<com.xxx.widget.SuperRationBar
    android:id="@+id/RationBar0"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_marginLeft="50dp"
    android:layout_marginTop="10dp"
    android:layout_marginRight="50dp"
    android:background="@color/colorAccent"
    app:SuperRationBar_number="6"
    app:SuperRationBar_startPadding="10dp"
    app:SuperRationBar_startWidth="40dp"
    app:mode="fixed" />
?
1
2
3
SuperRationBar_startWidth 这个为必传 而且只能在布局里面传
 RationBar0.setImageResIds(R.mipmap.img_ration_bar_sel, R.mipmap.img_ration_bar_nol)
        .launcher();

使用就这么一句 调用

?
1
int number0 = RationBar0.getSelectNumber();

可以获取到当前的评分是多少

以上代码可以复制粘贴使用 有经验的小伙伴们 改改代码就可以实现 别的功能了

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq_16592085/article/details/103603906

延伸 · 阅读

精彩推荐