androidviewfresco

Fresco-CustomView that extends view can not display image


I want to create a customize view that extends view and have the fresco function. The image can be download , but can not be display. Please help me, the code is below:

public class QView<DH extends DraweeHierarchy> extends View {

    private DraweeHolder<DH> mDraweeHolder;
    private boolean mInitialised = false;

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

    @SuppressWarnings("unchecked")
    private void init(Context context) {
        if(!mInitialised){
            GenericDraweeHierarchy hierarchy = new GenericDraweeHierarchyBuilder(getResources()).build();
            mDraweeHolder = (DraweeHolder<DH>) DraweeHolder.create(hierarchy, context);
            mInitialised = true;
        }
    }

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

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

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        mDraweeHolder.onAttach();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        mDraweeHolder.onDetach();
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return mDraweeHolder.onTouchEvent(event) || super.onTouchEvent(event);
    }

    @Override
    protected boolean verifyDrawable(Drawable who) {
        if (who == mDraweeHolder.getTopLevelDrawable()) {
            return true;
        }
        return true;
    }

    public void setImageURI(Uri uri) {
        DraweeController controller = Fresco.newDraweeControllerBuilder()
                .setUri(uri)
                .setOldController(mDraweeHolder.getController())
                .build();
        mDraweeHolder.setController(controller);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Drawable drawable = mDraweeHolder.getHierarchy().getTopLevelDrawable();
        //Drawable drawable =  getResources().getDrawable(R.drawable.weekend_move_access);
        drawable.setBounds(0, 0, 400, 400);
        drawable.draw(canvas);
    }
}

MainActivity.java:

LinearLayout ll = (LinearLayout)findViewById(R.id.viewObj); 
    //SimpleDraweeView view = new SimpleDraweeView(this);
    QView view = new QView(this);
    LinearLayout.LayoutParams layout = new LinearLayout.LayoutParams(400, 400);
    view.setLayoutParams(layout);
    view.setImageURI(Uri.parse("http://d.hiphotos.baidu.com/image/pic/item/aa64034f78f0f7363465230c0855b319ebc41327.jpg"));
    ll.addView(view);

Solution

  • I hava done it. You must implement the methods which are updateDrawable and setBounds as them in ImageView. Because the getIntrinsicWidth() and getIntrinsicHeight() of RootDrawable are -1.