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