1

我正在为我正在开发的应用程序编写一些 UI 测试,我需要根据推送通知切换图像。为了在没有推送通知的情况下编写测试,我在 androidTest res 目录中放置了一个 png 文件,然后使用来自测试资源的资源 id 使用 ContextCompat.getDrawable() 加载它。我还使用应用程序资源中的 id 从应用程序资源加载相同的图像。

这是加载资源的代码:

Drawable drawable1 = ContextCompat.getDrawable(setupRule.getActivity().getApplicationContext(), R.drawable.my_contact_pic);
Drawable drawable2 = ContextCompat.getDrawable(setupRule.getActivity().getApplicationContext(), com.mycompany.myapp.devenv.test.R.drawable.my_contact_pic);

drawable1 是 BitmapDrawable 的一个实例,drawable2 是 NinePatchDrawable 的一个实例。由于drawable2的底层PNG是drawable1的PNG的副本,我希望它们都是BitmapDrawables,我可以围绕这个进行编码,但想了解为什么会发生这种情况以供将来参考。

4

1 回答 1

0

所以我终于想通了,要从androidTest资源加载png,你必须指定InstrumentationRegister.getContext(),否则它会从应用程序资源加载。巧合的是,我指定的资源 ID 指向了 NinePatch 文件。

于 2017-01-05T22:42:31.653 回答