我现在使用本指南一直在努力解决这个问题:
http://frescolib.org/docs/writing-custom-views.html
但它建议编写我自己的onDraw方法,我只想设置TextView的复合drawable。我该怎么做?
答案 0 :(得分:2)
您不需要覆盖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和其他方法,如文档中所述,但您应该可以按原样粘贴该代码。