【问题标题】:add text on imageview and save it在 imageview 上添加文本并保存
【发布时间】:2013-01-05 10:56:18
【问题描述】:

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

<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();
    }
}

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

错误是:

【问题讨论】:

    标签: android android-layout android-canvas android-imageview android-image


    【解决方案1】:

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

    【讨论】:

    • 我应该在哪里写View.setDrawingCacheEnabled(true);
    • setContentView..findViewById(R.id.relativelayout).setDrawingCacheEnabled(true);
    • 它给了我错误。虽然图像保存在我指定的位置 bt 这也是无效的图像..
    • 没有必要。编辑您的问题并发布所有相关的 sn-p 代码
    【解决方案2】:

    选择图像后,为用户提供一个编辑文本框,用户可以在其中输入文本。而不是相对布局使用框架布局,以便文本出现在图像上。

    您必须将视图转换为位图

    这里是代码

     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;
     } 
    

    【讨论】:

    • 通过 FrameLayout 我可以将文本放在图像上,但是如何保存包含文本的图像??
    • 在底部提供一个完成按钮,当用户按下时,编辑文本框中的文本应显示为图像上的文本视图。最初,textview 将处于 Gone 模式。按钮单击后编辑文本框将进入消失模式。
    • 试试这个公共静态位图 loadBitmapFromView(View v) { Bitmap b = Bitmap.createBitmap( v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.ARGB_8888);画布 c = 新画布(b); v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height); v.draw(c);返回 b; }
    • 使用 v.getWidth() 代替 v.getLayoutParams().width 和类似的高度
    • 即使这不起作用试试这个...v.layout(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
    猜你喜欢
    • 2012-12-03
    • 1970-01-01
    • 2011-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-17
    相关资源
    最近更新 更多