【发布时间】:2015-11-23 04:35:54
【问题描述】:
问题
我正在编写一个应该具有“幻灯片放映”功能的 Android 应用程序。我发现this nice library,基于毕加索,这正是我想要的,而且它在大多数情况下都工作得很好。
问题是,有时我的图片没有加载到幻灯片ImageView...它只显示一个“黑色画布”,如下面的屏幕截图所示。
我正在从我的可绘制对象的本地资源中加载图像。有时它发生在纵向模式下,有时发生在横向模式下。我使用哪个图像并不重要,有时会发生“黑色”。
编辑:
我使用的是 Android 5.0.2 和 4.4.2 - 在 4.4.2 上似乎没有发生。仅在 5.0.2 上。
它发生在装有 android 5.1 的 Moto X 2014 上。
我尝试加载的图像在磁盘上有 45KB,分辨率为 900x445。
我按照建议打开了布局矩形,结果如下:
它在滚动时保持这种状态。
有时,它会变成白色而不是黑色(或者先是白色,然后是黑色)。
我尝试过的其他东西:我的可绘制对象位于 res/drawable 文件夹中,当我将该文件夹中的文件更改为 res/drawable-xxxhdpi 时,滑块在 5.0.2 设备上工作. wtf???
到目前为止我做了什么
我尝试了不同的图像,在幻灯片上加载多个图像,甚至 this pull request 将 Picasso 更改为库上的 Glide。似乎没有任何效果,而且错误似乎是随机的。
一旦我尝试使用来自网络的 URL,而不是本地存储上的实际可绘制对象,它就起作用了。使用完全相同的图像。
这是我加载图像的方式:
Fragment.java
private SliderLayout slider;
private PagerIndicator indicator;
// ...
private void setupSlider() {
HashMap<String,Integer> file_maps = new HashMap<>();
file_maps.put("Blah",R.drawable.banner_1);
file_maps.put("Bleh",R.drawable.banner_2);
file_maps.put("Blih",R.drawable.banner_3);
file_maps.put("Bloh",R.drawable.banner_4);
for (String name : file_maps.keySet()) {
DefaultSliderView dsv = new DefaultSliderView(getActivity());
dsv.description(name)
.image(file_maps.get(name))
.error(R.drawable.banner_error)
.empty(R.drawable.empty)
.setScaleType(BaseSliderView.ScaleType.Fit)
.setOnSliderClickListener(this);
//add your extra information
dsv.bundle(new Bundle());
dsv.getBundle()
.putString("extra",name);
slider.addSlider(dsv);
}
slider.setPresetTransformer(SliderLayout.Transformer.Default);
slider.setCustomIndicator(indicator);
slider.setCustomAnimation(new DescriptionAnimation());
slider.setDuration(4000);
slider.addOnPageChangeListener(this);
}
fragment.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:custom="http://schemas.android.com/apk/res-auto"
tools:context="com.example.fragments.Fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/sv_main">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/rl_main">
<com.daimajia.slider.library.SliderLayout
android:id="@+id/slider"
android:layout_width="match_parent"
android:layout_height="200dp"
/>
<com.daimajia.slider.library.Indicators.PagerIndicator
android:id="@+id/custom_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/slider"
custom:shape="oval"
custom:selected_color="#00BFA5"
custom:unselected_color="#55333333"
custom:selected_padding_left="@dimen/spacing_medium"
custom:selected_padding_right="@dimen/spacing_medium"
custom:selected_padding_top="3dp"
custom:selected_padding_bottom="@dimen/spacing_small"
custom:unselected_padding_left="@dimen/spacing_medium"
custom:unselected_padding_right="@dimen/spacing_medium"
custom:unselected_padding_top="@dimen/spacing_small"
custom:unselected_padding_bottom="4dp"
custom:selected_width="@dimen/spacing_medium"
custom:selected_height="@dimen/spacing_medium"
custom:unselected_width="6dp"
custom:unselected_height="6dp"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/custom_indicator"
android:id="@+id/ll_main_body">
</LinearLayout>
</RelativeLayout>
</ScrollView>
<!-- Other stuff -->
</RelativeLayout>
我正在关注库 wiki page 中的 the tutorial。并且 LogCat 上没有抛出任何错误,这使得解决这个问题变得更加困难。
有什么想法吗?
【问题讨论】:
-
也许你得到了 OutOfMemoryError。您是否打开了 picasso 或 glide 的调试功能?可绘制对象有多大?
-
我会尝试在开发人员设置中启用布局矩形的显示,然后去确保实际上有一个黑色的图像视图,因为我猜测视图是宽度和/或高度 == 0。 ..
-
我在使用完全相同的库时遇到了同样的问题,然后我更改了我的测试设备,它在其他设备上运行良好。之前我在 Moto G 上进行了测试,但后来我在 Nexus 5 和 Note 3 上进行了测试,它运行良好。虽然不知道原因:/ 只是尝试另一个设备。
-
你的背景默认是黑色的吗?我知道毕加索喜欢对图像进行弱引用,所以这可能是个问题。
-
在 Glide 中查看相关问题:github.com/bumptech/glide/issues/738 有针对特定设备的修复,但如果有足够的信息,可以添加更多。
标签: android imageview android-drawable picasso android-glide