将ConstraintLayout转换为图像并保存

时间:2020-02-29 07:53:49

标签: android android-studio

你好

所以我正在制作照片编辑器应用程序。我将两个图像并排添加到ConstraintLayout中。因此,我需要将ConstraintLayout转换为图像并将其保存在图库中。有可能吗?

Screenshot here

ConstraintLayout:

<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#f3f3f3">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/colorPrimary"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintDimensionRatio="16:13"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageView
            android:id="@+id/tv_image1"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginEnd="2dp"
            android:layout_marginStart="4dp"
            android:layout_marginTop="4dp"
            android:layout_marginBottom="4dp"
            android:scaleType="centerCrop"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/tv_image2"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:layout_height="0dp"
            tools:layout_width="0dp"
            android:src="@drawable/gatotriste"/>

        <ImageView
            android:id="@+id/tv_image2"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginEnd="4dp"
            android:layout_marginStart="2dp"
            android:layout_marginTop="4dp"
            android:layout_marginBottom="4dp"
            android:scaleType="centerCrop"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toEndOf="@+id/tv_image1"
            app:layout_constraintTop_toTopOf="parent"
            tools:layout_editor_absoluteY="0dp"
            tools:layout_height="0dp"
            tools:layout_width="0dp"
            android:src="@drawable/gatotriste" />
    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

谢谢!

2 个答案:

答案 0 :(得分:0)

传递约束视图并从中获取位图,将位图写入文件并保存,我使用了相同的逻辑来创建图像模板(图像+文本)

   public static Bitmap getBitMapFromView(View view){

    //Define a bitmap with the same size as the view
    Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
    //Bind a canvas to it
    Canvas canvas = new Canvas(returnedBitmap);
    //Get the view's background
    Drawable bgDrawable =view.getBackground();
    if (bgDrawable!=null)
        //has background drawable, then draw it on the canvas
        bgDrawable.draw(canvas);
    else
        //does not have background drawable, then draw white background on the canvas
        canvas.drawColor(Color.WHITE);
    // draw the view on the canvas
    view.draw(canvas);
    //return the bitmap
    return returnedBitmap;
}

答案 1 :(得分:0)

对于Kotlin,您可以使用此功能

private fun createBitmapFromView(ctx: Context, view: View): Bitmap {

view.layoutParams = ConstraintLayout.LayoutParams(
        ConstraintLayout.LayoutParams.MATCH_PARENT,
        ConstraintLayout.LayoutParams.MATCH_PARENT
    )
    val dm = ctx.resources.displayMetrics
    view.measure(
        View.MeasureSpec.makeMeasureSpec(dm.widthPixels, View.MeasureSpec.EXACTLY),
        View.MeasureSpec.makeMeasureSpec(dm.heightPixels, View.MeasureSpec.EXACTLY)
    )
    view.layout(0, 0, view.measuredWidth, view.measuredHeight)
    val bitmap = Bitmap.createBitmap(
        view.measuredWidth,
        view.measuredHeight,
        Bitmap.Config.ARGB_8888
    )
    val canvas = Canvas(bitmap)
    view.layout(view.left, view.top, view.right, view.bottom)
    view.draw(canvas)
    return bitmap
}
相关问题