Drawables pulled from wrong resource folder on rotation

浪尽此生 提交于 2019-12-23 15:15:35

问题


Pulling my hair out here. So I'm working with an application that has several types of drawables, and I have them in a structure like so:

res/
    //Portrait resources
    drawable-mdpi/
    drawable-hdpi/
    drawable-xhdpi/

    //Landscape resources
    drawable-land-mdpi/
    drawable-land-hdpi/
    drawable-land-xhdpi/

EDIT I've also tried even putting the portrait-specific drawables under drawable-port-mdpi, etc. with the same result.

And I have a custom View which I'm using to pull in drawables at runtime. Here's a pared-down example, but this is where I'm having an issue:

public class CustomView extends FrameLayout {
    private ImageView mBackgroundView;

    public CustomView (Context context) {
        super(context);
        initialize();
    }

    public CustomView (Context context, AttributeSet attrs) {
        super(context, attrs);
        initialize();
    }

    public CustomView (Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initialize();
    }

    private void initialize () {
        mBackgroundView = new ImageView(getContext());
        mBackgroundView.setImageResource(R.drawable.my_resource);
        addView(mBackgroundView, wrapContent());
    }

    private LayoutParams wrapContent () {
        return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    }
}   

I would expect that when I rotate to landscape (and let me first clarify, I AM NOT handling configChanges in the manifest for this Activity) and this View is re-created, mBackgroundView should get the drawable in drawable-land-xhdpi. Conversely when I rotate back, I should get the drawable in drawable-xhdpi.

Now here's where it gets weird. I can confirm that the drawables are all there and detected, as if I launch the app in landscape, I get the correct landscape drawable. However, on rotation, the landscape drawable remains, and I do not get the portrait one. Conversely, if I launch in portrait, I'm stuck with the portrait drawable.

Is there some special case that I'm supposed to handle to "reset" the resources or something when I'm grabbing them programmatically?


回答1:


So I've figured out where the problem resides, and I'm honestly not sure if it's my misunderstanding or a bug in Android. I didn't think to add this as part of the question (looking back, I should have) but my drawable was a layer-list drawable with two images stacked atop each other (one shadow, one image above the shadow) :

<layer-list xmlns:android="http://schemas.android.com/apk/res/android>
    <item>
        <bitmap android:src="@drawable/shadow"/>
    </item>

    <item>
        <bitmap android:src="@drawable/image" android:gravity="top|center_horizontal"/>
    </item>
</layer-list>

It seems that this only gets parsed out once (I'm assuming for efficiency it's reused) but it never goes back and reconstructs the layer-list with the drawables for the new configuration.

I changed to using two aligned ImageViews instead of a layer-list drawable and all is well.



来源:https://stackoverflow.com/questions/15983058/drawables-pulled-from-wrong-resource-folder-on-rotation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!