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
17package androidx.appcompat.widget;
18
19import android.content.Context;
20import android.graphics.Bitmap;
21import android.util.AttributeSet;
22import android.view.View;
23import android.widget.RatingBar;
24
25import androidx.appcompat.R;
26
27/**
28 * A {@link RatingBar} which supports compatible features on older versions of the platform.
29 *
30 * <p>This will automatically be used when you use {@link RatingBar} in your layouts
31 * and the top-level activity / dialog is provided by
32 * <a href="{@docRoot}topic/libraries/support-library/packages.html#v7-appcompat">appcompat</a>.
33 * You should only need to manually use this class when writing custom views.</p>
34 */
35public class AppCompatRatingBar extends RatingBar {
36
37    private final AppCompatProgressBarHelper mAppCompatProgressBarHelper;
38
39    public AppCompatRatingBar(Context context) {
40        this(context, null);
41    }
42
43    public AppCompatRatingBar(Context context, AttributeSet attrs) {
44        this(context, attrs, R.attr.ratingBarStyle);
45    }
46
47    public AppCompatRatingBar(Context context, AttributeSet attrs, int defStyleAttr) {
48        super(context, attrs, defStyleAttr);
49
50        mAppCompatProgressBarHelper = new AppCompatProgressBarHelper(this);
51        mAppCompatProgressBarHelper.loadFromAttributes(attrs, defStyleAttr);
52    }
53
54    @Override
55    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
56        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
57
58        Bitmap sampleTile = mAppCompatProgressBarHelper.getSampleTime();
59        if (sampleTile != null) {
60            final int width = sampleTile.getWidth() * getNumStars();
61            setMeasuredDimension(View.resolveSizeAndState(width, widthMeasureSpec, 0),
62                    getMeasuredHeight());
63        }
64    }
65
66}
67