代码相当于android:cropToPadding

时间:2011-09-23 16:06:00

标签: android imageview

在我的Android应用程序中,我以这种方式在xml中创建缩略图:

<ImageView android:id="@+id/my_thumbnail
           android:layout_width="50dp"
           android:layout_height="50dp"
           android:background="@color/white"
           android:padding="2dp"
           android:scaleType="fitXY"
           android:cropToPadding="true" />

这会在缩略图周围创建一个白色边框 - 看起来很不错。

现在,在代码的另一个地方我需要在代码中创建缩略图,而不是XML(这是GridView的Adapter类 - 创建缩略图网格)。我可以根据需要设置所有参数,但是我找不到在代码中设置cropToPadding的方法。结果,缩略图被绘制在填充上,看起来非常难看。

如何在代码中设置此cropToPadding内容?

顺便说一句,该应用必须适用于Android 1.6。

修改

根据 userSeven7s 的建议,我将此样式添加到包含其他样式的XML中:

<style name="GridImage">
    <item name="android:background">@color/white</item>
    <item name="android:padding">2dp</item>
    <item name="android:cropToPadding">true</item>
    <item name="android:typeface">monospace</item>
</style>

(请注意,该文件包含<resources>根元素,并包含其他几个<style>元素。但所有其他元素仅在布局xml文件中引用。)

然后我在网格视图适配器中添加了这段代码:

public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {
        XmlPullParser parser = mContext.getResources().getXml(R.style.GridImage);
        AttributeSet attributes = Xml.asAttributeSet(parser);
        imageView = new ImageView(mContext, attributes);
        // ... some more initialisation
    } else {
        imageView = (ImageView) convertView;
    }

    // ... code to create the bitmap and set it into the ImageView goes here
}

这编译很好(即R.style.GridImage存在)。但是,当我运行此代码时,应用程序会在Resources.NotFoundException上与getXml(R.style.GridImage)崩溃。

有什么建议吗?

3 个答案:

答案 0 :(得分:3)

如果您尝试使用AttributeSetattributes.getAttributeCount()始终返回0的解决方案,那么剩下的唯一解决方案是使用反射进行此黑客攻击:

Field field = ImageView.class.getDeclaredField("mCropToPadding");
field.setAccessible(true);
field.set(imageView, true);

使用此功能,您可能无法确定它是否适用于其他Android固件版本。 无论如何,失踪的二传手可能只是被遗忘了,所以这证明了我的黑客攻击。

答案 1 :(得分:1)

您可以从ImageView本身获取缩略图。在每个imageview上调用getDrawingCache()以获取用于绘制的位图。

新更新:

myimage_attrs.xml文件夹中创建xml xml,并添加所需的所有imageview属性。

<?xml version=”1.0″ encoding=”utf-8″?>
<item name=”android:layout_height”>10dp</item>
<item name=”android:layout_width”>10dp</item>
<item name="android:background">@color/white</item>
<item name="android:padding">2dp</item>
<item name="android:cropToPadding">true</item>
<item name="android:typeface">monospace</item>
....

然后从xml中创建一个AttributeSet并将其传递给ImageView构造函数。

XmlPullParser parser = resources.getXml(R.xml.myimage_attrs);
AttributeSet attributes = Xml.asAttributeSet(parser);

ImageView imgv = new ImageView(context, attributes);

答案 2 :(得分:1)

为了他人的利益,这里最终做了些工作(感谢 userSeven7s 让我朝着正确的方向发展)。

我使用以下内容创建了grid_image.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<style>
    <item name="android:layout_height">50dp</item>
    <item name="android:layout_width">50dp</item>
    <item name="android:background">@color/white</item>
    <item name="android:padding">2dp</item>
    <item name="android:cropToPadding">true</item>
    <item name="android:scaleType">fitXY</item>
</style>

然后在我的网格视图适配器中,我输入以下代码:

public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {
        XmlPullParser parser = mContext.getResources().getXml(R.xml.grid_image);
        AttributeSet attributes = null;
        int state = XmlPullParser.END_DOCUMENT;
        do {
            try {
                state = parser.next();
                if (state == XmlPullParser.START_TAG) {
                    if (parser.getName().equals("style")) {
                        attributes = Xml.asAttributeSet(parser);
                        break;
                    }
                }
            } catch (Exception ignore) {
            //ignore it - can't do much anyway
        } while(state != XmlPullParser.END_DOCUMENT);

        if(attributes == null)
            imageView = new ImageView(mContext);
        else
            imageView = new ImageView(mContext, attributes);

        //the rest of initialisation goes here
    } else {
        imageView = (ImageView) convertView;
    }

    //set image into the imageview here

    return imageView;
}