通过绘制的图像在Android中,以TypedArray [英] Pass drawable image to TypedArray in Android

查看:112
本文介绍了通过绘制的图像在Android中,以TypedArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在这里实现segmentated单选按钮: https://github.com/makeramen/ Android的segmentedradiobutton 但我需要以编程方式,而不是设置在图像中的XML。

I am trying to implement a segmentated radio button from here: https://github.com/makeramen/android-segmentedradiobutton but I need to set the image programmatically and not in XML.

这是自定义单选来源:

public class CenteredImageButton extends RadioButton {

    Drawable image;

    public CenteredImageButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.CompoundButton, 0, 0);
        image = a.getDrawable(1);
        setButtonDrawable(android.R.id.empty);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (image != null) {
            image.setState(getDrawableState());

            // scale image to fit inside button

            int imgHeight = image.getIntrinsicHeight();
            Log.d("IMAGEHEIGHT", "imageWidth is " + imgHeight);

            int imgWidth = image.getIntrinsicWidth();
            Log.d("IMAGEWIDTH", "imageWidth is " + imgWidth);

            int btnWidth = getWidth();
            Log.d("BUTTONWIDTH", "buttonWidth is " + btnWidth);
            int btnHeight = getHeight();
            Log.d("BUTTONHEIGHT", "buttonHeight is " + btnHeight);

            float scale;

            if (imgWidth <= btnWidth && imgHeight <= btnHeight) {
                scale = 1.0f;
            } else {
                scale = Math.min((float) btnWidth / (float) imgWidth,
                        (float) btnHeight / (float) imgHeight);
            }

            Log.d("SCALE", "scale is " + scale);

            int dx = (int) ((btnWidth - imgWidth * scale) * 0.5f + 0.5f);
            Log.d("DX", "dx is " + dx);
            int dy = (int) ((btnHeight - imgHeight * scale) * 0.5f + 0.5f);
            Log.d("DY", "dy is " + dy);

            image.setBounds(dx, dy, (int) (dx + imgWidth * scale),
                    (int) (dy + imgHeight * scale));

            image.draw(canvas);
        }
    }

我设置可绘制在这样另一个文件:

I am setting the drawable in another file like this:

private void setButtonImageProperties(RadioButton button,Drawable drawable){
    button.setGravity(Gravity.CENTER);
    Resources resources = this.context.getResources();
    float dipValue = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
            60, resources.getDisplayMetrics());
    float dipValue1 = TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP, 80, resources.getDisplayMetrics());

    button.setMinHeight((int) dipValue);
    button.setMinWidth((int) dipValue1);

    button.setButtonDrawable(drawable);
}

请人,指教。我真的需要援助之手。谢谢你。

Please anyone, advise. I really need helping hand. Thanks.

推荐答案

您pretty的太多需要一个setImage方法添加到CenteredImageButton:

You pretty much need to add a setImage method to CenteredImageButton:

public void setImage(Drawable newImage) {
    image = newImage;
}

和后来才调用它在你的主code:

And just call it later in your main code:

button.setImage(drawable);

请参阅此要点,看看该方法内联: https://gist.github.com/1470789

See this Gist to see the method inline: https://gist.github.com/1470789

我也注意到你CenteredRadioImageButton改变了我的类的名称CenteredImageButton。如果你不实际使用此为单选按钮类似的行为,我建议使用一个标准的的ImageButton

I also noticed you changed the name of my class from CenteredRadioImageButton to CenteredImageButton. If you're not actually using this for the RadioButton-like behavior, I would suggest using a standard ImageButton

(我 SegmentedRadioButton 的维护者)

这篇关于通过绘制的图像在Android中,以TypedArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆