2013-04-02 140 views
0

你好我有一个的两个图像视图中的一个与图象被从照相机选择和另一个ImageView的是仅包含“制造霍克纳尔逊”两个imageview的的图像TEXT低于合并两个图像,其是覆盖

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> 

上面是半屏代码及以上形象也半屏

现在我想牛逼o保存此图片任何机构都可以帮助我如何做到这一点?可能是CANVAS将帮助我,但我不知道该怎么做,所以请任何机构可以帮助我

+0

你需要在这里查看我的回答,HTTP://计算器。com/questions/6159186 /我怎么写文字在图片在Android和保存它 – MKJParekh

+0

亚MKJParekh首先谢谢,但MKJParekh它不会看起来像上面的图像无我想将文字图像插入到fitXY中的中心和背景图像中 –

+0

如果您将EditText的(或带有文本图像的imageview)重心设置为居中,那么结果图像将与上面的图像相同 – MKJParekh

回答

1

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

但是你必须首先理解那些ImageViews只是在屏幕上的表示,而用这两个图片创建一个Bitmap与它的屏幕显示没有多大关系。

正如在图像的内存表示中,您将拥有Drawables(根据屏幕大小进行预先缩放,因此它们的大小因设备而异,按照ldpi,mdpi,hdpi和xhdpi文件夹)和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: 

你可以(也应该)做一些数学并使用值0,0从drawBitmap()方法在画布上居中的文本。

或者,如果你有一个可绘制(如getResources.getDrawable(R.drawable.bkgr);)你可以使用draw() method画到画布上,并使用getIntrinsicHeight和getIntrinsicWidth创建使用this method

位图

快乐编码!

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" /> 

组setDrawingCacheEnabled(真);并从中创建位图。

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 
+0

我按照你的建议做了,但它只显示白色图片 –

+0

请你解释一下。白色图像是什么意思? –

+0

请确保您捕获的是根framlayout,并且这两个图像位于framlayout内 –

1

这可能帮助您,这里需要改变的功能与必字符串,并通过您的背景drawable图片, 并调整字体大小,文字颜色和对齐方式使用Canvas,并创建一个单一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; 
} 
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");