我正在为我正在开发的应用程序编写一些 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,我可以围绕这个进行编码,但想了解为什么会发生这种情况以供将来参考。