组合两个叠加的图像

时间:2013-04-02 10:36:02

标签: java android image overlay android-canvas

您好我有一个两个图像视图,一个是从相机中选择的图片,另一个图像视图只有TEXT,如“Made Hawk Nelson”,两个imageview的图像在下面

enter image description here

xml代码在

之下
  <RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="7"
    android:scaleType="fitXY" >

    <ImageView
        android:id="@+id/imgSelectedPhoto"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="fitXY" />

    <ImageView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/txt_made_hawk_nelson"
        android:layout_centerInParent="true" />

</RelativeLayout>

以上是半屏代码,上面的图像也是半屏

现在我要保存这张照片可以任何身体帮助我,我该怎么做?也许CANVAS会帮助我,但我不知道怎么做,所以请任何人帮助我

4 个答案:

答案 0 :(得分:1)

你有正确的想法。画布将是最简单的方法。

但是你必须首先理解那些ImageViews只是屏幕上的表示,并且创建一个位图,其中这两个图像是叠加的,与它的屏幕表示几乎没有关系。

在图像的内存表示中,您将拥有Drawables(根据屏幕尺寸进行预缩放,因此它们的大小因设备而异,根据ldpi,mdpi,hdpi和xhdpi文件夹)和Bitmaps,绝对代表。

我刚才说的所有内容都会根据您的申请而有所不同,我不会给您提供确切的解决方案,但会向您解释所有概念:

就像示例一样,假设你有背景和文本作为Bitmaps对象,所以你的代码将是:

// Init our overlay bitmap
Bitmap bmp = backgroundBitmap.copy(Bitmap.Config.ARGB_8888, true);
// Init the canvas
Canvas canvas = new Canvas(bmp);
// Draw the text on top of the canvas
canvas.drawBitmap(textBitmap, 0, 0, null);

// now bmp have the two overlayed:

你可以(而且应该)做一些数学运算,并使用drawBitmap()方法中的值0,0将文本置于画布中心。

或者,如果你有一个drawable(例如getResources.getDrawable(R.drawable.bkgr);)你可以使用draw() method绘制到画布并使用getIntrinsicHeight和getIntrinsicWidth来创建位图{ {3}}

快乐的编码!

答案 1 :(得分:1)

将布局更改为

<Framelayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="7" >

<ImageView
    android:id="@+id/imgSelectedPhoto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scaleType="fitXY" />

<ImageView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/txt_made_hawk_nelson"
    android:layout_centerInParent="true" />

set setDrawingCacheEnabled(true);并从中创建位图。

Bitmap bitmap;
// frmCaptureThis is the root framelayout (this contains your imageviews)
View v1 = frmCaptureThis; 
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
saveImgToSDcard(bitmap); // function to saves the image to sd card

答案 2 :(得分:1)

这可能会帮助你,这里需要用必需的字符串更改函数,并传递你的背景drawable图像, 并使用Canvas调整字体大小,TextColor和对齐方式并创建一个单Bitmap并进行检查。

private Bitmap getThumb(String strangle, String strnote, int width, int height) {

  //Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.transperet_bg);
  Bitmap bmOverlay = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

  Canvas canvas = new Canvas(bmOverlay);
  Paint paint = new Paint();

  paint.setColor(Color.WHITE);
  paint.setTextSize(20);
  paint.setFlags(Paint.ANTI_ALIAS_FLAG);
  // if the background image is defined in main.xml, omit this line
  canvas.drawARGB(140, 0, 0, 0);
  //canvas.drawBitmap(mBitmap, 0, 0, null);
  // draw the text and the point
  paint.setTextAlign(Paint.Align.LEFT);

  canvas.drawText("Head Angel = " + strangle, 10, 20, paint);

  strnote = edtnote.getText().toString();
  if (TextUtils.isEmpty(strnote)) {
   strnote = "Note";
  }
  canvas.drawText(strnote, 10, 50, paint);
  paint.setTextAlign(Paint.Align.RIGHT);

  canvas.drawText("www.moovemtb.com", width-60, 50, paint);
  canvas.drawText("Head Angel App", width-60, 20, paint);

  canvas.drawPoint(30.0f, height/2, paint);
  return bmOverlay;
 }

答案 3 :(得分:1)

请试试这个,效果很好

BufferedImage img1 = ImageIO.read(new File(pathtoImage)); //first image
BufferedImage img2 = ImageIO.read(new File(pathtoOverlay)); //overlay text image
BufferedImage combinedImage = new BufferedImage(img1.getWidth(),img1.getHeight(),BufferedImage.TYPE_INT_RGB);

    Graphics g = combinedImage.getGraphics();

    g.drawImage(img1, 0, 0, null);
    g.drawImage(img2, 0, 0, null);
    ImageIO.write(combinedImage,"JPG",new File(pathToBeSaved,"combined.jpg");