使用 Fresco 设置 TextView 的左侧可绘制对象
Setting TextView's left drawable with Fresco
使用本指南,我已经为此苦苦挣扎了一段时间:
http://frescolib.org/docs/writing-custom-views.html
但是建议自己写onDraw方法,我只想设置TextView的复合drawable。我该怎么做?
您不需要重写 onDraw,因为 TextView 已经为您完成了。你可以这样做:
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()));
}
实际设置 URI 和边界:
// build DraweeController as in Fresco docs
DraweeHolder<GenericDraweeHierarchy> holder = mMultiDraweeHolder.get(i);
holder.setController(controller);
holder.getTopLevelDrawable().setBounds(...)
将它们分配给您的 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));
您的 TextView 仍然需要覆盖 onDetachedFromWindow 和文档中解释的其他方法,但您应该能够按原样粘贴该代码。
使用本指南,我已经为此苦苦挣扎了一段时间:
http://frescolib.org/docs/writing-custom-views.html
但是建议自己写onDraw方法,我只想设置TextView的复合drawable。我该怎么做?
您不需要重写 onDraw,因为 TextView 已经为您完成了。你可以这样做:
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()));
}
实际设置 URI 和边界:
// build DraweeController as in Fresco docs
DraweeHolder<GenericDraweeHierarchy> holder = mMultiDraweeHolder.get(i);
holder.setController(controller);
holder.getTopLevelDrawable().setBounds(...)
将它们分配给您的 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));
您的 TextView 仍然需要覆盖 onDetachedFromWindow 和文档中解释的其他方法,但您应该能够按原样粘贴该代码。