自定义Drawable draw()方法未被调用(Android)

时间:2013-11-24 15:13:10

标签: java android

对于我的项目,我正在从网上下载图片,所以我为它实现了一个简单的类:

@SuppressWarnings("deprecation")
public class DynamicDrawable extends BitmapDrawable{
    private  Drawable drawable;

    @Override
    public void draw(Canvas canvas) {
        // override the draw to facilitate refresh function later
        if(drawable != null) {
            drawable.draw(canvas);
        }
        Log.d("dnull", String.valueOf(drawable == null));
    }
    protected void setDrawable(Drawable drawable){
        this.drawable = drawable;
    }
}

有一个处理程序可以获取并解析图像,将其异步添加到类中并使视图无效,我已经检查过并且工作正常。 drawable变量不为null。 然后将其添加到ImageView。但是,从不调用draw()方法。即使是第一次添加也没有。以下是图像进入视图的代码:

@Override
public View getView(int position, View convertView, ViewGroup parent){
View view = convertView;

if(view == null){
    view = inflater.inflate(R.layout.view_concerteposter, parent,false);
}

ConcertPoster poster = posters.get(position);

ImageView iconView = (ImageView)view.findViewById(R.id.ticketBuy_icon);
TextView titleView = (TextView)view.findViewById(R.id.ticketBuy_name);
TextView dateView = (TextView)view.findViewById(R.id.ticketBuy_date);

iconView.setImageDrawable(poster.getImage());
System.out.println(poster.getImage());
titleView.setText(poster.getTitle());
dateView.setText(poster.getDate());

return view;
}

是的,我检查过这些物品,它们都是正确的,并且里面有正确的抽屉。

任何帮助都将不胜感激。

3 个答案:

答案 0 :(得分:0)

在convertView的onDraw()方法中,您应该手动调用Drawable的draw()方法。

protected void onDraw(Canvas canvas) {
mDrawable.draw(canvas);
}

有关详细信息,请参阅here

答案 1 :(得分:0)

帖子Canvas does not draw in Custom View上描述的解决方案解决了我的问题。

  

您的onDraw方法永远不会被调用,您需要在自定义视图的构造函数上调用setWillNotDraw(false)才能获得实际调用的onDraw

答案 2 :(得分:0)

覆盖getIntrinsicHeight() / getIntrinsicWidth()并返回非零值将解决此问题。

请参见ImageView#setImageDrawable(Drawable)

public void setImageDrawable(@Nullable Drawable drawable) {
        if (mDrawable != drawable) {
           //...

            updateDrawable(drawable);

            if (oldWidth != mDrawableWidth || oldHeight != mDrawableHeight) {
                requestLayout();
            }
            invalidate();
        }
    }

然后参见ImageView#updateDrawable(Drawable)

private void updateDrawable(Drawable d) {
        //...
        if (d != null) {
            //...
            mDrawableWidth = d.getIntrinsicWidth();
            mDrawableHeight = d.getIntrinsicHeight();
            //...

            configureBounds();
        } else {
            mDrawableWidth = mDrawableHeight = -1;
        }
    }

mDrawableWidthmDrawableHeight中设置了d.getIntrinsicWidth()d.getIntrinsicHeight()

请参见ImageView#onDraw(Canvas)

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (mDrawable == null) {
            return; // couldn't resolve the URI
        }

        if (mDrawableWidth == 0 || mDrawableHeight == 0) {
            return;     // nothing to draw (empty bounds)
        }
    //...
    }

您可以查看mDrawableWidth == 0还是mDrawableHeight == 0,ImageView不会调用Drawable.draw(Canvas)