如何使使用多个位图的应用程序更具响应性? [英] How to make application more responsive which uses several bitmaps?

查看:17
本文介绍了如何使使用多个位图的应用程序更具响应性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Android 新手,所以我不知道如何让我的应用程序更具响应性,因为它为每个处理创建位图并设置为 imageView.基本上我要做的是创建位图,播放使用它,就像从 seekBar 传递值以更改其属性并将其设置为 imageView.如何创建 Bitmap 对象的副本以避免引用.任何建议?提前致谢

I'm new to Android so I'm not getting exactly how to make my application more responsive as it creates bitmaps for each processing and set to imageView.Basically What i'm trying to do is that create a bitmap, play with it,like passing values from seekBar to change its properties and set it to imageView.How to create a Copy of Bitmap object to avoid references.Any Suggestions ?? Thanks in advance

推荐答案

你可以试试这个非常有效地处理位图的库.
https://github.com/thest1/LazyList使用这个惰性列表库非常容易.它会自动缓存位图:-

You can try this library which handles bitmap very efficiently.
https://github.com/thest1/LazyList Its very easy to use this lazy list library. It does the job of caching bitmap automatically:-

 ImageLoader imageLoader=new ImageLoader(context);
 imageLoader.DisplayImage(url, imageView);

注意:

  • 不要忘记在您的 AndroidManifest.xml 中添加以下权限:

    NOTE :

  • Don't forget to add the following permissions to your AndroidManifest.xml:

      <uses-permission android:name="android.permission.INTERNET"/>
      <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    

  • 请只创建一个 ImageLoader 实例并在您的应用程序中重复使用它.这样图像缓存会更有效率.

  • Please create only one instance of ImageLoader and reuse it all around your application. This way image caching will be much more efficient.

    您还可以查看 Nostras ImageLoader,因为它可以有效地将图像加载到特定大小的容器中,即在您需要处理它们之前调整它们的大小和压缩它们.它还支持内容 uri,可以一次性帮助您.

    and also you can look into Nostras ImageLoader, as it efficiently handles loading images into specific sized containers, i.e resizing and compressing them, before you will need to handle them. It also supports content uris which will help you all at once.

    除此之外,如果一个 1024x768 像素的图像最终会在 ImageView 中以 128x96 像素的缩略图显示,那么将其加载到内存中是不值得的.您应该将缩小版本的图像加载到内存中.我还分享了很棒的位图实用程序类,它可以帮助您根据大小缩小图像:-

    Besides this,it’s not worth loading a 1024x768 pixel image into memory if it will eventually be displayed in a 128x96 pixel thumbnail in an ImageView. You should load a scaled down version of image into memory. I am also sharing wonderful utility class for bitmap which helps you to scale down your image according to size:-

    /**
    * Provides static functions to decode bitmaps at the optimal size
    */
    public class BitmapUtil {
    private BitmapUtil() {}
    
    /**
     * Returns Width or Height of the picture, depending on which size is smaller. Doesn't actually
     * decode the picture, so it is pretty efficient to run.
     */
    public static int getSmallerExtentFromBytes(byte[] bytes) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
    
        // don't actually decode the picture, just return its bounds
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);
    
        // test what the best sample size is
        return Math.min(options.outWidth, options.outHeight);
    }
    
    /**
     * Finds the optimal sampleSize for loading the picture
     * @param originalSmallerExtent Width or height of the picture, whichever is smaller
     * @param targetExtent Width or height of the target view, whichever is bigger.
     *
     * If either one of the parameters is 0 or smaller, no sampling is applied
     */
    public static int findOptimalSampleSize(int originalSmallerExtent, int targetExtent) {
        // If we don't know sizes, we can't do sampling.
        if (targetExtent < 1) return 1;
        if (originalSmallerExtent < 1) return 1;
    
        // test what the best sample size is
        int extent = originalSmallerExtent;
        int sampleSize = 1;
        while ((extent >> 1) >= targetExtent) {
            sampleSize <<= 1;
            extent >>= 1;
        }
    
        return sampleSize;
    }
    
    /**
     * Decodes the bitmap with the given sample size
     */
    public static Bitmap decodeBitmapFromBytes(byte[] bytes, int sampleSize) {
        final BitmapFactory.Options options;
        if (sampleSize <= 1) {
            options = null;
        } else {
            options = new BitmapFactory.Options();
            options.inSampleSize = sampleSize;
        }
        return BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);
      }
      }
    

    您也可以转到此链接 它将帮助您构建应用程序
    http://developer.android.com/training/displaying-bitmaps/index.html

    also You can go to this link It will help you to build the app
    http://developer.android.com/training/displaying-bitmaps/index.html

    这篇关于如何使使用多个位图的应用程序更具响应性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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