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 17 package com.example.android.renderscriptintrinsic; 18 19 import android.content.Context; 20 import android.graphics.Bitmap; 21 import android.graphics.Color; 22 import android.graphics.Paint; 23 import android.graphics.drawable.BitmapDrawable; 24 import android.graphics.drawable.Drawable; 25 import android.graphics.drawable.LayerDrawable; 26 import android.graphics.drawable.ShapeDrawable; 27 import android.graphics.drawable.StateListDrawable; 28 import android.graphics.drawable.shapes.RectShape; 29 import android.os.Build; 30 import android.util.AttributeSet; 31 import android.view.Gravity; 32 import android.widget.RadioButton; 33 34 /** 35 * A button with Thumbnail which extends Radio Button. 36 * 37 * <p>The widget override a background drawable of Radio Button with a StateList Drawable. Each 38 * state has a LayerDrawable with a Thumbnail image and a Focus rectangle. It's using original 39 * Radio Buttons text as a label, because LayerDrawable showed some issues with 40 * Canvas.drawText().</p> 41 */ 42 public class ThumbnailRadioButton extends RadioButton { 43 public ThumbnailRadioButton(Context context) { 44 super(context); 45 init(); 46 } 47 48 public ThumbnailRadioButton(Context context, AttributeSet attrs) { 49 super(context, attrs); 50 init(); 51 } 52 53 public ThumbnailRadioButton(Context context, AttributeSet attrs, int defStyle) { 54 super(context, attrs, defStyle); 55 init(); 56 } 57 58 private void init() { 59 setButtonDrawable(android.R.color.transparent); 60 } 61 62 public void setThumbnail(Bitmap bitmap) { 63 // Bitmap drawable 64 BitmapDrawable bmp = new BitmapDrawable(getResources(), bitmap); 65 bmp.setGravity(Gravity.CENTER); 66 67 int strokeWidth = 24; 68 // Checked state 69 ShapeDrawable rectChecked = new ShapeDrawable(new RectShape()); 70 rectChecked.getPaint().setColor(0xFFFFFFFF); 71 rectChecked.getPaint().setStyle(Paint.Style.STROKE); 72 rectChecked.getPaint().setStrokeWidth(strokeWidth); 73 rectChecked.setIntrinsicWidth(bitmap.getWidth() + strokeWidth); 74 rectChecked.setIntrinsicHeight(bitmap.getHeight() + strokeWidth); 75 Drawable drawableArray[] = new Drawable[]{bmp, rectChecked}; 76 LayerDrawable layerChecked = new LayerDrawable(drawableArray); 77 78 // Unchecked state 79 ShapeDrawable rectUnchecked = new ShapeDrawable(new RectShape()); 80 rectUnchecked.getPaint().setColor(0x0); 81 rectUnchecked.getPaint().setStyle(Paint.Style.STROKE); 82 rectUnchecked.getPaint().setStrokeWidth(strokeWidth); 83 rectUnchecked.setIntrinsicWidth(bitmap.getWidth() + strokeWidth); 84 rectUnchecked.setIntrinsicHeight(bitmap.getHeight() + strokeWidth); 85 Drawable drawableArray2[] = new Drawable[]{bmp, rectUnchecked}; 86 LayerDrawable layerUnchecked = new LayerDrawable(drawableArray2); 87 88 // StateList drawable 89 StateListDrawable states = new StateListDrawable(); 90 states.addState(new int[]{android.R.attr.state_checked}, 91 layerChecked); 92 states.addState(new int[]{}, 93 layerUnchecked); 94 95 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { 96 setBackground(states); 97 } else { 98 //noinspection deprecation 99 setBackgroundDrawable(states); 100 } 101 102 //Offset text to center/bottom of the checkbox 103 Paint paint = new Paint(); 104 paint.setAntiAlias(true); 105 paint.setTextSize(getTextSize()); 106 paint.setTypeface(getTypeface()); 107 float w = paint.measureText(getText(), 0, getText().length()); 108 setPadding(getPaddingLeft() + (int) ((bitmap.getWidth() - w) / 2.f + .5f), 109 getPaddingTop() + (int) (bitmap.getHeight() * 0.70), 110 getPaddingRight(), 111 getPaddingBottom()); 112 113 setShadowLayer(5, 0, 0, Color.BLACK); 114 } 115 } 116