【问题标题】:Android: Canvas.drawText when button clickAndroid:单击按钮时的Canvas.drawText
【发布时间】:2010-09-10 01:35:52
【问题描述】:

点击按钮时如何drawText?我如何setContentView(R.layout.main) 在单击按钮时查看按钮并绘制文本?我做不到,下面是我绘制文本的代码。

public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     drawView = new DrawView(this); 
     setContentView(drawView); 
}
public DrawView(Context context) { 
     super(context); 
     textpaint.setColor(Color.WHITE);
}
public void onDraw(Canvas canvas) {
     canvas.drawText("Testing", 20, 55, textpaint);
}

【问题讨论】:

    标签: android button canvas drawtext


    【解决方案1】:

    DrawView 继承自什么? 为什么不使用简单的 xml 布局,在其中放置 TextView 并在 OnClickListener 中使用 setVisible

    示例代码:

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_view);
        Button myButton = (Button) findViewById(R.id.my_button);
        final MySurfaceView surfaceView = (MySurfaceView) findViewById(R.id.mysurface);
        myButton.setOnClickListener(new OnClickListener() {
            @Override
            public boolean onClick(View v) {
                // here you should change the position and the text you want to write
                // like surfaceView.setCoordinates(x, y);
                // and surfaceView.setTextToDraw("myText");
                return true;
            }
        });
    }
    

    您当然需要创建自己的 SurfaceView。以如何做到这一点为例:http://www.droidnova.com/playing-with-graphics-in-android-part-ii,160.html

    xml:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <org.mypackage.MySurfaceView android:id="@+id/mysurface"
            android:layout_width="300dp"
            android:layout_height="300dp" />
        <Button android:id="@+id/my_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Push it real good!" />
    </LinearLayout>
    

    【讨论】:

    • 文本的位置不能用这种方法编辑,而且只会出现1次。
    • 请下次在您的问题中写下...我为您更新了答案
    猜你喜欢
    • 2021-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-14
    • 1970-01-01
    • 2013-12-18
    相关资源
    最近更新 更多