gpt4 book ai didi

java - 如何通过反射从 declare styleable TypedArray 获取值?

转载 作者:行者123 更新时间:2023-11-30 01:39:16 26 4
gpt4 key购买 nike

我正在使用形状 ImageView (https://github.com/siyamed/android-shape-imageview),当我想从形状 ImageView 定义的 declare-styleable 中获取值时:

<declare-styleable name="ShaderImageView">
<attr name="siSquare" format="boolean" />
<attr name="siBorderColor" format="color" />
<attr name="siBorderWidth" format="dimension" />
<attr name="siBorderAlpha" format="float" />
<attr name="siForeground" format="integer|reference" />
<!-- Rounded Image View-->
<attr name="siRadius" format="dimension" />
<!-- BubbleImageView-->
<attr name="siArrowPosition" />
<attr name="siTriangleHeight" format="dimension" />
<!-- PorterImageView-->
<attr name="siShape" format="integer|reference" />
<!-- ShaderImageView-->
<attr name="siBorderType" />
<attr name="siStrokeCap" />
<attr name="siStrokeJoin" />
<attr name="siStrokeMiter" format="dimension" />
</declare-styleable>
<attr name="siArrowPosition">
<enum name="left" value="0" />
<enum name="right" value="1" />
</attr>
<attr name="siBorderType">
<enum name="stroke" value="0" />
<enum name="fill" value="1" />
</attr>
<attr name="siStrokeCap">
<enum name="butt" value="0" />
<enum name="round" value="1" />
<enum name="square" value="2" />
</attr>
<attr name="siStrokeJoin">
<enum name="bevel" value="0" />
<enum name="miter" value="1" />
<enum name="round" value="2" />
</attr>

我有问题。我的形状 ImageView 的 xml 用法:

<com.demo.example.BubbleImageView
android:id="@+id/picture_iv"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:siArrowPosition="right"
android:layout_gravity="center"
app:siBorderWidth="@dimen/default_border_width"
app:siRadius="5dp"
android:src="@drawable/fetch_failed"/>

这是我的代码:

public void init(Context context, AttributeSet attrs, int defStyle) {
if(attrs != null){
int[] declareStyleableArray = IdHelper.getResourceDeclareStyleableIntArray(context, "ShaderImageView");
if (declareStyleableArray != null && declareStyleableArray.length > 0) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, declareStyleableArray, defStyle, 0);
square = typedArray.getBoolean(IdHelper.getAttr(context, "ShaderImageView_siSquare"), square);
borderColor = typedArray.getColor(IdHelper.getAttr(context, "ShaderImageView_siBorderColor"), borderColor);
borderWidth = typedArray.getDimensionPixelSize(IdHelper.getAttr(context, "ShaderImageView_siBorderWidth"), borderWidth);
borderAlpha = typedArray.getFloat(IdHelper.getAttr(context, "ShaderImageView_siBorderAlpha"), borderAlpha);
typedArray.recycle();
}

}

IdHelper中定义的方法:

public static int[] getResourceDeclareStyleableIntArray(Context context, String name) {
try {
//use reflection to access the resource class
Field[] fields2 = Class.forName(context.getPackageName() + ".R$styleable").getFields();

//browse all fields
for (Field f : fields2) {
//pick matching field
if (f.getName().equals(name)) {
//return as int array
return (int[]) f.get(null);
}
}
} catch (Throwable t) {
t.printStackTrace();
}

return null;
}
public static int getAttr(Context context, String attrName) {
return context.getResources().getIdentifier(attrName, "attr",
context.getApplicationContext().getPackageName());
}

当我运行我的应用程序时,我得到了日志:

01-11 17:04:07.244 3744-3744/? W/System.err: Caused by: java.lang.reflect.InvocationTargetException 01-11 17:04:07.247 3744-3744/? W/System.err: at java.lang.reflect.Constructor.constructNative(Native Method) 01-11 17:04:07.247 3744-3744/? W/System.err: at java.lang.reflect.Constructor.newInstance(Constructor.java:423) 01-11 17:04:07.247 3744-3744/? W/System.err: at android.view.LayoutInflater.createView(LayoutInflater.java:594) 01-11 17:04:07.247 3744-3744/? W/System.err: ... 57 more 01-11 17:04:07.247 3744-3744/? W/System.err: Caused by: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x10 01-11 17:04:07.252 3744-3744/? W/System.err: at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:464) 01-11 17:04:07.252 3744-3744/? W/System.err: at cn.jmessage.android.uikit.chatting.shader.ShaderHelper.init(ShaderHelper.java:80) 01-11 17:04:07.252 3744-3744/? W/System.err: at cn.jmessage.android.uikit.chatting.shader.BubbleShader.init(BubbleShader.java:45) 01-11 17:04:07.253 3744-3744/? W/System.err: at cn.jmessage.android.uikit.chatting.ShaderImageView.setup(ShaderImageView.java:45) 01-11 17:04:07.253 3744-3744/? W/System.err: at cn.jmessage.android.uikit.chatting.ShaderImageView.(ShaderImageView.java:36) 01-11 17:04:07.253 3744-3744/? W/System.err: at cn.jmessage.android.uikit.chatting.BubbleImageView.(BubbleImageView.java:27) 01-11 17:04:07.253 3744-3744/? W/System.err: ... 60 more

我该如何解决这个问题?

最佳答案

我已经解决了这个问题。您在 attrs 文件中定义的 declare-styleable 属性,解析后,将在 R 文件中生成一个 int 数组。在我的例子中,数组是这样的:

       @see #ShaderImageView_siArrowPosition
@see #ShaderImageView_siBorderAlpha
@see #ShaderImageView_siBorderColor
@see #ShaderImageView_siBorderType
@see #ShaderImageView_siBorderWidth
@see #ShaderImageView_siForeground
@see #ShaderImageView_siRadius
@see #ShaderImageView_siShape
@see #ShaderImageView_siSquare
@see #ShaderImageView_siStrokeCap
@see #ShaderImageView_siStrokeJoin
@see #ShaderImageView_siStrokeMiter
@see #ShaderImageView_siTriangleHeight
*/
public static final int[] ShaderImageView = {
0x7f010001, 0x7f010002, 0x7f010003, 0x7f010004,
0x7f01000c, 0x7f01000d, 0x7f01000e, 0x7f01000f,
0x7f010010, 0x7f010011, 0x7f010012, 0x7f010013,
0x7f010014
};

注意这个数组的顺序,是按照字母升序排列的,所以我可以这样获取属性值:

public void init(Context context, AttributeSet attrs, int defStyle) {
if(attrs != null){
int[] declareStyleableArray = IdHelper.getResourceDeclareStyleableIntArray(context, "ShaderImageView");
if (declareStyleableArray != null && declareStyleableArray.length > 0) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, declareStyleableArray, defStyle, 0);
square = typedArray.getBoolean(8, square);
borderColor = typedArray.getColor(2, borderColor);
borderWidth = typedArray.getDimensionPixelSize(4, borderWidth);
borderAlpha = typedArray.getFloat(1, borderAlpha);
typedArray.recycle();
}

}

注意这一行:square = typedArray.getBoolean(8, square);第一个参数是8,这是R文件中生成的数组ShaderImageView中属性“ShaderImageView_siSquare”的顺序。这意味着如果您在 attrs 文件中定义自定义 declare-styleable 集,您定义的顺序可能与解析后在 R 文件中生成的顺序不同,如果您使用 R 获取属性值,例如:

square = typedArray.getBoolean(R.styleable.ShaderImageView_siSquare, square);

那么顺序就不用管了。希望我的案例对某人有所帮助。

关于java - 如何通过反射从 declare styleable TypedArray 获取值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34718347/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com