gpt4 book ai didi

java - 根据 textView 的位置在图像上定位文本

转载 作者:太空宇宙 更新时间:2023-11-04 12:45:53 26 4
gpt4 key购买 nike

我正在尝试根据 textView 的位置在图像上绘制文本,该位置将在运行时更改和确定。我有缩放问题。

请注意,保存时文本没有颜色,它只是阴影(不知道为什么请告诉我,如果你知道的话)

我想要什么:/image/fw8uv.png

这就是我得到的:/image/ys9t1.png

这里是代码:

//positioning the textView on the screen
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.leftMargin = 100;
params.topMargin = 100;
textView.setLayoutParams(params);

textView.setText("Android");

textView.setTextColor(Color.parseColor("#ffffffff"));

textView.setBackgroundColor(Color.parseColor("#00000000"));

textView.setTypeface(Typeface.MONOSPACE);

textView.setTextSize(20);

textView.setRotation(0);

textView.setAlpha(1);

textView.setShadowLayer(2, 5, 5, Color.parseColor("#c9be35f9")); // radius, dx, dy, int color

//all the attributes will be kept in a custom class which i have omitted here

下面是静态方法中的代码,它获取位图和textView

//making the bitmap mutable here
bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);

float scale = context.getResources().getDisplayMetrics().density;

Paint paint = new Paint();
paint.setTextSize(20 * scale); // i also set the textView's text size to 20
paint.setColor(Color.WHITE);//dosen't work ???? and the text is transparent didn't make any difference when i used parseColor
paint.setShadowLayer(2, 5, 5, Color.parseColor("#c9be3539")); //predefined colors didn't work
paint.setAlpha(1);
paint.setTypeface(Typeface.MONOSPACE);

RelativeLayout container = (RelativeLayout) findViewById(R.id.activity_edit_image_relative_layout_container);
// this relativeLayout only contains the image in the xml file and i add textView as it's child at run time (the textView that i set it's attributes)
float heightScale = (float) bitmap.getHeight() / container.getHeight();
float widthScale = (float) bitmap.getWidth() / container.getWidth();

float x = 100 * heightScale; //these two 100s are the position that i set on the textView
float y = 100 * widthScale;

Log.e("TextProcess", "widthScale: " + widthScale + " heightScale: " + heightScale + " scale: " + scale);
Log.e("TextProcess", "container.getWidth(): " + container.getWidth() + " container.getHeight(): " + container.getHeight());
Log.e("TextProcess", "bitmap.getWidth(): " + bitmap.getWidth() + " bitmap.getHeight(): " + bitmap.getHeight());
Log.e("TextProcess", "top: " + x + " left: " + y);

Canvas canvas = new Canvas(bitmap);
canvas.drawText("Android", x, y, paint);

//i return the bitmap and save it

以下是海绵鲍勃图像的日志文件:

E/TextProcess: widthScale: 1.8 heightScale: 1.8266385 scale: 1.5
E/TextProcess: container.getWidth(): 480 container.getHeight(): 473
E/TextProcess: bitmap.getWidth(): 864 bitmap.getHeight(): 864
E/TextProcess: top: 182.66385 left: 180.0

最佳答案

我无法实现这一点,所以我使用了 Canvas。由于 Bitmap.config,文本颜色发生了变化

关于java - 根据 textView 的位置在图像上定位文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36329802/

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