1 /*
2  * Copyright 2015 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 com.example.android.supportv4.graphics;
18 
19 import android.app.Activity;
20 import android.graphics.Bitmap;
21 import android.graphics.BitmapFactory;
22 import android.os.Bundle;
23 import android.widget.CompoundButton;
24 import android.widget.ImageView;
25 import android.widget.ToggleButton;
26 
27 import androidx.core.graphics.drawable.RoundedBitmapDrawable;
28 import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory;
29 
30 import com.example.android.supportv4.R;
31 
32 /**
33  * Demonstrates use of a {@link RoundedBitmapDrawable}'s ability to become circular.
34  */
35 public class RoundedBitmapDrawableActivity extends Activity {
36 
37     private static final int IMAGE_RES = R.drawable.android_robot;
38     private RoundedBitmapDrawable mRoundedBitmapDrawable;
39 
40     @Override
onCreate(Bundle savedInstanceState)41     protected void onCreate(Bundle savedInstanceState) {
42         super.onCreate(savedInstanceState);
43         setContentView(R.layout.activity_rounded_bitmap);
44 
45         // Create a bitmap and set it circular.
46         Bitmap bitmap = BitmapFactory.decodeResource(getResources(), IMAGE_RES);
47         mRoundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), bitmap);
48 
49         // Get references to the inflated views.
50         ToggleButton toggle = findViewById(R.id.toggle_round);
51         ImageView image = findViewById(R.id.image);
52 
53         // Set up initial view state and on checked change listener.
54         image.setImageDrawable(mRoundedBitmapDrawable);
55         toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
56             @Override
57             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
58                 mRoundedBitmapDrawable.setCircular(isChecked);
59             }
60         });
61     }
62 
63 }
64