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 com.example.android.supportv4.R; 20 21 import android.app.Activity; 22 import android.graphics.Color; 23 import android.graphics.drawable.Drawable; 24 import android.os.Bundle; 25 import android.support.v4.content.ContextCompat; 26 import android.support.v4.graphics.drawable.DrawableCompat; 27 import android.widget.ImageView; 28 import android.widget.RadioGroup; 29 30 /** 31 * Demonstrates use of a {@link DrawableCompat}'s ability to become circular. 32 */ 33 public class DrawableCompatActivity extends Activity { 34 35 private static final int IMAGE_RES = R.drawable.ic_favorite; 36 37 private ImageView mImageView; 38 private Drawable mDrawable; 39 40 @Override 41 protected void onCreate(Bundle savedInstanceState) { 42 super.onCreate(savedInstanceState); 43 setContentView(R.layout.drawable_compat); 44 45 mImageView = findViewById(R.id.image); 46 47 Drawable d = ContextCompat.getDrawable(this, IMAGE_RES); 48 mDrawable = DrawableCompat.wrap(d.mutate()); 49 50 mImageView.setImageDrawable(mDrawable); 51 52 RadioGroup rg = findViewById(R.id.drawable_compat_options); 53 rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 54 @Override 55 public void onCheckedChanged(RadioGroup radioGroup, int id) { 56 switch (id) { 57 case R.id.drawable_compat_no_tint: 58 clearTint(); 59 break; 60 case R.id.drawable_compat_color: 61 setColorTint(); 62 break; 63 case R.id.drawable_compat_state_list: 64 setColorStateListTint(); 65 break; 66 } 67 } 68 }); 69 } 70 71 private void clearTint() { 72 DrawableCompat.setTintList(mDrawable, null); 73 } 74 75 private void setColorTint() { 76 DrawableCompat.setTint(mDrawable, Color.MAGENTA); 77 } 78 79 private void setColorStateListTint() { 80 DrawableCompat.setTintList(mDrawable, 81 ContextCompat.getColorStateList(this, R.color.tint_state_list)); 82 } 83 84 } 85