我有一个使用 ViewFlipper 为我的应用程序构建的图像轮播,但存储在设备上的图像质量很差。我正在使用以下内容:
String fileLoc = mediaData.get("fileLoc");
ImageView imageItem = new ImageView(getApplicationContext());
InputStream imageStream = null;
try {
File file = new File(fileLoc);
imageStream = new FileInputStream(file);
imageItem.setImageBitmap(Configurator.decodeSampledBitmapFromResource(getResources(), fileLoc, 100, 100));
} catch...
内存是一个问题,因为可能显示任意数量的图像。
我确实找到了这个:
但我看不出如何将其转化为我的情况。
根据@Ultimo_m,以下是需要发生的事情的详细信息:
- 用户会看到一个屏幕,该屏幕具有三个部分,基于先前选择的选项:pdf 文档、图像和视频。
- 用户从显示的集合中选择一个图像
- 用户被带到显示所选项目的屏幕
- 从右向左滑动显示该集中的下一张图片
- 从左向右滑动可显示集合中的上一张图片
现在我正在使用 ViewFlipper:
- 我通过 SharedPreference 传递每个图像位置的数组
- 我也传了当前图片的关键位置
- 我遍历数组以构建 ViewFlipper
- 我使用当前键在脚蹼中设置当前视图
使用从@Ultimo_m 中选择的库,我如何告诉库要显示的当前图像以及下一个和上一个图像是什么?
- - - 编辑 - - -
从上一个屏幕点击时,我有通用图像加载器处理当前图像。我在文档中找不到如何处理滑动事件并告诉库接下来要显示什么。图书馆的 github 上有一个正在进行的滑动事件的屏幕截图,但我该如何利用它呢?
- - - 编辑 - - -
应用程序崩溃。在 .处抛出一个 NullPointer imageLoadView.setAdapter(new ImagePagerAdapter(imageArraySet));
。我已经按照您的指定制作了 xml 文件。我确实对您的代码进行了调整,以从SharedPref
我已经记录了我正在传递的数组(imageArraySet
)中获取我的数组,并且数组是正确的。我在代码之后粘贴了完整的错误日志:
public class Viewer extends baseActivity {
clientDB clientDB = new ClientDB(this);
public static final String PREFS_NAME = "myPrefs";
SharedPreferences storedInfo;
String chosenImg;
ViewPager imageLoadView;
DisplayImageOptions options;
String[] imageArraySet;
//LoadImageUtil mLoadImageUtil;
private static final String STATE_POSITION = "STATE_POSITION";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_viewer);
Bundle bundle = getIntent().getExtras();
assert bundle != null;
storedInfo = getSharedPreferences(PREFS_NAME,0);
String mediaID = storedInfo.getString("imgId", null); //ids of all passed images
String chosenImg = storedInfo.getString("chosenImg", null); //id of current image
String[] imageArray = mediaID.split(",");
imageArraySet = clientDB.getMediaDataSet(imageArray);
int pagerPosition = 0;
imageLoadView = (ViewPager) findViewById(R.id.imageLoadView);
imageLoadView.setAdapter(new ImagePagerAdapter(imageArraySet));
imageLoadView.setCurrentItem(pagerPosition);
}
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putInt(STATE_POSITION, imageLoadView.getCurrentItem());
}
private class ImagePagerAdapter extends PagerAdapter {
private String[] images;
private LayoutInflater inflater;
ImagePagerAdapter(String[] images) {
this.images = images;
inflater = getLayoutInflater();
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public int getCount() {
return images.length;
}
@Override
public Object instantiateItem(ViewGroup view, int position) {
View imageLayout = inflater.inflate(R.layout.item_pager_image, view, false);
assert imageLayout != null;
ImageView imageView = (ImageView) imageLayout.findViewById(R.id.image);
final ProgressBar spinner = (ProgressBar) imageLayout.findViewById(R.id.loading);
imageLoader.displayImage(images[position], imageView, options, new SimpleImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
spinner.setVisibility(View.VISIBLE);
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
String message = null;
switch (failReason.getType()) {
case IO_ERROR:
message = "Input/Output error";
break;
case DECODING_ERROR:
message = "Image can't be decoded";
break;
case NETWORK_DENIED:
message = "Downloads are denied";
break;
case OUT_OF_MEMORY:
message = "Out Of Memory error";
break;
case UNKNOWN:
message = "Unknown error";
break;
}
//Toast.makeText(ImagePagerActivity.this, message, Toast.LENGTH_SHORT).show();
spinner.setVisibility(View.GONE);
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
spinner.setVisibility(View.GONE);
}
});
view.addView(imageLayout, 0);
return imageLayout;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view.equals(object);
}
@Override
public void restoreState(Parcelable state, ClassLoader loader) {
}
@Override
public Parcelable saveState() {
return null;
}
}
}
错误日志:
6-12 16:41:30.455: W/dalvikvm(19960): threadid=1: thread exiting with uncaught exception (group=0x41fdfe10)
06-12 16:41:30.455: E/AndroidRuntime(19960): FATAL EXCEPTION: main
06-12 16:41:30.455: E/AndroidRuntime(19960): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.client.clientconfigurator/com.client.clientconfigurator.Viewer}: java.lang.NullPointerException
06-12 16:41:30.455: E/AndroidRuntime(19960): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
06-12 16:41:30.455: E/AndroidRuntime(19960): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
06-12 16:41:30.455: E/AndroidRuntime(19960): at android.app.ActivityThread.access$700(ActivityThread.java:150)
06-12 16:41:30.455: E/AndroidRuntime(19960): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
06-12 16:41:30.455: E/AndroidRuntime(19960): at android.os.Handler.dispatchMessage(Handler.java:99)
06-12 16:41:30.455: E/AndroidRuntime(19960): at android.os.Looper.loop(Looper.java:176)
06-12 16:41:30.455: E/AndroidRuntime(19960): at android.app.ActivityThread.main(ActivityThread.java:5279)
06-12 16:41:30.455: E/AndroidRuntime(19960): at java.lang.reflect.Method.invokeNative(Native Method)
06-12 16:41:30.455: E/AndroidRuntime(19960): at java.lang.reflect.Method.invoke(Method.java:511)
06-12 16:41:30.455: E/AndroidRuntime(19960): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
06-12 16:41:30.455: E/AndroidRuntime(19960): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
06-12 16:41:30.455: E/AndroidRuntime(19960): at dalvik.system.NativeStart.main(Native Method)
06-12 16:41:30.455: E/AndroidRuntime(19960): Caused by: java.lang.NullPointerException
06-12 16:41:30.455: E/AndroidRuntime(19960): at com.client.clientconfigurator.Viewer.onCreate(Viewer.java:92)
06-12 16:41:30.455: E/AndroidRuntime(19960): at android.app.Activity.performCreate(Activity.java:5267)