aboutsummaryrefslogtreecommitdiff
path: root/renderScript/RenderScriptIntrinsic/Application/src/main/java/com/example/android/renderscriptintrinsic/ThumbnailRadioButton.java
blob: 0a42fbe401a43363c43fa483d474ff29516464b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.android.renderscriptintrinsic;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.StateListDrawable;
import android.graphics.drawable.shapes.RectShape;
import android.os.Build;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.RadioButton;

/**
 * A button with Thumbnail which extends Radio Button.
 *
 * <p>The widget override a background drawable of Radio Button with a StateList Drawable. Each
 * state has a LayerDrawable with a Thumbnail image and a Focus rectangle. It's using original
 * Radio Buttons text as a label, because LayerDrawable showed some issues with
 * Canvas.drawText().</p>
 */
public class ThumbnailRadioButton extends RadioButton {
    public ThumbnailRadioButton(Context context) {
        super(context);
        init();
    }

    public ThumbnailRadioButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ThumbnailRadioButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        setButtonDrawable(android.R.color.transparent);
    }

    public void setThumbnail(Bitmap bitmap) {
        // Bitmap drawable
        BitmapDrawable bmp = new BitmapDrawable(getResources(), bitmap);
        bmp.setGravity(Gravity.CENTER);

        int strokeWidth = 24;
        // Checked state
        ShapeDrawable rectChecked = new ShapeDrawable(new RectShape());
        rectChecked.getPaint().setColor(0xFFFFFFFF);
        rectChecked.getPaint().setStyle(Paint.Style.STROKE);
        rectChecked.getPaint().setStrokeWidth(strokeWidth);
        rectChecked.setIntrinsicWidth(bitmap.getWidth() + strokeWidth);
        rectChecked.setIntrinsicHeight(bitmap.getHeight() + strokeWidth);
        Drawable drawableArray[] = new Drawable[]{bmp, rectChecked};
        LayerDrawable layerChecked = new LayerDrawable(drawableArray);

        // Unchecked state
        ShapeDrawable rectUnchecked = new ShapeDrawable(new RectShape());
        rectUnchecked.getPaint().setColor(0x0);
        rectUnchecked.getPaint().setStyle(Paint.Style.STROKE);
        rectUnchecked.getPaint().setStrokeWidth(strokeWidth);
        rectUnchecked.setIntrinsicWidth(bitmap.getWidth() + strokeWidth);
        rectUnchecked.setIntrinsicHeight(bitmap.getHeight() + strokeWidth);
        Drawable drawableArray2[] = new Drawable[]{bmp, rectUnchecked};
        LayerDrawable layerUnchecked = new LayerDrawable(drawableArray2);

        // StateList drawable
        StateListDrawable states = new StateListDrawable();
        states.addState(new int[]{android.R.attr.state_checked},
                layerChecked);
        states.addState(new int[]{},
                layerUnchecked);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            setBackground(states);
        } else {
            //noinspection deprecation
            setBackgroundDrawable(states);
        }

        //Offset text to center/bottom of the checkbox
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setTextSize(getTextSize());
        paint.setTypeface(getTypeface());
        float w = paint.measureText(getText(), 0, getText().length());
        setPadding(getPaddingLeft() + (int) ((bitmap.getWidth() - w) / 2.f + .5f),
                getPaddingTop() + (int) (bitmap.getHeight() * 0.70),
                getPaddingRight(),
                getPaddingBottom());

        setShadowLayer(5, 0, 0, Color.BLACK);
    }
}