gpt4 book ai didi

Android 文本周围的黑色轮廓

转载 作者:行者123 更新时间:2023-11-29 01:29:55 25 4
gpt4 key购买 nike

我正在尝试找到一种简单的解决方案来为文本添加轮廓,无论背景是什么,它都能脱颖而出。

此问题之前被标记为重复,但链接的答案无效。因此,这仍然是一个持续的问题。链接的答案如下:

@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
Paint strokePaint = new Paint();
strokePaint.setARGB(255, 0, 0, 0);
strokePaint.setTextAlign(Paint.Align.CENTER);
strokePaint.setTextSize(16);
strokePaint.setTypeface(Typeface.DEFAULT_BOLD);
strokePaint.setStyle(Paint.Style.STROKE);
strokePaint.setStrokeWidth(2);

Paint textPaint = new Paint();
textPaint.setARGB(255, 255, 255, 255);
textPaint.setTextAlign(Paint.Align.CENTER);
textPaint.setTextSize(16);
textPaint.setTypeface(Typeface.DEFAULT_BOLD);

Path path = new Path();
String text = "Some Text";
textPaint.getTextPath(text, 0, text.length(), 0, 100, path);
canvas.drawPath(path, strokePaint);
canvas.drawPath(path, textPaint);

super.draw(canvas, mapView, shadow);
}

行:

super.draw(canvas, mapView, shadow);

被编译器识别为错误。 .draw 部分是红色的,因为它“无法解析方法”。由于此解决方案不是一个有效的解决方案,因此这个问题不是重复的。

我还尝试了另一个创建类的示例,如下所示:(编辑如下,编译器强制我添加构造函数,如图所示)。

 public class OutlineTextView extends TextView {

public OutlineTextView(Context context) { //added this section
super(context);
}

@Override
public void draw(Canvas canvas) {
for (int i = 0; i < 5; i++) {
super.draw(canvas);
}
}
}

然后 xml 文件尝试使用此类,如下所示:

<OutlineTextView
android:shadowColor="#000"
android:shadowRadius="3.0"
android:id="@+id/score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="20"
android:textSize="160dp"
android:typeface="sans"
android:textColor="#ffffe0"
android:layout_marginTop="60dp"
android:layout_centerHorizontal="true" />

这会在尝试启动此 Activity 时引发错误:

Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.OutlineTextView" on path: DexPathList[[zip file "/data/app/rule02.touchpool-2/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)

关于如何让它工作的任何进一步想法,任何人?

最佳答案

您需要为自定义 View 使用完整的包名称。所以你当前的布局应该变成,

<com.example.OutlineTextView
android:shadowColor="#000"
android:shadowRadius="3.0"
android:id="@+id/score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="20"
android:textSize="160dp"
android:typeface="sans"
android:textColor="#ffffe0"
android:layout_marginTop="60dp"
android:layout_centerHorizontal="true" />

关于Android 文本周围的黑色轮廓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31768412/

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