Android Gallery App

时间:2016-02-25 15:50:47

标签: android performance

我已编写此代码以在android上运行。它从android获取所有图像并将它们放在网格视图中。当我使用相同的图像而不缩小时,表示设备内存不足。当我缩小图像时,它说应用程序停止了。

   This also says in the logcat (The application is doing too much work at its first activity.)

错误在此行

 imageView.setLayoutParams(new GridView.LayoutParams(100,100));

下面是代码的其余部分。请帮我解决这个问题,或者让我有任何想法让画廊就像Android内置的画廊一样快速和负责。

  public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView = null;
    if(convertView == null)
    {

        imageView = new ImageView(activity);

        imageView.setLayoutParams(new GridView.LayoutParams(100,100));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8,8,8,8);
    }
    else
    {
        imageView = (ImageView)convertView;
    }
    Bitmap bmp = BitmapFactory.decodeFile(filepath[position]);
    imageView.setImageBitmap(bmp);
    return imageView;
}

1 个答案:

答案 0 :(得分:3)

如果没有看到痕迹,很难说出您的应用程序花费了所有时间。从它的声音中你转换并重新调整MainThread上的所有图像,这会锁定你的应用程序。

我建议使用像Glide这样的库来处理将图像加载到图像视图中。一定要调整图像大小。你最终会得到类似下面的内容

def token = Action.async { implicit request =>


    // 1. Get Future for read on cache

    val cacheFuture = scala.concurrent.Future {   
        cache.get[String](id)    
    }


    // 2. Map inside cache Future to call web service

    cacheFuture.map { result =>   

        WS.url(url).withQueryString("id" -> result).get().map { response =>
            // process response
            Ok(responseData)
        }

    }

}

另一种选择是Picasso,这与Glide非常相似。