Setting a TextView to the left with Fresco

I have been struggling with this for a while using this guide:

http://frescolib.org/docs/writing-custom-views.html

But he suggests writing my own onDraw method, I just want to set the constituent TextView. How to do it?

+3


source to share


1 answer


You don't need to override onDraw as the TextView already does this for you. You can do something like this:

class MyTextView extends TextView {
  MultiDraweeHolder mMultiDraweeHolder;

  // called from constructors
  private void init() {
    mMultiDraweeHolder = new MultiDraweeHolder<GenericDraweeHierarchy>();
    GenericDraweeHierarchyBuilder builder = 
        new GenericDraweeHierarchyBuilder(getResources());
    for (int i = 0; i < 4; i++) {
      GenericDraweeHierarchy hierarchy = builder.reset()
        .set...
        .build();
      mMultiDraweeHolder.add(
          new DraweeHolder<GenericDraweeHierarchy>(hierarchy, getContext()));
  }

      

To actually set URIs and restrictions:

// build DraweeController as in Fresco docs
DraweeHolder<GenericDraweeHierarchy> holder = mMultiDraweeHolder.get(i);
holder.setController(controller);
holder.getTopLevelDrawable().setBounds(...)

      



To assign them to your TextView:

List<Drawable> drawables = new ArrayList<>();
for (int i = 0; i < 4; i++) {
  drawables.add(mMultiDraweeHolder.get(i).getTopLevelDrawable());
}
setCompoundDrawables(
    drawables.get(0), drawables.get(1), drawables.get(2), drawables.get(3));

      

You still need to override onDetachedFromWindow and other methods in your TextView as described in the docs, but you can paste that code as is.

+2


source







All Articles