开发者

Code equivalent of android:cropToPadding

In my android app, I create thumbnails in xml this way:

<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" />

This creates a white border around the thumbnail - and looks quite nice.

Now, in another place in the code I need to create the thumbnails in the code, not XML (this is an Adapter class for GridView - to create a grid of thumbnails). I can set all parameters as required, however I cannot find a way to set the cropToPadding in the code. As a result, the thumbnail are drawn over the padding and it looks really ugly.

How do I set this cropToPadding thing in the code?

BTW, the app must work on Android 1.6.

Edit

Following a suggestion from userSeven7s, I add this style to my XML containing other styles:

<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>

(Note that the file contains <resources> root element and contains a couple of other <style> elements. Yet all of the other ones are only referenced form layout xml files.)

I then added this code inside my grid view adapter:

public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {
        XmlPullParser p开发者_运维知识库arser = 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
}

This compiles fine (i.e. R.style.GridImage exists). However when I run this code, the app crashes with Resources.NotFoundException on getXml(R.style.GridImage).

Any suggestions?


If you try the solution with AttributeSet and attributes.getAttributeCount() always returns 0, then just about the only solution left is this hack using reflection:

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

Using this you probably can't be sure it will work in other android firmware versions. Anyway the missing setter is probably just forgotten, so that justifies the hack for me.


You can get the thumbnails from the ImageView itself. Call getDrawingCache() on each imageview to get the bitmap used for drawing.

new Update :

Create a xml myimage_attrs.xml in xml folder and add all the imageview attributes you need to it.

<?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>
....

Then create a AttributeSet out of the xml and pass it to the ImageView constructor.

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

ImageView imgv = new ImageView(context, attributes);


For the benefit of others, here's what finally did work (thanks to userSeven7s for pushing me in the right direction).

I created grid_image.xml file with the following content:

<?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>

Then in my Adapter for the grid view, I put the following code:

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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜