| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.widget; |
| 18 |
|
| 19 |
import android.compat.annotation.UnsupportedAppUsage; |
| 20 |
import android.content.Context; |
| 21 |
import android.content.res.TypedArray; |
| 22 |
import android.graphics.drawable.shapes.RectShape; |
| 23 |
import android.graphics.drawable.shapes.Shape; |
| 24 |
import android.util.AttributeSet; |
| 25 |
import android.view.accessibility.AccessibilityNodeInfo; |
| 26 |
import android.view.inspector.InspectableProperty; |
| 27 |
|
| 28 |
import com.android.internal.R; |
| 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 |
public class RatingBar extends AbsSeekBar { |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public interface OnRatingBarChangeListener { |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser); |
| 77 |
|
| 78 |
} |
| 79 |
|
| 80 |
private int mNumStars = 5; |
| 81 |
|
| 82 |
private int mProgressOnStartTracking; |
| 83 |
|
| 84 |
@UnsupportedAppUsage |
| 85 |
private OnRatingBarChangeListener mOnRatingBarChangeListener; |
| 86 |
|
| 87 |
public RatingBar(Context context, AttributeSet attrs, int defStyleAttr) { |
| 88 |
this(context, attrs, defStyleAttr, 0); |
| 89 |
} |
| 90 |
|
| 91 |
public RatingBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 92 |
super(context, attrs, defStyleAttr, defStyleRes); |
| 93 |
|
| 94 |
final TypedArray a = context.obtainStyledAttributes( |
| 95 |
attrs, R.styleable.RatingBar, defStyleAttr, defStyleRes); |
| 96 |
saveAttributeDataForStyleable(context, R.styleable.RatingBar, |
| 97 |
attrs, a, defStyleAttr, defStyleRes); |
| 98 |
final int numStars = a.getInt(R.styleable.RatingBar_numStars, mNumStars); |
| 99 |
setIsIndicator(a.getBoolean(R.styleable.RatingBar_isIndicator, !mIsUserSeekable)); |
| 100 |
final float rating = a.getFloat(R.styleable.RatingBar_rating, -1); |
| 101 |
final float stepSize = a.getFloat(R.styleable.RatingBar_stepSize, -1); |
| 102 |
a.recycle(); |
| 103 |
|
| 104 |
if (numStars > 0 && numStars != mNumStars) { |
| 105 |
setNumStars(numStars); |
| 106 |
} |
| 107 |
|
| 108 |
if (stepSize >= 0) { |
| 109 |
setStepSize(stepSize); |
| 110 |
} else { |
| 111 |
setStepSize(0.5f); |
| 112 |
} |
| 113 |
|
| 114 |
if (rating >= 0) { |
| 115 |
setRating(rating); |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
mTouchProgressOffset = 0.6f; |
| 121 |
} |
| 122 |
|
| 123 |
public RatingBar(Context context, AttributeSet attrs) { |
| 124 |
this(context, attrs, com.android.internal.R.attr.ratingBarStyle); |
| 125 |
} |
| 126 |
|
| 127 |
public RatingBar(Context context) { |
| 128 |
this(context, null); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
public void setOnRatingBarChangeListener(OnRatingBarChangeListener listener) { |
| 137 |
mOnRatingBarChangeListener = listener; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
public OnRatingBarChangeListener getOnRatingBarChangeListener() { |
| 145 |
return mOnRatingBarChangeListener; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
public void setIsIndicator(boolean isIndicator) { |
| 157 |
mIsUserSeekable = !isIndicator; |
| 158 |
if (isIndicator) { |
| 159 |
setFocusable(FOCUSABLE_AUTO); |
| 160 |
} else { |
| 161 |
setFocusable(FOCUSABLE); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
@InspectableProperty(name = "isIndicator") |
| 171 |
public boolean isIndicator() { |
| 172 |
return !mIsUserSeekable; |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
public void setNumStars(final int numStars) { |
| 183 |
if (numStars <= 0) { |
| 184 |
return; |
| 185 |
} |
| 186 |
|
| 187 |
mNumStars = numStars; |
| 188 |
|
| 189 |
|
| 190 |
requestLayout(); |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
@InspectableProperty |
| 198 |
public int getNumStars() { |
| 199 |
return mNumStars; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
public void setRating(float rating) { |
| 208 |
setProgress(Math.round(rating * getProgressPerStar())); |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
@InspectableProperty |
| 217 |
public float getRating() { |
| 218 |
return getProgress() / getProgressPerStar(); |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
public void setStepSize(float stepSize) { |
| 228 |
if (stepSize <= 0) { |
| 229 |
return; |
| 230 |
} |
| 231 |
|
| 232 |
final float newMax = mNumStars / stepSize; |
| 233 |
final int newProgress = (int) (newMax / getMax() * getProgress()); |
| 234 |
setMax((int) newMax); |
| 235 |
setProgress(newProgress); |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
@InspectableProperty |
| 244 |
public float getStepSize() { |
| 245 |
return (float) getNumStars() / getMax(); |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
private float getProgressPerStar() { |
| 252 |
if (mNumStars > 0) { |
| 253 |
return 1f * getMax() / mNumStars; |
| 254 |
} else { |
| 255 |
return 1; |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
@Override |
| 260 |
Shape getDrawableShape() { |
| 261 |
|
| 262 |
return new RectShape(); |
| 263 |
} |
| 264 |
|
| 265 |
@Override |
| 266 |
void onProgressRefresh(float scale, boolean fromUser, int progress) { |
| 267 |
super.onProgressRefresh(scale, fromUser, progress); |
| 268 |
|
| 269 |
|
| 270 |
updateSecondaryProgress(progress); |
| 271 |
|
| 272 |
if (!fromUser) { |
| 273 |
|
| 274 |
dispatchRatingChange(false); |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
private void updateSecondaryProgress(int progress) { |
| 286 |
final float ratio = getProgressPerStar(); |
| 287 |
if (ratio > 0) { |
| 288 |
final float progressInStars = progress / ratio; |
| 289 |
final int secondaryProgress = (int) (Math.ceil(progressInStars) * ratio); |
| 290 |
setSecondaryProgress(secondaryProgress); |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
@Override |
| 295 |
protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 296 |
super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 297 |
|
| 298 |
if (mSampleWidth > 0) { |
| 299 |
final int width = mSampleWidth * mNumStars; |
| 300 |
setMeasuredDimension(resolveSizeAndState(width, widthMeasureSpec, 0), |
| 301 |
getMeasuredHeight()); |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
@Override |
| 306 |
void onStartTrackingTouch() { |
| 307 |
mProgressOnStartTracking = getProgress(); |
| 308 |
|
| 309 |
super.onStartTrackingTouch(); |
| 310 |
} |
| 311 |
|
| 312 |
@Override |
| 313 |
void onStopTrackingTouch() { |
| 314 |
super.onStopTrackingTouch(); |
| 315 |
|
| 316 |
if (getProgress() != mProgressOnStartTracking) { |
| 317 |
dispatchRatingChange(true); |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
@Override |
| 322 |
void onKeyChange() { |
| 323 |
super.onKeyChange(); |
| 324 |
dispatchRatingChange(true); |
| 325 |
} |
| 326 |
|
| 327 |
void dispatchRatingChange(boolean fromUser) { |
| 328 |
if (mOnRatingBarChangeListener != null) { |
| 329 |
mOnRatingBarChangeListener.onRatingChanged(this, getRating(), |
| 330 |
fromUser); |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
@Override |
| 335 |
public synchronized void setMax(int max) { |
| 336 |
|
| 337 |
if (max <= 0) { |
| 338 |
return; |
| 339 |
} |
| 340 |
|
| 341 |
super.setMax(max); |
| 342 |
} |
| 343 |
|
| 344 |
@Override |
| 345 |
public CharSequence getAccessibilityClassName() { |
| 346 |
return RatingBar.class.getName(); |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
@Override |
| 351 |
public void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) { |
| 352 |
super.onInitializeAccessibilityNodeInfoInternal(info); |
| 353 |
|
| 354 |
if (canUserSetProgress()) { |
| 355 |
info.addAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_SET_PROGRESS); |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
@Override |
| 360 |
boolean canUserSetProgress() { |
| 361 |
return super.canUserSetProgress() && !isIndicator(); |
| 362 |
} |
| 363 |
} |