Android:点击按钮时Canvas.drawText

发布于 2024-09-18 11:24:06 字数 496 浏览 7 评论 0原文

单击按钮时如何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);
}

How to drawText when button click? How can I setContentView(R.layout.main) to see the button and draw the text when button click? I cannot make it, and below is my code for drawing text.

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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

看海 2024-09-25 11:24:06

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>

What does DrawView inherite from?
Why not using a simple xml layout where you place a TextView and use setVisible inside your OnClickListener

Sample code:

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;
        }
    });
}

You need to create your own SurfaceView of course. How to do that as an example: 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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文