合并两个图像并获取布局android的截图

时间:2016-07-08 10:31:31

标签: android

我在activity_main中有一个图像视图,在content_main中有其他图像视图,我想合并图像视图并截取它的截图,但它没有显示content_main的图像。以下是它的图片。

我想用里面的照片拍摄的图像

Image which I want to capture with the photo inside it

正在捕获的图像

Image which is being captured

activity_main内的代码

  <android.support.design.widget.CoordinatorLayout     
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      android:id="@+id/cl_template_one"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:fitsSystemWindows="true"
      tools:context=".activity.template.TemplateOne">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_template_one" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="2"
    android:layout_marginTop="?attr/actionBarSize"
    android:orientation="vertical">
    <RelativeLayout
        android:id="@+id/rl"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="2">
        <FrameLayout
            android:id="@+id/frame_memecontent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <RelativeLayout
                android:id="@+id/ec_rltabselected"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingTop="5dp"
                android:paddingRight="5dp"
                android:paddingLeft="5dp" >

                <AbsoluteLayout
                    android:id="@+id/relative1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >

                    <ImageView
                        android:id="@+id/ivCardView"
                        android:layout_width="match_parent"
                        android:background="@drawable/template_one_original"
                        android:paddingLeft="1dp"
                        android:paddingRight="1dp"
                        android:layout_height="match_parent" />


                </AbsoluteLayout>
            </RelativeLayout>

            <LinearLayout
                android:id="@+id/llBottomLayout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:layout_margin="5dp"
                android:orientation="horizontal"
                android:padding="5dp"
                android:weightSum="0" >
            </LinearLayout>
        </FrameLayout>

    </RelativeLayout>

</android.support.design.widget.CoordinatorLayout>

代码内容content_main

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <com.pankaj.birthdayinvitationmaker.others.TouchImageView
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:src="@drawable/ic_black_add_image"
         android:layout_weight="0.95"
         android:adjustViewBounds="false"
         android:gravity="center"
         android:layout_gravity="center_horizontal"
         android:layout_marginLeft="100dp"
         android:layout_marginRight="80dp"
         android:id="@+id/addImageButton" />
</RelativeLayout>

以下是我完成图像捕捉的代码。

View vScreenShot = findViewById(R.id.frame_memecontent);

FrameLayout z = (FrameLayout) findViewById(R.id.frame_memecontent);
if(z!=null) {
     int totalHeight = z.getChildAt(0).getHeight();
     int totalWidth = z.getChildAt(0).getWidth();
     vScreenShot.layout(0, 0, totalWidth, totalHeight);
}
vScreenShot.buildDrawingCache(true);

//create bitmap and save drawing cache
Bitmap bitmapSetDrawingChache = Bitmap.createBitmap(vScreenShot.getDrawingCache());

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

bitmapSetDrawingChache.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);

// saving in sdcard
File file = new File(Environment.getExternalStorageDirectory().toString() + File.separator + "Birthday Invitation Maker" + File.separator);
if(!file.exists())
    file.mkdirs();

String imageName = "Image-" + DateFormat.getDateTimeInstance().format(new Date()) + ".jpg";

File f = new File(file,imageName);
try {

    FileOutputStream fileOutputStream = new FileOutputStream(f);
    fileOutputStream.write(byteArrayOutputStream.toByteArray());
    fileOutputStream.close();
    Toast.makeText(TemplateOne.this, "Image Saved",Toast.LENGTH_SHORT).show();
} catch (IOException e) {
    e.printStackTrace();
}

欢迎任何建议/意见。提前谢谢。

1 个答案:

答案 0 :(得分:1)

您正在创建仅一个布局的图像,即vScreenShot

Bitmap bitmapSetDrawingChache = Bitmap.createBitmap(vScreenShot.getDrawingCache());

vScreenShot仅包含没有图片的框架。

解决方案是在vScreenShot内包含您的图片布局,或者拍摄父版面

的屏幕截图
<RelativeLayout android:id="@+id/my_layout">

     // include layout which contains your template/frame
     // include layout which contains your rounded ImageView.

</RelativeLayout>

现在您可以截取my_layout

的屏幕截图了
RelativeLayout my_layout=(RelativeLayout)findViewById(R.id.my_layout);
Bitmap bitmapSetDrawingChache = Bitmap.createBitmap(my_layout.getDrawingCache());