0

我正在尝试创建一个向图像添加文本的应用程序。在此我的图像来自画廊,文本由用户添加。为此,我创建了我的布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativelayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<ImageView
    android:id="@+id/myImageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

<TextView
    android:id="@+id/myImageViewText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/myImageView"
    android:layout_alignLeft="@+id/myImageView"
    android:layout_alignRight="@+id/myImageView"
    android:layout_alignTop="@+id/myImageView"
    android:layout_margin="1dp"
    android:gravity="center"
    android:text="Hello"
    android:textColor="#000000" />

活动如下:

public class AddText extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.add_text);

    findViewById(R.id.relativelayout).setDrawingCacheEnabled(true);
    Bitmap finalImg = findViewById(R.id.relativelayout).getDrawingCache();

    String save_location = Environment.getExternalStorageDirectory()
            .getAbsolutePath() + "/texted";
    File dir = new File(save_location);
    if (!dir.exists())
        dir.mkdirs();
    File f = new File(dir, "tmp.jpg");
    FileOutputStream out;
    try {
        out = new FileOutputStream(f);
        finalImg.compress(Bitmap.CompressFormat.PNG, 90, out);

        out.flush();
        out.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

现在我想用文本保存整个图像。用文字保存图像的方法是什么?

错误是:

在此处输入图像描述

4

2 回答 2

0

您的布局应该将启用的绘图缓存设置为 true。您可以通过 setter 设置它View.setDrawingCacheEnabled(true);然后您应该能够View使用View.getDrawingCache(). View该方法返回一个表示' 内容的 Bitmap 实例。您可以使用Bitmap.compress(..), 来存储它。

于 2013-01-05T11:01:27.293 回答
-1

After the image selection, provide the user an edit text box where he enters the text. Instead of relative layout use Frame layout so that text will appear over the image.

You have to convert the view into a Bitmap

here is the code

 public static Bitmap loadBitmapFromView(View v)
 { 
Bitmap b = Bitmap.createBitmap(v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.ARGB_8888); 
Canvas c = new Canvas(b); v.layout(0, 0, v.getLayoutParams().width,v.getLayoutParams().height); v.draw(c); 
return b;
 } 
于 2013-01-05T11:54:48.653 回答