图像不再在带有Android 7的CustomRadioImageButton上绘制



我已经使用这个分段控制器库很长时间了,但在Android 7下,图像不再绘制在单选按钮上。。。有人知道这个版本的安卓系统发生了什么变化,导致它停止工作吗?

分割的控制器会绘制diobutton,但该按钮上的图像将不再绘制。

使用中的库:

https://github.com/vinc3m1/android-segmentedradiobutton

package com.makeramen.segmented;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.RadioButton;
public class CenteredRadioImageButton extends RadioButton {
Drawable image;
public CenteredRadioImageButton(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, com.makeramen.segmented.R.styleable.CompoundButton, 0, 0);
image = a.getDrawable(1);
setButtonDrawable(android.R.color.transparent);
a.recycle();
}
@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();
int imgWidth = image.getIntrinsicWidth();
int btnWidth = getWidth();
int btnHeight = getHeight();
float scale;
if (imgWidth <= btnWidth && imgHeight <= btnHeight) {
scale = 1.0f;
} else {
scale = Math.min((float) btnWidth / (float) imgWidth,
(float) btnHeight / (float) imgHeight);
}

int dx = (int) ((btnWidth - imgWidth * scale) * 0.5f + 0.5f);
int dy = (int) ((btnHeight - imgHeight * scale) * 0.5f + 0.5f);
image.setBounds(dx, dy, (int)(dx + imgWidth * scale), (int)(dy + imgHeight * scale));
image.draw(canvas);
}
}
}

实际上,IMAGE值在Android 7运行时始终为NULL。

<com.makeramen.segmented.CenteredRadioImageButton
android:id="@+id/myButton"
android:layout_width="40dip"
android:layout_height="50dip"
android:button="@drawable/myImage"
android:clickable="true"
android:minWidth="40dip"
android:minHeight="33dip"
android:focusable="true">
</com.makeramen.segmented.CenteredRadioImageButton>

发现,7中发生了一些变化…TypedArray中的索引从1变为0…修改解决了问题。

相关内容

  • 没有找到相关文章

最新更新