gpt4 book ai didi

java - 在 TextView 上绘制

转载 作者:行者123 更新时间:2023-11-29 05:39:06 26 4
gpt4 key购买 nike

我有以下类代码:

package com.example.tview;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.view.*;


public class TestView extends Activity {
float x = 0;
float y = 0;
LinearLayout layout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_view);
layout=(LinearLayout)findViewById(R.id.viewd);
layout.addView(new CustomView(TestView.this));
}
public class CustomView extends View {
Bitmap mBitmap;
Paint paint;
Path path;
public CustomView(Context context) {
super(context);
mBitmap = Bitmap.createBitmap(640, 1024, Bitmap.Config.ARGB_8888);
paint = new Paint();
path= new Path();
paint.setColor(Color.BLUE);
//paint.setStyle(Style.FILL); //if I want to fill but I don't
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(5);
}

protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawPath(path,paint);
canvas.drawCircle(x, y, 25, paint);
}

public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
switch (action){
case MotionEvent.ACTION_DOWN:
path.moveTo(event.getX(), event.getY());
path.lineTo(event.getX(), event.getY());
break;
case MotionEvent.ACTION_MOVE:
x = event.getX();
y = event.getY();
path.lineTo(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
path.lineTo(event.getX(), event.getY());
break;
case MotionEvent.ACTION_CANCEL:
break;
default:
break;}
return true;
}}
}

我的 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#000000" >

<LinearLayout
android:id="@+id/viewd"
android:layout_width="250dp"
android:layout_height="304dp"
android:background="#FFFFFF"
android:orientation="vertical" >

<TextView
android:id="@+id/textView1"
android:layout_width="159dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="A"
android:textSize="125dp" />
</LinearLayout>

</LinearLayout>

它所做的是获取布局并允许我在其上绘图。

当我运行该应用程序时,屏幕上会显示以下内容,我可以在上面绘图: enter image description here

当我在 Eclipse 中查看 XML 布局时,它向我显示: enter image description here

如何让 A 显示在绘图 Canvas 后面?我试图让用户追踪字母。

最佳答案

尝试在构造函数中将 View 的背景设置为透明:

setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

正如上面其他人所说,您可能也想使用框架布局,以便此 View 位于 TextView 之上。我个人会从 xml 中取出 textview,扩展它并将 View 的功能放入其中,然后您只需要在自己的绘图方法之后调用 super.onDraw。

我很快就把它放在一起了,我是在记事本中完成的,所以它可能需要一些工作:

public class TestView extends Activity {
float x = 0;
float y = 0;
LinearLayout layout;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_view);
layout=(LinearLayout)findViewById(R.id.viewd);
layout.removeAllViews();
CustomView view = new CustomView(TestView.this);
view.setText("A");
view.setLayoutParams(new LinearLayout.LayoutParams(
250,
340));
layout.addView(view);
}

public class CustomView extends TextView {
Bitmap mBitmap;
Paint paint;
Path path;

public CustomView(Context context) {
super(context);
paint = new Paint();
path= new Path();
paint.setColor(Color.BLUE);
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(5);
}

protected void onDraw(Canvas canvas) {
canvas.drawPath(path,paint);
canvas.drawCircle(x, y, 25, paint);
super.onDraw(canvas);
}

public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
path.moveTo(event.getX(), event.getY());
path.lineTo(event.getX(), event.getY());
break;
case MotionEvent.ACTION_MOVE:
x = event.getX();
y = event.getY();
path.lineTo(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
path.lineTo(event.getX(), event.getY());
break;
case MotionEvent.ACTION_CANCEL:
break;
default:
break;
}
return true;
}
}
}

关于java - 在 TextView 上绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18344984/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com