问题
我正在编写一个安卓应用程序,希望它具有“幻灯片”功能。我找到了这个不错的库,基于Picasso,完全符合我的需求,并且大多数时候都可以正常工作。
问题是,有时我的图片无法加载到幻灯片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设备上滑块起作用了。什么鬼???
到目前为止我都尝试了什么
我尝试了不同的图片,加载多个图片到幻灯片上,甚至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>
我正在跟随教程,它来自于维基页面。但是LogCat没有抛出任何错误,这让问题变得更加困难。
你有什么想法吗?