RenderScriptIntrinsic / src / com.example.android.renderscriptintrinsic /

ThumbnailRadioButton.java

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