使用去codeSampledBitmapFromResource较差的图像质量,反正改善呢? [英] Poor image quality using decodeSampledBitmapFromResource, anyway to improve this?

查看:494
本文介绍了使用去codeSampledBitmapFromResource较差的图像质量,反正改善呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个图片轮播我已经建立了使用ViewFlipper而是存储在设备上的图像的质量我的应用程序都很差。我已经使用了以下内容:

 字符串fileLoc = mediaData.get(fileLoc);
ImageView的的ImageItem =新ImageView的(getApplicationContext());
的InputStream的ImageStream = NULL;
尝试{
     档案文件=新的文件(fileLoc);
     的ImageStream =新的FileInputStream(文件);
     imageItem.setImageBitmap(Configurator.de codeSampledBitmapFromResource(getResources(),fileLoc,100,100));
}赶上...

内存是可以有任意数量的图像的显示关注

我发现这一点:

<一个href=\"http://stackoverflow.com/questions/11567599/resource-to-bitmap-conversion-results-in-poor-quality\">Resource在质量差位图转换的结果。

但我不能看到如何将其翻译为我的处境。

每@Ultimo_m,这里是需要采取什么措施的细节:


  1. 用户是与具有基于所选previous选项三部分的屏幕psented $ P $:PDF文档,图像和视频

  2. 的用户从集合中所示的图像

  3. 用户被带到一个屏幕,显示所选择的项目

  4. 从右至左刷卡显示了在集
  5. 下一张图片
  6. 从左至右刷卡显示该组中的previous图片

现在,我使用的是ViewFlipper:


  1. 我通过每个图像的位置的阵列通过共享preference

  2. 我也传递当前图像
  3. 的关键位置
  4. 我通过阵列构建ViewFlipper环

  5. 我使用当前键设置当前视图升降舵

运用@Ultimo_m所选库我怎么告诉库当前图像显示以及下一个和previous图像将是什么?

----- -----编辑

我已经从挖掘时万能图像加载器的当前图像上工作previous屏幕。我不能在这个文档中找到如何处理事件刷卡,告诉图书馆旁边显示的内容。目前在建刷卡事件库的GitHub的一个截图,但我怎么利用呢?

----- -----编辑

应用程序崩溃。在 imageLoadView.setAdapter抛出一个空指针(新ImagePagerAdapter(imageArraySet)); 。当你指定我所做的XML文件。我没有作出调整,以您的code来从共享preF 我catlogged我的数组,我通过我的数组( imageArraySet )和数组是正确的。我的code粘贴后完整的错误日志:

 公共类浏览器扩展baseActivity {    clientDB clientDB =新ClientDB(本);    公共静态最后弦乐preFS_NAME =我的preFS
    共享preferences storedInfo;    串chosenImg;    ViewPager imageLoadView;    DisplayImageOptions选择;    的String [] imageArraySet;    // LoadImageUtil mLoadImageUtil;
    私有静态最后弦乐STATE_POSITION =STATE_POSITION;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_viewer);        束束= getIntent()getExtras()。
        断言包!= NULL;        storedInfo = getShared preferences(preFS_NAME,0);
        字符串mediaID = storedInfo.getString(imgId,NULL);全部通过影像// IDS
        字符串chosenImg = storedInfo.getString(chosenImg,NULL); //当前图像的ID        串[] imageArray = mediaID.split(,);
        imageArraySet = clientDB.getMediaDataSet(imageArray);
        INT pagerPosition = 0;        imageLoadView =(ViewPager)findViewById(R.id.imageLoadView);
        imageLoadView.setAdapter(新ImagePagerAdapter(imageArraySet));
        imageLoadView.setCurrentItem(pagerPosition);
    }    @覆盖
    公共无效的onSaveInstanceState(捆绑outState){
        outState.putInt(STATE_POSITION,imageLoadView.getCurrentItem());
    }    私有类ImagePagerAdapter扩展PagerAdapter {        私有String []图像;
        私人LayoutInflater吹气;        ImagePagerAdapter(字符串[]的图像){
            this.images =图像;
            充气= getLayoutInflater();
        }        @覆盖
        公共无效destroyItem(ViewGroup中的容器,INT位置,Object对象){
            container.removeView((查看)对象);
        }        @覆盖
        公众诠释的getCount(){
            返回images.length;
        }        @覆盖
        公共对象instantiateItem(ViewGroup中认为,INT位置){
            查看imageLayout = inflater.inflate(R.layout.item_pager_image,查看,FALSE);
            断言imageLayout!= NULL;
            ImageView的ImageView的=(ImageView的)imageLayout.findViewById(R.id.image);
            最终进度微调=(进度)imageLayout.findViewById(R.id.loading);            imageLoader.displayImage(图像[位置],ImageView的,选项,新SimpleImageLoadingListener(){
                @覆盖
                公共无效onLoadingStarted(字符串imageUri,查看视图){
                    spinner.setVisibility(View.VISIBLE);
                }                @覆盖
                公共无效onLoadingFailed(字符串imageUri,观景,FailReason failReason){
                    字符串消息= NULL;
                    开关(failReason.getType()){
                        案例IO_ERROR:
                            消息=输入/输出错误;
                            打破;
                        案例DECODING_ERROR:
                            消息=图片不能去codeD;
                            打破;
                        案例NETWORK_DENIED:
                            消息=下载被拒绝;
                            打破;
                        案例OUT_OF_MEMORY:
                            消息=内存不足的错误;
                            打破;
                        案例UNKNOWN:
                            消息=未知错误;
                            打破;
                    }
                    //Toast.makeText(ImagePagerActivity.this,消息,Toast.LENGTH_SHORT).show();                    spinner.setVisibility(View.GONE);
                }                @覆盖
                公共无效onLoadingComplete(字符串imageUri,观景,位图loadedImage){
                    spinner.setVisibility(View.GONE);
                }
            });            view.addView(imageLayout,0);
            返回imageLayout;
        }        @覆盖
        公共布尔isViewFromObject(查看视图,Object对象){
            返回view.equals(对象);
        }        @覆盖
        公共无效restoreState(Parcelable状态,ClassLoader的装载机){
        }        @覆盖
        公共Parcelable saveState和(){
            返回null;
        }
    }}

错误日志:

  6-12 16:41:30.455:W / dalvikvm(19960):主题ID = 1:螺纹未捕获的异常退出(组= 0x41fdfe10)
06-12 16:41:30.455:E / AndroidRuntime(19960):致命异常:主要
06-12 16:41:30.455:E / AndroidRuntime(19960):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.client.clientconfigurator / com.client.clientconfigurator.Viewer}:显示java.lang.NullPointerException
06-12 16:41:30.455:E / AndroidRuntime(19960):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
06-12 16:41:30.455:E / AndroidRuntime(19960):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
06-12 16:41:30.455:E / AndroidRuntime(19960):在android.app.ActivityThread.access $ 700(ActivityThread.java:150)
06-12 16:41:30.455:E / AndroidRuntime(19960):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1280)
06-12 16:41:30.455:E / AndroidRuntime(19960):在android.os.Handler.dispatchMessage(Handler.java:99)
06-12 16:41:30.455:E / AndroidRuntime(19960):在android.os.Looper.loop(Looper.java:176)
06-12 16:41:30.455:E / AndroidRuntime(19960):在android.app.ActivityThread.main(ActivityThread.java:5279)
06-12 16:41:30.455:E / AndroidRuntime(19960):在java.lang.reflect.Method.invokeNative(本机方法)
06-12 16:41:30.455:E / AndroidRuntime(19960):在java.lang.reflect.Method.invoke(Method.java:511)
06-12 16:41:30.455:E / AndroidRuntime(19960):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1102)
06-12 16:41:30.455:E / AndroidRuntime(19960):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
06-12 16:41:30.455:E / AndroidRuntime(19960):在dalvik.system.NativeStart.main(本机方法)
06-12 16:41:30.455:E / AndroidRuntime(19960):致:显示java.lang.NullPointerException
06-12 16:41:30.455:E / AndroidRuntime(19960):在com.client.clientconfigurator.Viewer.onCreate(Viewer.java:92)
06-12 16:41:30.455:E / AndroidRuntime(19960):在android.app.Activity.performCreate(Activity.java:5267)


解决方案

我建议你使用通用图像装载机,它采用异步的图片加载,缓存和显示,那里你可以找到的例子如何实现它。你这样做后,你不必了解影像的数量表示关注。

下面我会给你一个例子如何使用它:

第一创建这个LoadImageUtil.class

 公共类LoadImageUtil {    保护ImageLoader的ImageLoader的= ImageLoader.getInstance();
    DisplayImageOptions选择;
    公共布尔memoryManage(INT项){//用这个来清除缓存
        开关(项目){
            情况下0:
                imageLoader.clearMemoryCache();
                返回true;
            情况1:
                imageLoader.clearDiscCache();
                返回true;
            默认:
                返回false;
        }
    }    公共LoadImageUtil(上下文mContext){
        initUILSettings(mContext);
    }    公共无效initUILSettings(上下文mContext){        imageLoader.init(ImageLoaderConfiguration.createDefault(mContext));        选项​​=新DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.calice)
                .showImageForEmptyUri(R.drawable.calice)
                .showImageOnFail(R.drawable.calice)
                .cacheInMemory(真)
                .cacheOnDisc(真)
                .considerExifParams(真)
                .bitmapConfig(Bitmap.Config.RGB_565)
                。建立();    }    公共无效loadBitmapToImageView(ImageView的myImageView,弦乐的ImagePath){        imageLoader.displayImage(的ImagePath,myImageView,期权,新SimpleImageLoadingListener(){
            @覆盖
            公共无效onLoadingComplete(字符串imageUri,观景,位图loadedImage){
                super.onLoadingComplete(imageUri,查看loadedImage);            }            @覆盖
            公共无效onLoadingFailed(字符串imageUri,观景,FailReason failReason){
               // Log.e(onLoadingFailed:+ imageUri,failReason:+ failReason);            }            @覆盖
            公共无效onLoadingCancelled(字符串imageUri,查看视图){
                super.onLoadingCancelled(imageUri,视图);
            }
        });
    }
}

要加载图像的第2 从你的类做到这一点:

  LoadImageUtil mLoadImageUtil =新LoadImageUtil(getApplicationContext());

第三要在其中加载图像将这个:

  mLoadImageUtil.loadBitmapToImageView(YourImageView,ImagePathOrUrl);

在这里,你有,你应该通过在加载图像法imagesPath的例子

 字符串imageUri =htt​​p://8.8.8.8/image.png; //来自网页
字符串imageUri =文件:///mnt/sdcard/image.png; //从SD卡
字符串imageUri =内容://媒体/外部/音频/ albumart / 13; //从内容提供商
字符串imageUri =资产://image.png //从资产
字符串imageUri =绘制://+ R.drawable.image; //从绘制资源(仅适用于图像,非9patch)


编辑:

在我看来,这将是最好的,如果你会用ViewPager来解决你的问题,下面我会告诉你一些code如何根据我告诉你该库做的:

  / **
 * @author谢尔盖Tarasevich(nostra13 [在]的Gmail [点] com)
 * /
公共类ImagePagerActivity延伸活动{    私有静态最后弦乐STATE_POSITION =STATE_POSITION;
    ViewPager寻呼机;
     LoadImageUtil mLoadImageUtil;    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.ac_image_pager);             //在本实施例中的数据被传递到用捆此活动
        束束= getIntent()getExtras()。
        断言包!= NULL;            // imageUrls是包含图像的URL阵列加载
        的String [] = imageUrls bundle.getStringArray(Extra.IMAGES);        INT pagerPosition = bundle.getInt(Extra.IMAGE_POSITION,0);        如果(savedInstanceState!= NULL){
            pagerPosition = savedInstanceState.getInt(STATE_POSITION);
        }             mLoadImageUtil =新LoadImageUtil(getApplicationContext());
        寻呼机=(ViewPager)findViewById(R.id.pager);
        pager.setAdapter(新ImagePagerAdapter(imageUrls));
        pager.setCurrentItem(pagerPosition);
    }    @覆盖
    公共无效的onSaveInstanceState(捆绑outState){
        outState.putInt(STATE_POSITION,pager.getCurrentItem());
    }    私有类ImagePagerAdapter扩展PagerAdapter {        私有String []图像;
        私人LayoutInflater吹气;        ImagePagerAdapter(字符串[]的图像){
            this.images =图像;
            充气= getLayoutInflater();
        }        @覆盖
        公共无效destroyItem(ViewGroup中的容器,INT位置,Object对象){
            container.removeView((查看)对象);
        }
           //视图寻呼机将具有相同数目的页数作为字符串数组的长度
        @覆盖
        公众诠释的getCount(){
            返回images.length;
        }        @覆盖
        公共对象instantiateItem(ViewGroup中认为,INT位置){
            查看imageLayout = inflater.inflate(R.layout.item_pager_image,查看,FALSE);
            断言imageLayout!= NULL;
            ImageView的ImageView的=(ImageView的)imageLayout.findViewById(R.id.image);
               //这里是形象是每个寻呼机负荷部分
               mLoadImageUtil.loadBitmapToImageView(ImageView的,图像[位置]);            view.addView(imageLayout,0);
            返回imageLayout;
        }        @覆盖
        公共布尔isViewFromObject(查看视图,Object对象){
            返回view.equals(对象);
        }        @覆盖
        公共无效restoreState(Parcelable状态,ClassLoader的装载机){
        }        @覆盖
        公共Parcelable saveState和(){
            返回null;
        }
    }
}

下面是XML文件:

ac_image_pager.xml

 &LT;?XML版本=1.0编码=UTF-8&GT?;
&LT; android.support.v4.view.ViewPager的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID /寻呼机
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT/&GT;

item_pager_image.xml

 &LT;?XML版本=1.0编码=UTF-8&GT?;
&LT;的FrameLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:填充=1dip&GT;    &LT; ImageView的
        机器人:ID =@ + ID /图像
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_gravity =中心
        机器人:adjustViewBounds =真
        机器人:contentDescription =@字符串/ descr_image/&GT;&LT; /&的FrameLayout GT;

I have an image carousel I've built for my app using a ViewFlipper but the quality of the images stored on the device are very poor. I've using the following:

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...

Memory is a concern as there could be any number of images displayed.

I did find this:

Resource to bitmap conversion results in poor quality

But I can't see how to translate it to my situation.

Per @Ultimo_m , Here is detail on what needs to happen:

  1. The user is presented with a screen that has three sections based on previous options selected: pdf documents, images, and videos.
  2. The user selects an image from the set shown
  3. User is taken to a screen that shows the selected item
  4. Swiping from right to left shows the next image in that set
  5. Swiping from left to right shows the previous image in the set

Right now I'm using a ViewFlipper:

  1. I pass an array of each image's location via a SharedPreference
  2. I also pass the key location of the current image
  3. I loop through the array to build the ViewFlipper
  4. I use the current key to set the current view in the flipper

Using the selected library from @Ultimo_m how do I tell the library the current image to show as well what the next and previous image will be?

----- EDIT -----

I have Univeral Image loader working on the current image when tapped from the previous screen. I can't find in the docs how to handle the swipe event and tell the library what to display next. There is a screenshot on the library's github of a swipe event in progress but how do I leverage this?

----- EDIT -----

App is crashing. Throwing a NullPointer at imageLoadView.setAdapter(new ImagePagerAdapter(imageArraySet));. I've made the xml files as you specified. I did make adjustments to your code to get my array from the SharedPref I've catlogged my array that I'm passing (imageArraySet) and the array is correct. I've pasted the full error log after the code:

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;
        }
    }



}

Error log:

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)

解决方案

I suggest you use Universal Image Loader, it uses asynchronous image loading, caching and displaying, there you can find examples how to implement it. After you do it you dont have to concern about the number of images.

Here i will give you an example how to use it:

First create this 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);                
            }
        });
    }
}

Second from your class where you want to load the images do this:

LoadImageUtil mLoadImageUtil = new LoadImageUtil(getApplicationContext());

Third Put this where you want to load image:

mLoadImageUtil.loadBitmapToImageView(YourImageView, ImagePathOrUrl);

Here you have examples of imagesPath that you should pass at the method for loading images

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)


EDIT:

In my opinion it would be best if you would use ViewPager to solve your problem, below i will show you some code how to do that based on the library I told you:

/**
 * @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;
        }
    }
}

Here are the xml files:

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>

这篇关于使用去codeSampledBitmapFromResource较差的图像质量,反正改善呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆