在android中合并两个图像。一个是从图片中拍摄的,另一个是从可提取资源中拍摄的。这个位置不合适。
我有一个Imageview,假设是image1。现在有一个名为image2的imageview。图像1>图像2。我想将image2拖到image1上,并使用canvas保存新图像。已合并图像,但未在适当位置合并图像2。任何帮助都将不胜感激。 这是我的合并代码在android中合并两个图像。一个是从图片中拍摄的,另一个是从可提取资源中拍摄的。这个位置不合适。,android,merge,Android,Merge,我有一个Imageview,假设是image1。现在有一个名为image2的imageview。图像1>图像2。我想将image2拖到image1上,并使用canvas保存新图像。已合并图像,但未在适当位置合并图像2。任何帮助都将不胜感激。 这是我的合并代码 int maxWidth = (bitmap1.getWidth() > resizedbitmap2.getWidth() ? bitmap1.getWidth() : resizedbitmap2.getWidth());
int maxWidth = (bitmap1.getWidth() > resizedbitmap2.getWidth() ? bitmap1.getWidth() : resizedbitmap2.getWidth());
int maxHeight = (bitmap1.getHeight() > resizedbitmap2.getHeight() ? bitmap1.getHeight() : resizedbitmap2.getHeight());
Bitmap bmOverlay = Bitmap.createBitmap(maxWidth, maxHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmOverlay);
canvas.drawBitmap(bitmap1, 0f, 0f, null);
canvas.drawBitmap(resizedbitmap2, x, y, null);
这就是我拖动图像2的方式
public boolean onTouch(View arg0, MotionEvent arg1) {
switch (arg1.getAction())
{
case MotionEvent.ACTION_DOWN:
// attemptClaimDrag();
moving=true;
break;
case MotionEvent.ACTION_MOVE:
if(moving){
x=arg1.getRawX()-ima2.getWidth()/2;
y=arg1.getRawY()-ima2.getHeight()*3/2;
ima2.setX(x);
ima2.setY(y);
}
break;
case MotionEvent.ACTION_UP:
moving=false;
break;
}
return true;
}
定义了这两个
float x,y=0.0f
试试这个:
首先,使用RelativeLayout
重叠两个图像。正确地显示它
其次,将此RelativeLayout
转换为位图
layout.xml中的
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/layout">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/image1"/>
<!-- Change the position of the image. Now, its in center -->
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/image2"
android:layout_centerInParent="true"
/>
</RelativeLayout>
那么,你的意思是我应该把image1的imageview保存在一个相对的布局中吗?我不想这样硬编码。用户将在image1上的任何位置拖动image2。但在保存合并图像时。我想让人知道它到底掉在哪里。
// Set enable drawing cache first.
layout.setDrawingCacheEnabled(true);
// Get Bitmap from RelativeLayout
Bitmap bitmap = layout.getDrawingCache();