问题描述
目前我正在制作照片编辑器应用程序。我有活动,您可以在其中添加文本到图像。我需要在位图中保存带有文本的图像。我已经尝试过保存绘图缓存,但问题是它会以低质量保存图像。原始图像4032 * 3024当我保存它使用imageview的大小1517 * 1137.我已经尝试将位图缩放到原始大小,但质量太差。现在我正在尝试在画布上绘制原始位图,然后在与View上相同的位置上绘制文本。
float x = editText.getLeft()*(scale);
但由于某种原因,文字不在同一个地方。我尝试使用密度来获取坐标,但它也不起作用。
float y = editText.getTop()*(scale);
canvas.drawText(editText.getText()。toString(),x,y,tp);
Currently i'm making a photo editor app. I have activity where you can add a text to the image. I need to save image with text in bitmap. I've already tried to save drawing cache, but the problem is that it saves image in bad quality. Original image 4032*3024 when i save it uses size of imageview which are 1517 * 1137. I've tried to scale bitmap up to original size but quality is too bad. Now i'm trying to draw original bitmap on canvas then to draw text on same position as it was on View.float x = editText.getLeft() * (scale);float y = editText.getTop() * (scale);canvas.drawText(editText.getText().toString(), x, y, tp);
But for some reason text is not on the same place. I've tried using density to get coordinates but it also don't works.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/add_text_color_picker_relative_layout"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true">
<RelativeLayout
android:id="@+id/edit_photo_add_text_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<ImageView
android:id="@+id/edit_photo_add_text_main_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:adjustViewBounds="true"
android:scaleType="fitCenter" />
<EditText
android:id="@+id/edit_photo_add_text_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/rect_edittext"
android:hint="Поле для текста"
android:inputType="textNoSuggestions"
android:padding="4dp"
android:textSize="14dp" />
</RelativeLayout>
</RelativeLayout>
这里是java代码:
public Bitmap createImage(View view, int imageViewId, int textViewId, Context context){
ImageView imageView = (ImageView) view.findViewById(imageViewId);
Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
float bitmapRatio = (bitmap.getWidth()) / bitmap.getHeight();
float imageViewRatio = (imageView.getWidth()) / imageView.getHeight();
float scaleW = bitmap.getWidth() / imageView.getWidth();
float scaleH = bitmap.getHeight() / imageView.getHeight();
android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
bitmap = bitmap.copy(bitmapConfig, true);
EditText editText = (EditText) view.findViewById(textViewId);
float density = context.getResources().getDisplayMetrics().density;
Canvas canvas = new Canvas(bitmap);
canvas.setBitmap(bitmap);
TextPaint tp = new TextPaint(TextPaint.ANTI_ALIAS_FLAG);
tp.setColor(Color.WHITE);
tp.setTextSize(editText.getTextSize()*density);
Resources resources = context.getResources();
float scale = resources.getDisplayMetrics().density;
int[] xy = {0, 0};
editText.getLocationOnScreen(xy);
float x = xy[0] * (scaleW);
float y = xy[1] * (scaleH);
canvas.drawText(editText.getText().toString(), x, y, tp);
canvas.save();
canvas.restore();
return bitmap; }
推荐答案
最后我找到了解决方案。
当我计算屏幕比例时
Finally I've found the solution.When i'm calculating screen scale
float scaleW = bitmap.getWidth()/ imageView.getWidth();
我正在划分为整数并收到错误的值;
而不是它应该有
I'm dividing to integers and receiving wrong value;Instead of it there should be
float scaleW =(float)bitmap.getWidth()/(float)imageView.getWidth( );
然后你可以在画布的正确位置画画。
and then you can draw at right position of canvas.
这篇关于保存Imageview使用textview到位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!