1a9585dae398a69dd67797e7ca86f44ffcabd9e86Chris Banes/*
2a9585dae398a69dd67797e7ca86f44ffcabd9e86Chris Banes * Copyright (C) 2014 The Android Open Source Project
3a9585dae398a69dd67797e7ca86f44ffcabd9e86Chris Banes *
4a9585dae398a69dd67797e7ca86f44ffcabd9e86Chris Banes * Licensed under the Apache License, Version 2.0 (the "License");
5a9585dae398a69dd67797e7ca86f44ffcabd9e86Chris Banes * you may not use this file except in compliance with the License.
6a9585dae398a69dd67797e7ca86f44ffcabd9e86Chris Banes * You may obtain a copy of the License at
7a9585dae398a69dd67797e7ca86f44ffcabd9e86Chris Banes *
8a9585dae398a69dd67797e7ca86f44ffcabd9e86Chris Banes *      http://www.apache.org/licenses/LICENSE-2.0
9a9585dae398a69dd67797e7ca86f44ffcabd9e86Chris Banes *
10a9585dae398a69dd67797e7ca86f44ffcabd9e86Chris Banes * Unless required by applicable law or agreed to in writing, software
11a9585dae398a69dd67797e7ca86f44ffcabd9e86Chris Banes * distributed under the License is distributed on an "AS IS" BASIS,
12a9585dae398a69dd67797e7ca86f44ffcabd9e86Chris Banes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a9585dae398a69dd67797e7ca86f44ffcabd9e86Chris Banes * See the License for the specific language governing permissions and
14a9585dae398a69dd67797e7ca86f44ffcabd9e86Chris Banes * limitations under the License.
15a9585dae398a69dd67797e7ca86f44ffcabd9e86Chris Banes */
16a9585dae398a69dd67797e7ca86f44ffcabd9e86Chris Banes
17ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikaspackage androidx.appcompat.widget;
18a9585dae398a69dd67797e7ca86f44ffcabd9e86Chris Banes
19a9585dae398a69dd67797e7ca86f44ffcabd9e86Chris Banesimport android.content.Context;
20a9585dae398a69dd67797e7ca86f44ffcabd9e86Chris Banesimport android.graphics.Bitmap;
21a9585dae398a69dd67797e7ca86f44ffcabd9e86Chris Banesimport android.util.AttributeSet;
22fa0f82f629bf95681c14ed559922f77a3030aa18Aurimas Liutikasimport android.view.View;
23a9585dae398a69dd67797e7ca86f44ffcabd9e86Chris Banesimport android.widget.RatingBar;
24a9585dae398a69dd67797e7ca86f44ffcabd9e86Chris Banes
253de8a4e8305507475d7890205184946a25cf45e7Aurimas Liutikasimport androidx.appcompat.R;
263de8a4e8305507475d7890205184946a25cf45e7Aurimas Liutikas
27a9585dae398a69dd67797e7ca86f44ffcabd9e86Chris Banes/**
28de8d2ecb44867a66d749dd49cea36eb2a425924cKirill Grouchnikov * A {@link RatingBar} which supports compatible features on older versions of the platform.
29091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes *
30de8d2ecb44867a66d749dd49cea36eb2a425924cKirill Grouchnikov * <p>This will automatically be used when you use {@link RatingBar} in your layouts
31de8d2ecb44867a66d749dd49cea36eb2a425924cKirill Grouchnikov * and the top-level activity / dialog is provided by
32de8d2ecb44867a66d749dd49cea36eb2a425924cKirill Grouchnikov * <a href="{@docRoot}topic/libraries/support-library/packages.html#v7-appcompat">appcompat</a>.
33091b0f935e68ce9bfecc2422e60eada33fa3b09cChris Banes * You should only need to manually use this class when writing custom views.</p>
34a9585dae398a69dd67797e7ca86f44ffcabd9e86Chris Banes */
352cccf609662389d6a23dbc0711d5fb2e826e8c63Chris Banespublic class AppCompatRatingBar extends RatingBar {
36a9585dae398a69dd67797e7ca86f44ffcabd9e86Chris Banes
3729def828b59307ae6bfec1e5da8c44b7e651ddcfKirill Grouchnikov    private final AppCompatProgressBarHelper mAppCompatProgressBarHelper;
38a9585dae398a69dd67797e7ca86f44ffcabd9e86Chris Banes
392cccf609662389d6a23dbc0711d5fb2e826e8c63Chris Banes    public AppCompatRatingBar(Context context) {
40a9585dae398a69dd67797e7ca86f44ffcabd9e86Chris Banes        this(context, null);
41a9585dae398a69dd67797e7ca86f44ffcabd9e86Chris Banes    }
42a9585dae398a69dd67797e7ca86f44ffcabd9e86Chris Banes
432cccf609662389d6a23dbc0711d5fb2e826e8c63Chris Banes    public AppCompatRatingBar(Context context, AttributeSet attrs) {
442cccf609662389d6a23dbc0711d5fb2e826e8c63Chris Banes        this(context, attrs, R.attr.ratingBarStyle);
45a9585dae398a69dd67797e7ca86f44ffcabd9e86Chris Banes    }
46a9585dae398a69dd67797e7ca86f44ffcabd9e86Chris Banes
472cccf609662389d6a23dbc0711d5fb2e826e8c63Chris Banes    public AppCompatRatingBar(Context context, AttributeSet attrs, int defStyleAttr) {
48a9585dae398a69dd67797e7ca86f44ffcabd9e86Chris Banes        super(context, attrs, defStyleAttr);
49a9585dae398a69dd67797e7ca86f44ffcabd9e86Chris Banes
504c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes        mAppCompatProgressBarHelper = new AppCompatProgressBarHelper(this);
5110e2dbc1ad1c01d2824d921a8b0f070859d6f146Chris Banes        mAppCompatProgressBarHelper.loadFromAttributes(attrs, defStyleAttr);
52a9585dae398a69dd67797e7ca86f44ffcabd9e86Chris Banes    }
53a9585dae398a69dd67797e7ca86f44ffcabd9e86Chris Banes
54a9585dae398a69dd67797e7ca86f44ffcabd9e86Chris Banes    @Override
55a9585dae398a69dd67797e7ca86f44ffcabd9e86Chris Banes    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
56a9585dae398a69dd67797e7ca86f44ffcabd9e86Chris Banes        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
57a9585dae398a69dd67797e7ca86f44ffcabd9e86Chris Banes
5810e2dbc1ad1c01d2824d921a8b0f070859d6f146Chris Banes        Bitmap sampleTile = mAppCompatProgressBarHelper.getSampleTime();
5910e2dbc1ad1c01d2824d921a8b0f070859d6f146Chris Banes        if (sampleTile != null) {
6010e2dbc1ad1c01d2824d921a8b0f070859d6f146Chris Banes            final int width = sampleTile.getWidth() * getNumStars();
61fa0f82f629bf95681c14ed559922f77a3030aa18Aurimas Liutikas            setMeasuredDimension(View.resolveSizeAndState(width, widthMeasureSpec, 0),
62a9585dae398a69dd67797e7ca86f44ffcabd9e86Chris Banes                    getMeasuredHeight());
63a9585dae398a69dd67797e7ca86f44ffcabd9e86Chris Banes        }
64a9585dae398a69dd67797e7ca86f44ffcabd9e86Chris Banes    }
65a9585dae398a69dd67797e7ca86f44ffcabd9e86Chris Banes
66a9585dae398a69dd67797e7ca86f44ffcabd9e86Chris Banes}
67