关于onDraw()方法不被执行的解决方法(setWillNotDraw)

一.引言:

想必大家以前也遇到过这个问题:出于项目的需要,我们有时需要新建一个直接或者间接继承View的类,以便复写View提供的onDraw()方法,但有时我们反而得不到我们想要的结果,今天就说一下onDraw()方法不被执行的解决方法。你可能也在onDraw()方法里面设置了断点或log,却发现程序并没有执行onDraw()方法,那么你需要在你直接或者间接继承View的类的构造函数中加入下面的语句:

        setWillNotDraw(false);


二.解释:

那么加这条语句的作用是什么?先看API:

  If this view doesn't do any drawing on its own, set this flag to allow further optimizations. By default, this flag is not set on View, but could be set on some View subclasses such as ViewGroup. Typically, if you override onDraw(Canvas) you should clear this flag.

本人外语基础不是很好,简要翻译一下,如果翻译的不好,不要扔砖啊,重复一句我的语言:要想象,没有了想象,世界会是什么样。嘿嘿:

如果在当前的view上面不做任何的绘制操作,需要设置这个标记以便将来的更好的需要,默认的,这个标记在View里是不设定的。但是像View的一些子类如ViewGroup是可以设定的,典型的,你如果复写了onDraw(Canvas)方法,你需要清除此标记。

那么正好,我们所实现的就是View的子类:LinearLayout,当然你也可以继承其他的子类如:

AbsoluteLayout,AdapterView,FrameLayout,LinearLayout,RelativeLayout,SlidingDrawer,子类就不说了,你可以自己去查文档。

这条语句要放在继承类的构造函数中,如:

public classBackgroundLayout extendsLinearLayout {  

        publicBackgroundLayout(Context context, intposition) {  

                super(context);  

                // TODOAuto-generated constructor stub  

                setWillNotDraw(false);  

        }  

        @Override  

        protected voidonDraw(Canvas canvas) {  

                // TODOAuto-generated method stub  

                super.onDraw(canvas);  

                }  

        }  

}  


三,扩展看法:

eoeandroid上面Little关于这条语句的看法是:

   设置view是否更改,如果开发者用自定义的view,重写ondraw()应该将调用此方法设置为false,这样程序会调用自定义的布局。

在此引用一下。

   其实从这条语句的字面意思上可以看出:setWillNotDraw(false);就是设置将不绘画吗?你重写了onDraw()当然是要进行绘画了,所以应将此语句参数置为false.


四,ForegroundImageView:
package com.example.android.unsplash.ui;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.ViewOutlineProvider;
import android.widget.ImageView;

import com.example.android.unsplash.R;


public class ForegroundImageView extends ImageView {

    private Drawable foreground;

    public ForegroundImageView(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ForegroundView);

        final Drawable d = a.getDrawable(R.styleable.ForegroundView_android_foreground);
        if (d != null) {
            setForeground(d);
        }
        a.recycle();
        setOutlineProvider(ViewOutlineProvider.BOUNDS);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if (foreground != null) {
            foreground.setBounds(0, 0, w, h);
        }
    }

    @Override
    public boolean hasOverlappingRendering() {
        return false;
    }

    @Override
    protected boolean verifyDrawable(Drawable who) {
        return super.verifyDrawable(who) || (who == foreground);
    }

    @Override
    public void jumpDrawablesToCurrentState() {
        super.jumpDrawablesToCurrentState();
        if (foreground != null) foreground.jumpToCurrentState();
    }

    @Override
    protected void drawableStateChanged() {
        super.drawableStateChanged();
        if (foreground != null && foreground.isStateful()) {
            foreground.setState(getDrawableState());
        }
    }

    /**
     * Returns the drawable used as the foreground of this view. The
     * foreground drawable, if non-null, is always drawn on top of the children.
     *
     * @return A Drawable or null if no foreground was set.
     */
    public Drawable getForeground() {
        return foreground;
    }

    /**
     * Supply a Drawable that is to be rendered on top of the contents of this ImageView
     *
     * @param drawable The Drawable to be drawn on top of the ImageView
     */
    public void setForeground(Drawable drawable) {
        if (foreground != drawable) {
            if (foreground != null) {
                foreground.setCallback(null);
                unscheduleDrawable(foreground);
            }

            foreground = drawable;

            if (foreground != null) {
                foreground.setBounds(0, 0, getWidth(), getHeight());
                setWillNotDraw(false);
                foreground.setCallback(this);
                if (foreground.isStateful()) {
                    foreground.setState(getDrawableState());
                }
            } else {
                setWillNotDraw(true);
            }
            invalidate();
        }
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        if (foreground != null) {
            foreground.draw(canvas);
        }
    }

    @Override
    public void drawableHotspotChanged(float x, float y) {
        super.drawableHotspotChanged(x, y);
        if (foreground != null) {
            foreground.setHotspot(x, y);
        }
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值