请问TextView中的OnDraw支持图形绘制? [英] Does TextView's onDraw supports graphical drawing?

查看:144
本文介绍了请问TextView中的OnDraw支持图形绘制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图继承一个TextView并添加一个位图给它。这不可行。 使用相同的子类,我尝试用画线。它仍然无法正常工作。 我试图绘制文本。它的工作原理。

I tried to subclass a textView and add a bitmap to it. It does not work. Using the same subclass, i experiment by drawing a line. It still does not work. I tried to draw text. It works.

当我改变了基类的TextView查看。一切正常。

When I changed the base class from textView to View. Everything works.

我想知道如果TextView中不支持任何其他绘图除了文字? 可有人告诉工作code。关于如何绘制使用视图中的drawLine()中的OnDraw()与TextView中的基类线的例子吗?

I'm wondering if textView does not support drawing of anything else except text? Can someone show an example of working code on how to draw a line using drawLine() in onDraw() of a view with a base class of textView?

package com.main.test;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

public class Display extends TextView{

    Paint p;

    public Display (Context context) {
       super(context);
       init();
    }

    public Display (Context context, AttributeSet attrs) {
       super(context, attrs);
       init();
    }

    private void init()
    {
       p = new Paint();
       p.setColor(Color.WHITE);
    }

    @Override
    protected void onDraw(Canvas canvas) {
   // TODO Auto-generated method stub
       super.onDraw(canvas);
       canvas.drawLine(0, 0, this.getMeasuredWidth(), this.getMeasuredHeight(), p);
    }
}

主要活动的code     包com.main.test;

main activity's code package com.main.test;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class main extends Activity {
    Display input;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initcomponents();
    }


    private void initcomponents()
    {
        input = (Display)findViewById(R.id.rateInput);
    }
}

如果我从TextView的更改进行查看。它的工作原理。

If I change it from TextView to View. It works.

package com.main.test;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

public class Display extends View{

    Paint p;

    public Display (Context context) {
       super(context);
       init();
    }

    public Display (Context context, AttributeSet attrs) {
       super(context, attrs);
       init();
    }

    private void init()
    {
       p = new Paint();
       p.setColor(Color.WHITE);
    }

    @Override
    protected void onDraw(Canvas canvas) {
   // TODO Auto-generated method stub
       super.onDraw(canvas);
       canvas.drawLine(0, 0, this.getMeasuredWidth(), this.getMeasuredHeight(), p);
    }
}

主要活动的code     包com.main.test;

main activity's code package com.main.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;

public class main extends Activity {
    View input;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initcomponents();
    }


    private void initcomponents()
    {
        input = (View)findViewById(R.id.rateInput);
    }
}

确定该系统不允许我发布的PIC现在。所以我不能告诉你从第2 code的结果。

ok this system don't allow me to post pic for now. So i cannot show you the results from the 2nd code.

推荐答案

确定我发现为什么DrawLine的已经不工作的原因。

ok I found the reason why drawline does not work already.

当我删除此行清单中的XML。线可以得出

When i remove this line in the manifest XML. the line can be drawn.

安卓inputType =numberDecimal

android:inputType="numberDecimal"

但为什么...?

这篇关于请问TextView中的OnDraw支持图形绘制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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