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

时间:2016-03-31 09:46:30

标签: java android xml

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

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

我想要的是什么:http://i.stack.imgur.com/fw8uv.png

以及我得到的内容:http://i.stack.imgur.com/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 

这是海绵bob图像的日志文件:

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

1 个答案:

答案 0 :(得分:0)

我无法实现这一点,所以我使用了Canvas。由于Bitmap.config

,文本颜色正在改变