帆布不自定义视图中绘制 [英] Canvas does not draw in Custom View

查看:188
本文介绍了帆布不自定义视图中绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个自定义视图CircleView是这样的:

I created a Custom View CircleView like this:

public class CircleView extends LinearLayout {

    Paint paint1;
    public CircleView(Context context) {
        super(context);
        init();
    }   
    public CircleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
    public void init() {
        paint1 = new Paint();
        paint1.setColor(Color.RED); 
    }       
    protected void onDraw(Canvas canvas) {
        //super.onDraw(canvas);         
        canvas.drawCircle(50, 50, 25, paint1);
        this.draw(canvas);  
    }
}

然后我把它在我的活动布局根<&RelativeLayout的GT;

  <com.turkidroid.test.CircleView
      android:id="@+id/circle_view"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent" 
      android:layout_centerInParent="true"  />  

然而,什么也没有拉!

However, nothing was drawn!


  • 是我实现自定义视图吧?

  • 或者是我是如何使用自定义视图?

一些信息:


  • 双方CircleView和MyActivity都在同一个包: com.turkidroid.test

  • 的onDraw()方法,我试过包括 super.onDraw()和评论吧。

  • 我知道我可以画一个圆,更简单的方法,但我CircleView的将会的含有比画圆了。我要使它成为一个自定义视图。

  • Both CircleView and MyActivity are in the same package: com.turkidroid.test.
  • In onDraw() method, I tried including super.onDraw() and commenting it.
  • I know I can draw a circle with much simpler approaches but my CircleView will contain more than drawing a circle. I need to make it a Custom View.

推荐答案

您的onDraw方法不会被调用,你需要为了得到的onDraw居然叫调用setWillNotDraw(假)上的自定义视图的构造。

Your onDraw method is never called, you need to call setWillNotDraw(false) on the constructor of your Custom View in order to get onDraw actually called.

由于在Android SDK中指出:

As stated on Android SDK:

如果这个观点没有做对自己的任何绘图,设置该标志以允许
  进一步优化。默认情况下,这个标志没有被设置上查看,但
  可以在某些视图子类,如设置的ViewGroup。通常情况下,如果
  你重写的onDraw(android.graphics.Canvas),你应该清除此
  标志。

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(android.graphics.Canvas) you should clear this flag.

这篇关于帆布不自定义视图中绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆