我有一个使用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...
内存是一个问题,因为可能会显示任意数量的图像。
我确实找到了这个:
Resource to bitmap conversion results in poor quality
但我看不出如何将其转化为我的情况。
Per @Ultimo_m,以下是需要发生的事情的详细信息:
现在我正在使用ViewFlipper:
使用@Ultimo_m中的选定库,如何告诉库当前图像以及下一张和上一张图像是什么?
-----编辑-----
从上一个屏幕点击时,我有Univeral Image loader处理当前图像。我在文档中找不到如何处理滑动事件并告诉库接下来要显示什么。正在进行滑动事件的库的github上有截图,但我该如何利用它呢?
-----编辑-----
应用程序正在崩溃。在imageLoadView.setAdapter(new ImagePagerAdapter(imageArraySet));
投掷NullPointer。我按照你的指定制作了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)
答案 0 :(得分:1)
我建议您使用Universal Image Loader,它使用异步图像加载,缓存和显示,您可以在其中找到如何实现它的示例。在你这样做之后,你不必担心图像的数量。
在这里,我将举例说明如何使用它:
首先创建此LoadImageUtil.class
public class LoadImageUtil {
protected ImageLoader imageLoader = ImageLoader.getInstance();
DisplayImageOptions options;
public boolean memoryManage(int item) {// use this to clear cache
switch (item) {
case 0:
imageLoader.clearMemoryCache();
return true;
case 1:
imageLoader.clearDiscCache();
return true;
default:
return false;
}
}
public LoadImageUtil(Context mContext) {
initUILSettings(mContext);
}
public void initUILSettings(Context mContext){
imageLoader.init(ImageLoaderConfiguration.createDefault(mContext));
options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.calice)
.showImageForEmptyUri(R.drawable.calice)
.showImageOnFail(R.drawable.calice)
.cacheInMemory(true)
.cacheOnDisc(true)
.considerExifParams(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
}
public void loadBitmapToImageView(ImageView myImageView, String ImagePath) {
imageLoader.displayImage(ImagePath, myImageView, options, new SimpleImageLoadingListener() {
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
super.onLoadingComplete(imageUri, view, loadedImage);
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
// Log.e("onLoadingFailed : " + imageUri, "failReason: " + failReason);
}
@Override
public void onLoadingCancelled(String imageUri, View view) {
super.onLoadingCancelled(imageUri, view);
}
});
}
}
您要加载图片的类中的第二执行此操作:
LoadImageUtil mLoadImageUtil = new LoadImageUtil(getApplicationContext());
第三次将其放在要加载图片的位置:
mLoadImageUtil.loadBitmapToImageView(YourImageView, ImagePathOrUrl);
这里有一些应该在加载图像的方法中传递的imagesPath的例子
String imageUri = "http://8.8.8.8/image.png"; // from Web
String imageUri = "file:///mnt/sdcard/image.png"; // from SD card
String imageUri = "content://media/external/audio/albumart/13"; // from content provider
String imageUri = "assets://image.png"; // from assets
String imageUri = "drawable://" + R.drawable.image; // from drawables (only images, non-9patch)
修改强>
在我看来,如果您使用ViewPager来解决您的问题最好,下面我将根据我告诉您的库向您展示一些代码:
/**
* @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
*/
public class ImagePagerActivity extends Activity {
private static final String STATE_POSITION = "STATE_POSITION";
ViewPager pager;
LoadImageUtil mLoadImageUtil;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ac_image_pager);
// in this example the data are passed to this activity using bundle
Bundle bundle = getIntent().getExtras();
assert bundle != null;
// imageUrls is the array that contains the images URL to load
String[] imageUrls = bundle.getStringArray(Extra.IMAGES);
int pagerPosition = bundle.getInt(Extra.IMAGE_POSITION, 0);
if (savedInstanceState != null) {
pagerPosition = savedInstanceState.getInt(STATE_POSITION);
}
mLoadImageUtil = new LoadImageUtil(getApplicationContext());
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(new ImagePagerAdapter(imageUrls));
pager.setCurrentItem(pagerPosition);
}
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putInt(STATE_POSITION, pager.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);
}
// the view pager will have the same number of pages as the length of the Strings array
@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);
// here is the part where image is load for each pager
mLoadImageUtil.loadBitmapToImageView(imageView, images[position]);
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;
}
}
}
以下是xml文件:
<强> ac_image_pager.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<强> item_pager_image.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="1dip" >
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:contentDescription="@string/descr_image" />
</FrameLayout>