myc_100 2013-03-19 09:49 采纳率: 10%
浏览 2755
已采纳

当点击按钮时,如何执行 Canvas.drawText 事件?

当点击按钮时,如何执行绘制文本 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);
}
  • 写回答

1条回答

  • Baby_Bonnie 2013-03-20 02:14
    关注

    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。
    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?