1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package androidx.appcompat.widget;
18 
19 import android.content.Context;
20 import android.graphics.Bitmap;
21 import android.util.AttributeSet;
22 import android.view.View;
23 import android.widget.RatingBar;
24 
25 import androidx.appcompat.R;
26 
27 import org.jspecify.annotations.NonNull;
28 import org.jspecify.annotations.Nullable;
29 
30 /**
31  * A {@link RatingBar} which supports compatible features on older versions of the platform.
32  *
33  * <p>This will automatically be used when you use {@link RatingBar} in your layouts
34  * and the top-level activity / dialog is provided by
35  * <a href="{@docRoot}topic/libraries/support-library/packages.html#v7-appcompat">appcompat</a>.
36  * You should only need to manually use this class when writing custom views.</p>
37  */
38 public class AppCompatRatingBar extends RatingBar {
39 
40     private final AppCompatProgressBarHelper mAppCompatProgressBarHelper;
41 
AppCompatRatingBar(@onNull Context context)42     public AppCompatRatingBar(@NonNull Context context) {
43         this(context, null);
44     }
45 
AppCompatRatingBar(@onNull Context context, @Nullable AttributeSet attrs)46     public AppCompatRatingBar(@NonNull Context context, @Nullable AttributeSet attrs) {
47         this(context, attrs, R.attr.ratingBarStyle);
48     }
49 
AppCompatRatingBar( @onNull Context context, @Nullable AttributeSet attrs, int defStyleAttr)50     public AppCompatRatingBar(
51             @NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
52         super(context, attrs, defStyleAttr);
53 
54         ThemeUtils.checkAppCompatTheme(this, getContext());
55 
56         mAppCompatProgressBarHelper = new AppCompatProgressBarHelper(this);
57         mAppCompatProgressBarHelper.loadFromAttributes(attrs, defStyleAttr);
58     }
59 
60     @Override
onMeasure(int widthMeasureSpec, int heightMeasureSpec)61     protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
62         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
63 
64         Bitmap sampleTile = mAppCompatProgressBarHelper.getSampleTile();
65         if (sampleTile != null) {
66             final int width = sampleTile.getWidth() * getNumStars();
67             setMeasuredDimension(View.resolveSizeAndState(width, widthMeasureSpec, 0),
68                     getMeasuredHeight());
69         }
70     }
71 
72 }
73