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 package com.example.android.supportv7.view;
17 
18 import android.os.Bundle;
19 import android.os.Handler;
20 import android.view.View;
21 import android.view.ViewGroup;
22 import android.widget.RadioGroup;
23 import android.widget.SeekBar;
24 import android.widget.TextView;
25 
26 import androidx.appcompat.app.AppCompatActivity;
27 import androidx.cardview.widget.CardView;
28 import androidx.core.content.ContextCompat;
29 
30 import com.example.android.supportv7.R;
31 
32 public class CardViewActivity extends AppCompatActivity {
33 
34     CardView mCardView;
35 
36     TextView mInfoText;
37 
38     SeekBar mCornerRadiusSeekBar;
39 
40     SeekBar mWidthSeekBar;
41 
42     SeekBar mHeightSeekBar;
43 
44     SeekBar mElevationSeekBar;
45 
46     SeekBar mMaxElevationSeekBar;
47 
48     SeekBar mAlphaSeekBar;
49 
50     boolean mResizeCardView = true;
51 
52     private SeekBar.OnSeekBarChangeListener mOnSeekBarChangedListener
53             = new SeekBar.OnSeekBarChangeListener() {
54         @Override
55         public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
56             update();
57         }
58 
59         @Override
60         public void onStartTrackingTouch(SeekBar seekBar) {
61 
62         }
63 
64         @Override
65         public void onStopTrackingTouch(SeekBar seekBar) {
66 
67         }
68     };
69 
update()70     private void update() {
71         mElevationSeekBar.setMax(mMaxElevationSeekBar.getProgress());
72         if (mCornerRadiusSeekBar.getProgress() != mCardView.getRadius()) {
73             mCardView.setRadius(mCornerRadiusSeekBar.getProgress());
74         }
75         if (mElevationSeekBar.getProgress() != mCardView.getCardElevation()) {
76             mCardView.setCardElevation(mElevationSeekBar.getProgress());
77         }
78         if (mMaxElevationSeekBar.getProgress() != mCardView.getMaxCardElevation()) {
79             mCardView.setMaxCardElevation(mMaxElevationSeekBar.getProgress());
80         }
81         mCardView.setAlpha(mAlphaSeekBar.getProgress() / 255f);
82         ViewGroup.LayoutParams lp;
83         if (mResizeCardView) {
84             lp = setViewBounds(mCardView);
85         } else {
86             lp = setViewBounds(mInfoText);
87         }
88         mInfoText.setText("radius: " + mCornerRadiusSeekBar.getProgress()
89                 + ", alpha: " + mAlphaSeekBar.getProgress()
90                 + "\n w: " + lp.width + "\nh: " + lp.height
91                 + "\nelevation: " + mCardView.getCardElevation() + " of "
92                 + mCardView.getMaxCardElevation());
93     }
94 
setViewBounds(View view)95     private ViewGroup.LayoutParams setViewBounds(View view) {
96         ViewGroup.LayoutParams lp = view.getLayoutParams();
97         boolean changed = lp.width != mWidthSeekBar.getProgress()
98                 || lp.height != mHeightSeekBar.getProgress();
99         if (!changed) {
100             return lp;
101         }
102         lp.width = mWidthSeekBar.getProgress();
103         lp.height = mHeightSeekBar.getProgress();
104         view.setLayoutParams(lp);
105         return lp;
106     }
107 
108     @Override
onCreate(Bundle savedInstanceState)109     protected void onCreate(Bundle savedInstanceState) {
110         super.onCreate(savedInstanceState);
111         setContentView(R.layout.activity_card_view);
112         mInfoText = findViewById(R.id.info_text);
113         mCardView = findViewById(R.id.card_view);
114         mCornerRadiusSeekBar = findViewById(R.id.corner_radius_seek_bar);
115         mCornerRadiusSeekBar.setProgress((int) mCardView.getRadius());
116         mCornerRadiusSeekBar.setOnSeekBarChangeListener(mOnSeekBarChangedListener);
117 
118         mWidthSeekBar = findViewById(R.id.width_seek_bar);
119         mWidthSeekBar.setProgress(mCardView.getLayoutParams().width);
120 
121         mWidthSeekBar.setOnSeekBarChangeListener(mOnSeekBarChangedListener);
122 
123         mHeightSeekBar = findViewById(R.id.height_seek_bar);
124         mHeightSeekBar.setProgress(mCardView.getLayoutParams().height);
125         mHeightSeekBar.setOnSeekBarChangeListener(mOnSeekBarChangedListener);
126 
127         mElevationSeekBar = findViewById(R.id.elevation_seek_bar);
128         mElevationSeekBar.setProgress((int) mCardView.getCardElevation());
129         mElevationSeekBar.setOnSeekBarChangeListener(mOnSeekBarChangedListener);
130 
131         mMaxElevationSeekBar = findViewById(R.id.max_elevation_seek_bar);
132         mMaxElevationSeekBar.setProgress((int) mCardView.getMaxCardElevation());
133         mMaxElevationSeekBar.setOnSeekBarChangeListener(mOnSeekBarChangedListener);
134 
135         mAlphaSeekBar = findViewById(R.id.alpha_seek_bar);
136         mAlphaSeekBar.setProgress((int) (mCardView.getAlpha() * 255));
137         mAlphaSeekBar.setOnSeekBarChangeListener(mOnSeekBarChangedListener);
138 
139         RadioGroup rb = findViewById(R.id.select_target_radio);
140         rb.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
141             @Override
142             public void onCheckedChanged(RadioGroup group, int checkedId) {
143                 mResizeCardView = checkedId == R.id.resize_card_view;
144                 update();
145             }
146         });
147 
148         new Handler().postDelayed(new Runnable() {
149             @Override
150             public void run() {
151                 View content = findViewById(android.R.id.content);
152                 mWidthSeekBar.setProgress(mCardView.getWidth());
153                 mHeightSeekBar.setProgress(mCardView.getHeight());
154                 mWidthSeekBar.setMax(content.getWidth());
155                 mHeightSeekBar.setMax(content.getHeight());
156                 update();
157             }
158         }, 100);
159 
160         ((RadioGroup) findViewById(R.id.select_bg_color_radio))
161                 .setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
162                     @Override
163                     public void onCheckedChanged(RadioGroup group, int checkedId) {
164                         mCardView.setCardBackgroundColor(
165                                 ContextCompat.getColorStateList(CardViewActivity.this,
166                                         getColorId(checkedId)));
167                     }
168                 });
169     }
170 
getColorId(int id)171     private int getColorId(int id) {
172         switch (id) {
173             case R.id.yellow:
174                 return R.color.card_yellow;
175             case R.id.aquatic:
176                 return R.color.card_aquatic;
177             case R.id.classic:
178                 return R.color.card_classic;
179             case R.id.sunbrite:
180                 return R.color.card_sunbrite;
181             case R.id.tropical:
182                 return R.color.card_tropical;
183             case R.id.selector:
184                 return R.color.card_selector;
185             default:
186                 return R.color.cardview_light_background;
187         }
188     }
189 }
190