如何将文本添加到图像并另存为新图像

Fra*_*sco 3 android android-layout

我正在尝试创建一个Android应用程序,为图像添加随机引用.

一般过程是这样的:

  1. 从启动应用程序时显示的自定义给定图像开始.
  2. 从该图像中,用户可以做的就是点击它并生成覆盖在图像上的新的随机"引用".
  3. 用户可以使用他选择的报价保存新创建的图像并将其设置为墙纸.

我已经到了可以在ImageView中显示图像的地步.

我的引号列表存储在我的strings.xml文件中.

AmI*_*ord 6

我在应用程序中做了类似的事情.使用画布.

我编辑了一段代码,实际上在背景上添加了几个其他图像.

代码肉:

private static Bitmap getPoster(...) {
    Bitmap background = BitmapFactory.decodeResource(res, background_id)
        .copy(Bitmap.Config.ARGB_8888, true);
    Canvas canvas = new Canvas(background);
    Typeface font = Typeface.createFromAsset(res.getAssets(), FONT_PATH);
    font = Typeface.create(font, Typeface.BOLD);
    Paint paint = new Paint();
    paint.setTypeface(font);
    paint.setAntiAlias(true);
    paint.setColor(Color.WHITE);
    paint.setStyle(Style.FILL);
    paint.setShadowLayer(2.0f, 1.0f, 1.0f, Color.BLACK);
    float fontSize = getFontSize(background.getWidth(), THE_QUOTE, paint); //You'll have to define a way to find a size that fits, or just use a constant size.

    paint.setTextSize(fontSize);
    canvas.drawText(THE_QUOTE, (background.getWidth() - paint.measureText(THE_QUOTE)) / 2,
        background.getHeight() - FILLER_HEIGHT, paint); //You might want to do something different. In my case every image has a filler in the bottom which is 50px. 
    return background;
}
Run Code Online (Sandbox Code Playgroud)

将您自己的版本放在一个类中,并为其提供图像ID和其他任何内容.它返回一个位图,供您随意使用(在imageview中显示,让用户保存并设置为wallpape).