我试图以这种方式写文字,但它不起作用,我不明白我错在哪里。
mImageView.buildDrawingCache();
Bitmap bmap = mImageView.getDrawingCache();
Canvas c = new Canvas (bmap);
Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setStyle(Style.FILL);
paint.setTextSize(20);
c.drawText("Some Text", 0, 25, paint);
我试了几次但是显示器无法显示任何文字。我必须确保此图像随后与书面文本一起保存。感谢
答案 0 :(得分:17)
以下是如何在ImageView上绘制文本的简单示例:
<强> MainActivity.java 强>
package com.exmple.imagetest;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.os.Bundle;
import android.widget.ImageView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView mImageView = (ImageView)findViewById(R.id.imageView1);
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.image);
Config config = bm.getConfig();
int width = bm.getWidth();
int height = bm.getHeight();
Bitmap newImage = Bitmap.createBitmap(width, height, config);
Canvas c = new Canvas(newImage);
c.drawBitmap(bm, 0, 0, null);
Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setStyle(Style.FILL);
paint.setTextSize(20);
c.drawText("Some Text", 0, 25, paint);
mImageView.setImageBitmap(newImage);
}
}
<强> activity_main.xml中强>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/image"
android:contentDescription="@string/image" />
</RelativeLayout>
答案 1 :(得分:0)
你可以尝试,我使用了1个简单的技巧。 我创建了一个包含ImageView和TextView的外部FrameLayout 像下面的代码:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="center"
android:src="@drawable/image" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dip"
android:layout_gravity="center_horizontal|bottom"
android:background="#9F000000"
android:textColor="#ffffffff"
android:text="+10" />