屏幕部分的画布

发布于 2024-12-25 12:45:34 字数 1258 浏览 1 评论 0原文

我正在创建一个应用程序,我希望屏幕的一部分可以绘制。我使用以下代码创建画布,但这占据了整个屏幕。因此,我非常感谢您能回答我如何使画布仅出现在屏幕的一部分上,并且如果可能的话,使其居中。

package com.testing;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class testing extends Activity {
TouchView tv;
float x,y;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    tv = new TouchView(this);
    setContentView(tv);
}

class TouchView extends View implements OnTouchListener {
    Paint paint;

    public TouchView(Context context) {
        super(context);
        setOnTouchListener(this);
        paint = new Paint();
    }

    @Override
    protected void onDraw(Canvas c) {
        super.onDraw(c);
        c.drawPaint(paint);
        c.drawColor(Color.WHITE);
        c.drawText("I LIKE TO DRAW", x, y, paint);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        x = event.getX();
        y = event.getY();
        invalidate();
        return true;
    }
}

}

提前致谢。

I am creating an app where I want a part of the screen to be drawable. I use the following code to create a canvas but this takes up the entire screen. So I would greatly appreciate you could answer me how to make a canvas appear on only part of the screen and if possible be centered.

package com.testing;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class testing extends Activity {
TouchView tv;
float x,y;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    tv = new TouchView(this);
    setContentView(tv);
}

class TouchView extends View implements OnTouchListener {
    Paint paint;

    public TouchView(Context context) {
        super(context);
        setOnTouchListener(this);
        paint = new Paint();
    }

    @Override
    protected void onDraw(Canvas c) {
        super.onDraw(c);
        c.drawPaint(paint);
        c.drawColor(Color.WHITE);
        c.drawText("I LIKE TO DRAW", x, y, paint);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        x = event.getX();
        y = event.getY();
        invalidate();
        return true;
    }
}

}

Thanks in advance.

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

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

发布评论

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

评论(1

陪我终i 2025-01-01 12:45:34

我认为你应该布局并将 CanvasView 放在 LinearLayout 或其他东西中。
然后将该布局放入 Activity 内。
我是一个全新的机器人,但我认为这是一种方法:)

I think you should you Layouts and put CanvasView inside of LinearLayout or something.
Then put that layout inside of Activity.
I'm totally new android but I think this is a way to do it :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文