壁画多个图像视图

时间:2017-02-16 12:36:09

标签: android image android-studio fresco

我正在尝试制作一个自定义视图,使用Fresco将多个图像显示在一个视图中。我已经阅读了文档并完成了一些步骤,但我的实现似乎不起作用。这是代码:

public class MultiDraweeView extends View {

    private static final int MAX_COUNT = 4;

    private MultiDraweeHolder<GenericDraweeHierarchy> multiDraweeHolder;
    private ArrayList<GenericDraweeHierarchy> hierarchies;

    public MultiDraweeView(Context context) {
        super(context);
        init();
    }

    public MultiDraweeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MultiDraweeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        multiDraweeHolder = new MultiDraweeHolder<>();
        hierarchies = new ArrayList<>();
        for (int i = 0; i < MAX_COUNT; i++) {
            hierarchies.add(new GenericDraweeHierarchyBuilder(getResources())
                    .build());
        }
    }

    public void setImageUris(Uri... uris) {
        multiDraweeHolder.clear();
        for (int i = 0; i < (uris.length < MAX_COUNT ? uris.length : MAX_COUNT); i++) {
            final int j = i;
            final DraweeHolder<GenericDraweeHierarchy> draweeHolder = DraweeHolder.create(hierarchies.get(i), getContext());
            DraweeController controller = Fresco.newDraweeControllerBuilder()
                    .setUri(Uri.parse("http://www.thewrap.com/wp-content/uploads/2015/11/Donald-Trump.jpg"))
                    .setOldController(draweeHolder.getController())
                    .setControllerListener(new ControllerListener<ImageInfo>() {
                        @Override
                        public void onSubmit(String id, Object callerContext) {
                            Log.e("fresco" + j, "onSubmit");
                        }

                        @Override
                        public void onFinalImageSet(String id, ImageInfo imageInfo, Animatable animatable) {
                            Log.e("fresco" + j, "onFinalImageset");
                            invalidate();
                        }

                        @Override
                        public void onIntermediateImageSet(String id, ImageInfo imageInfo) {
                            Log.e("fresco" + j, "onIntermediateImageSet");
                        }

                        @Override
                        public void onIntermediateImageFailed(String id, Throwable throwable) {
                            Log.e("fresco" + j, "onIntermediateImageFailed");
                        }

                        @Override
                        public void onFailure(String id, Throwable throwable) {
                            Log.e("fresco" + j, "onFailure");
                        }

                        @Override
                        public void onRelease(String id) {
                            Log.e("fresco" + j, "onRelease");
                        }
                    })
                    .build();
            draweeHolder.setController(controller);
            multiDraweeHolder.add(draweeHolder);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.e("fresco", "ondraw");
        for (int i = 0; i < multiDraweeHolder.size(); i++) {
            Drawable drawable = multiDraweeHolder.get(i).getTopLevelDrawable();
            drawable.setBounds(50 * i, 50 * i, 50 * (i + 1), 50 * (i + 1));
            drawable.draw(canvas);
        }
    }

    @Override
    protected boolean verifyDrawable(@NonNull Drawable who) {
        return multiDraweeHolder.verifyDrawable(who);
    }

    @Override
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        multiDraweeHolder.onDetach();
    }

    @Override
    public void onStartTemporaryDetach() {
        super.onStartTemporaryDetach();
        multiDraweeHolder.onDetach();
    }

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        multiDraweeHolder.onAttach();
    }

    @Override
    public void onFinishTemporaryDetach() {
        super.onFinishTemporaryDetach();
        multiDraweeHolder.onAttach();
    }
}

我的代码背后的逻辑是在onFinalImageSet中下载图像后立即使视图无效,以便重新绘制。但Drawables并未绘制。如果那时我将应用程序发送到后台并再次打开它,以便再次调用onStart,Drawables显示。我在这里遗漏了什么,或者是否有任何自定义的DraweView示例扩展View而不是ImageView

1 个答案:

答案 0 :(得分:1)

您需要按照此处所述设置Drawable.Callbackhttp://frescolib.org/docs/writing-custom-views.html

此外,请确保回调实际上使正确的无效。如果你使错误的一个无效,它也不会工作。