本文介绍了毕加索目标onBitmapLoaded未在for循环中调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下将文件加载到Bitmap中并将其保存在内部目录(即PNG或JPG格式)中的代码段

Below code snippet for load file into Bitmap and save this file in internal directory(i.e PNG,or JPG format)

final List<Target> targets = new ArrayList<Target>();
final List<Target> targetsNormal = new ArrayList<Target>();
for (int j = 0; j < defaultTileImage.size(); j++) {

    final String slangTiles = defaultTileImage.get(j).getPairName() +
            ApplicationConstants.SLANG_TILES;
    final String normalTiles = defaultTileImage.get(j).getPairName() +
            ApplicationConstants.NORMAL_TILES;

    final int k = j;

    Target target = new Target() {
        @Override
        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
            Log.i("Targets", "Loaded: " + k);
            targets.remove(this);
            saveIntoBitmap(bitmap, slangTiles);
        }
        @Override
        public void onBitmapFailed(Drawable errorDrawable) {
            targets.remove(this);
        }
        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {
            Log.i("Targets", "Preparing: " + k);
        }
    };


    Target targetNormal = new Target() {
        @Override
        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
            Log.i("TargetsNormal", "Loaded: " + k);
            targetsNormal.remove(this);
            saveIntoBitmapSlang(bitmap, normalTiles);
        }
        @Override
        public void onBitmapFailed(Drawable errorDrawable) {
            targetsNormal.remove(this);
        }
        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {
            Log.i("TargetsNormal", "Preparing: " + k);
        }
    };
    targetsNormal.add(targetNormal);
    Picasso.with(MainActivity.this)
            .load(defaultTileImage.get(j).getNormalTileImg()) // Start loading the current target
            .resize(100, 100)
            .into(targetNormal);

    targets.add(target);
    Picasso.with(MainActivity.this)
            .load(defaultTileImage.get(j).getSlangTileImg()) // Start loading the current target
            .resize(100, 100)
            .into(target);
}

还访问了此链接,并根据该指南进行了实施,使Target成为了强有力的参考.但是很不幸,很多时候 onBitmapLoaded 没有得到任何回调.

Also visited this link and implemented as per this guideline that make Target a strong reference . But unfortunately many time onBitmapLoaded not get any callback.

非常感谢您为解决问题提供的帮助 m

推荐答案

确保targetstargetsNormal是全局变量,而不是局部变量.

Make sure targets and targetsNormal are global variables instead of local.

如果它们是局部变量,则可以在离开方法后立即对其进行垃圾回收.

If they are local variables, they can be garbage collected as soon as the method has been left.

这篇关于毕加索目标onBitmapLoaded未在for循环中调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 06:02