可绘制从旋转错误的资源文件夹拉 [英] Drawables pulled from wrong resource folder on rotation

查看:87
本文介绍了可绘制从旋转错误的资源文件夹拉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

拉​​我的头发在这里。所以,我正在与有几种类型的可绘制的应用程序,我有他们在像这样的结构:

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/

修改我也试过,甚至把人像专用可绘制在绘制端口-MDPI 等具有相同的结果。

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

我希望,当我旋转为横向(让我先澄清一下,我的我不是办理 configChanges 在清单中该活动),这查看重新创建, mBackgroundView 应得到提拉绘制,陆地xhdpi 。相反,当我旋转回来,我应该得到的绘制-xhdpi提拉

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?

推荐答案

所以,我已经想通了,哪里出了问题所在,我真的不知道,如果是我的误解或在Android中的一个错误。我没想到添加这是问题的一部分(回头看,我应该有),但我绘制了一层列表绘制与彼此相叠,两个图像(一个影子,一个图像上面的阴影):

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.

我改为使用图层列表绘制的两个对齐ImageViews代替,一切都很好。

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

这篇关于可绘制从旋转错误的资源文件夹拉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆