关于onDraw()方法不被执行的解决方法(setWillNotDraw)
来源:互联网 发布:php高并发解决方案 编辑:程序博客网 时间:2024/06/05 10:16
一.引言:
想必大家以前也遇到过这个问题:出于项目的需要,我们有时需要新建一个直接或者间接继承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); } }}
0 0
- 关于onDraw()方法不被执行的解决方法(setWillNotDraw)
- 关于onDraw()方法不被执行的解决方法(setWillNotDraw)
- 关于onDraw()方法不被执行的解决方法(setWillNotDraw(false))
- 关于onDraw()方法不被执行的解决方法(setWillNotDraw)
- 关于onDraw()方法不被执行的解决方法(setWillNotDraw)
- 处理onDraw()方法不被执行的解决方法:
- Android 自定义ViewGroup中onDraw方法不执行的解决方法
- 自定义view的时候,执行了invalidate()方法后 onDraw()不被调用的解决方法
- ViewGroup 中onDraw不被调用设置 setWillNotDraw()
- 自定义View onDraw方法不被执行
- 关于自定义View初始化时不执行onDraw方法的笔记
- Android关于LinearLayout中onDraw(Canvas canvas)方法不执行问题
- 自定义view ondraw不执行的问题
- setWillNotDraw();方法的使用
- 自定义view的onDraw()方法不被调用
- iOS 关于preferredStatusBarStyle不执行的解决方法
- CLLocationManager 关于地图 模拟器运行不提示授权的解决方法(即不执行代理方法)
- 自定义的VIEW没有执行onDraw方法
- 伙伴系统之伙伴系统概述--Linux内存管理(十五)
- 递归实现无限极分类
- 【单例设计模式】单例模式中集合枚举使用更好
- 最大的矩形问题描述
- 以后拓歌就在这里安家啦!
- 关于onDraw()方法不被执行的解决方法(setWillNotDraw)
- 41.Javascript脚本语言详解(下)
- jsp(html)页面中引入日历
- Canvas.save() Canvasre.restore()小区别
- C#怎么修改生成的EXE文件图标
- 2016.9.2
- java对象的初始化顺序
- Leetcode 212. Word Search II
- 拓歌名称由来