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

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你应该布局并将 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 :)