我第一次收到1张毕加索图像,第二次收到它们

时间:2016-12-08 14:26:16

标签: android image bitmap imageview picasso

我在for循环中制作了毕加索的目标。我第一次收到1张图片,其他图片进入'onPrepareLoad'语句。我第二次收到所有图片。为什么第一次不能正常工作?

代码:

        final Set<Target> protectedFromGarbageCollectorTargets = new HashSet<>();

        for (int i = 0; i < alleFotosArray.size(); i++) {
            final Target target = new Target() {
                @Override
                public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                    protectedFromGarbageCollectorTargets.remove(this);

                    int width = bitmap.getWidth();
                    int height = bitmap.getHeight();

                    deelnemerSizesFotos sizeFotos = new deelnemerSizesFotos();

                    sizeFotos.setWidth(width);
                    sizeFotos.setHeight(height);

                    sizeFotosArray.add(sizeFotos);
                }

                @Override
                public void onBitmapFailed(Drawable errorDrawable) {
                    protectedFromGarbageCollectorTargets.remove(this);

                    deelnemerSizesFotos sizeFotos = new deelnemerSizesFotos();

                    sizeFotos.setWidth(200);
                    sizeFotos.setHeight(200);

                    sizeFotosArray.add(sizeFotos);
                }

                @Override
                public void onPrepareLoad(Drawable placeHolderDrawable) {
                    deelnemerSizesFotos sizeFotos = new deelnemerSizesFotos();

                    sizeFotos.setWidth(200);
                    sizeFotos.setHeight(200);

                    sizeFotosArray.add(sizeFotos);
                }
            };

            protectedFromGarbageCollectorTargets.add(target);
            Picasso.with(c).load(alleFotosArray.get(i).getImageurl()).into(target);
}

3 个答案:

答案 0 :(得分:0)

向Picasso添加内存政策

Picasso.with(c).load(alleFotosArray.get(i).getImageurl()).memoryPolicy(MemoryPolicy.NO_CACHE).into(target);

答案 1 :(得分:0)

你只需要保留对你创建的目标对象的引用,因为Picasso使用WeakReference,因为Android 2.3(API Level 9),垃圾收集器更积极地收集软/弱引用。< / p>

在活动中保留引用的示例

public class YourActivity extends Activity {

    private List<Target> targetList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getImages();
    }

    private void getImages() {
        for (int i = 0; i < alleFotosArray.size(); i++) {
            final Target target = new Target() {
                @Override
                public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                    int width = bitmap.getWidth();
                    int height = bitmap.getHeight();

                    deelnemerSizesFotos sizeFotos = new deelnemerSizesFotos();

                    sizeFotos.setWidth(width);
                    sizeFotos.setHeight(height);

                    sizeFotosArray.add(sizeFotos);
                }

                @Override
                public void onBitmapFailed(Drawable errorDrawable) {
                    deelnemerSizesFotos sizeFotos = new deelnemerSizesFotos();

                    sizeFotos.setWidth(200);
                    sizeFotos.setHeight(200);

                    sizeFotosArray.add(sizeFotos);
                }

                @Override
                public void onPrepareLoad(Drawable placeHolderDrawable) {
                    deelnemerSizesFotos sizeFotos = new deelnemerSizesFotos();

                    sizeFotos.setWidth(200);
                    sizeFotos.setHeight(200);

                    sizeFotosArray.add(sizeFotos);
                }
            };

            targetList.add(target);
            Picasso.with(c).load(alleFotosArray.get(i).getImageurl()).into(target);
        }
    }
}

答案 2 :(得分:0)

我使用的是ViewPager,因此我不必立即加载所有图片。如果我想我可以在&#39; onPageSelected&#39;中逐个加载它们。 ViewPager的方法。

代码:

AndroidImageAdapter adapterView = new AndroidImageAdapter(c, alleFotosArray, left, right, flImage);
        mViewPager.setAdapter(adapterView);

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                    Picasso.with(c).load(alleFotosArray.get(position).getImageurl()).into(new Target() {
                        @Override
                        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                            int width = bitmap.getWidth();
                            int height = bitmap.getHeight();

                            deelnemerSizesFotos sizeFotos = new deelnemerSizesFotos();

                            sizeFotos.setWidth(width);
                            sizeFotos.setHeight(height);

                            sizeFotosArray.add(sizeFotos);

                            try {
                                int widthFoto = bitmap.getWidth();
                                int heightFoto = bitmap.getHeight();

                                heightContainer = mViewPager.getHeight();
                                widthContainer = mViewPager.getWidth();

                                if (widthFoto == 0 || heightFoto == 0) {
                                    widthFoto = 200;
                                    heightFoto = 200;
                                }

                                    FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) flImage.getLayoutParams();

                                    //in het begin krijg je 1 foto te zien van de vorige pagina. Omdat ik de breedte en hoogte niet van de container krijg
                                    //heb ik deze if gemaakt waarbij de hoogte en breedte wordt opgehaald
                                    if (heightContainer == 0 || widthContainer == 0) {
                                        heightContainer = params.height;

                                        //breedte scherm pixels ophalen
                                        DisplayMetrics metrics = new DisplayMetrics();
                                        WindowManager windowManager = (WindowManager) c.getSystemService(Context.WINDOW_SERVICE);
                                        windowManager.getDefaultDisplay().getMetrics(metrics);

                                        widthContainer = metrics.widthPixels;
                                    }

                                    float r = (float) widthFoto / (float) widthContainer;
                                    float h = (float) heightFoto / r;

                                    // Changes the height and width to the specified *pixels*
                                    params.height = (int) h;
                                    params.width = widthContainer;

                                    flImage.setLayoutParams(params);
                            } catch (Exception e) {
                                FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) flImage.getLayoutParams();

                                //breedte scherm pixels ophalen
                                DisplayMetrics metrics = new DisplayMetrics();
                                WindowManager windowManager = (WindowManager) c.getSystemService(Context.WINDOW_SERVICE);
                                windowManager.getDefaultDisplay().getMetrics(metrics);

                                params.width = metrics.widthPixels;

                                flImage.setLayoutParams(params);
                            } 

                        @Override
                        public void onBitmapFailed(Drawable errorDrawable) {
                            deelnemerSizesFotos sizeFotos = new deelnemerSizesFotos();

                            sizeFotos.setWidth(200);
                            sizeFotos.setHeight(200);

                            sizeFotosArray.add(sizeFotos);
                        }

                        @Override
                        public void onPrepareLoad(Drawable placeHolderDrawable) {
                            deelnemerSizesFotos sizeFotos = new deelnemerSizesFotos();

                            sizeFotos.setWidth(200);
                            sizeFotos.setHeight(200);

                            sizeFotosArray.add(sizeFotos);
                        }
                    });
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });