简体   繁体   English

将卡片视图转换为图像(位图)

[英]Converting a cardview to an image (bitmap)

I have a CardView Layout with some views inside it.我有一个 CardView 布局,里面有一些视图。 I want to convert that CardView(containing all views inside it) to an image.我想将该 CardView(包含其中的所有视图)转换为图像。

CHECK OUT XML:查看 XML:

<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
    tools:context=".MISC.IdentityCardActivity">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/header"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="10dp"
                android:text="  "
                android:textColor="@color/blacko"
                android:textSize="18dp"
                android:textStyle="bold" />


            <androidx.cardview.widget.CardView
                android:id="@+id/cardfront"
                android:layout_width="390dp"
                android:layout_height="190dp"
                android:layout_below="@id/header"
                android:layout_centerHorizontal="true"
                android:layout_marginLeft="15dp"
                android:layout_marginTop="40dp"
                android:layout_marginRight="15dp"
                app:cardBackgroundColor="@color/ash"
                app:cardCornerRadius="10dp"
                app:cardElevation="8dp">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <ImageView
                        android:id="@+id/cardbackgorun"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:scaleType="centerCrop"
                        android:src="@drawable/jeep" />

                    <TextView
                        android:id="@+id/heading"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentTop="true"
                        android:layout_centerHorizontal="true"
                        android:layout_marginTop="5dp"
                        android:fontFamily="@font/creditcard"
                        android:shadowColor="@color/text_shadow"
                        android:shadowDx="1"
                        android:shadowDy="1"
                        android:shadowRadius="10"
                        android:text=" RC IDENTITY CARD "
                        android:textColor="@color/white"
                        android:textSize="16dp" />


                    <TextView
                        android:id="@+id/uid"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@id/heading"
                        android:layout_centerInParent="true"
                        android:layout_marginTop="10dp"
                        android:fontFamily="@font/oswaldbold"
                        android:shadowColor="@color/text_shadow"
                        android:shadowDx="1"
                        android:shadowDy="1"
                        android:shadowRadius="10"
                        android:text="9C1bWbji49W613qVZowsQwMAOH32"
                        android:textColor="@color/white"
                        android:textSize="20dp" />

                    <LinearLayout
                        android:id="@+id/datesLL"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@id/uid"
                        android:layout_centerHorizontal="true"
                        android:layout_marginTop="5dp"
                        android:orientation="horizontal">

                        <TextView
                            android:id="@+id/validfrom"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:fontFamily="@font/creditcard"
                            android:shadowColor="@color/text_shadow"
                            android:shadowDx="1"
                            android:shadowDy="1"
                            android:shadowRadius="10"
                            android:text="VALID FROM: "
                            android:textColor="@color/white"
                            android:textSize="10dp" />

                        <TextView
                            android:id="@+id/validfromdate"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:fontFamily="@font/creditcard"
                            android:shadowColor="@color/text_shadow"
                            android:shadowDx="1"
                            android:shadowDy="1"
                            android:shadowRadius="10"
                            android:text="11/23           "
                            android:textColor="@color/white"
                            android:textSize="10dp" />

                        <TextView
                            android:id="@+id/validthru"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:fontFamily="@font/creditcard"
                            android:shadowColor="@color/text_shadow"
                            android:shadowDx="1"
                            android:shadowDy="1"
                            android:shadowRadius="10"
                            android:text="VALID THRU: "
                            android:textColor="@color/white"
                            android:textSize="10dp" />

                        <TextView
                            android:id="@+id/validthrudate"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:fontFamily="@font/creditcard"
                            android:shadowColor="@color/text_shadow"
                            android:shadowDx="1"
                            android:shadowDy="1"
                            android:shadowRadius="10"
                            android:text="11/35"
                            android:textColor="@color/white"
                            android:textSize="10dp" />
                    </LinearLayout>

                    <RelativeLayout
                        android:id="@+id/userRL"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@id/datesLL"
                        android:layout_centerHorizontal="true"
                        android:layout_marginTop="12dp"
                        android:layout_marginBottom="8dp">


                        <de.hdodenhof.circleimageview.CircleImageView
                            android:id="@+id/userimage"
                            android:layout_width="90dp"
                            android:layout_height="90dp"
                            android:layout_centerVertical="true"
                            android:src="@drawable/def" />

                        <RelativeLayout
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_centerVertical="true"
                            android:layout_marginLeft="10dp"
                            android:layout_toRightOf="@id/userimage">

                            <TextView
                                android:id="@+id/username"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:ellipsize="end"
                                android:fontFamily="@font/creditcard"
                                android:maxLines="1"
                                android:shadowColor="@color/text_shadow"
                                android:shadowDx="1"
                                android:shadowDy="1"
                                android:shadowRadius="10"
                                android:text="NITHEESH AG"
                                android:textColor="@color/white" />

                            <TextView
                                android:id="@+id/rides"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_below="@id/username"
                                android:fontFamily="@font/creditcard"
                                android:shadowColor="@color/text_shadow"
                                android:shadowDx="1"
                                android:shadowDy="1"
                                android:shadowRadius="10"
                                android:text="RIDES"
                                android:textColor="@color/white" />

                            <TextView
                                android:id="@+id/vehiclemake"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_below="@id/rides"
                                android:ellipsize="end"
                                android:fontFamily="@font/creditcard"
                                android:maxLines="1"
                                android:shadowColor="@color/text_shadow"
                                android:shadowDx="1"
                                android:shadowDy="1"
                                android:shadowRadius="10"
                                android:text="HONDA"
                                android:textColor="@color/white" />

                            <TextView
                                android:id="@+id/vehiclemodel"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_below="@id/vehiclemake"
                                android:ellipsize="end"
                                android:fontFamily="@font/creditcard"
                                android:maxLines="1"
                                android:shadowColor="@color/text_shadow"
                                android:shadowDx="1"
                                android:shadowDy="1"
                                android:shadowRadius="10"
                                android:text="CBR 150R"
                                android:textColor="@color/white" />
                        </RelativeLayout>
                    </RelativeLayout>


                </RelativeLayout>


            </androidx.cardview.widget.CardView>


            <ImageView
                android:id="@+id/dummy"
                android:layout_width="300dp"
                android:layout_height="200dp"
                android:layout_below="@id/cardfront"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="30dp"
                android:scaleType="centerCrop" />

        </RelativeLayout>
    </ScrollView>


</androidx.constraintlayout.widget.ConstraintLayout>
  • This layout is a dummy layout for demonstration purpose.此布局是用于演示目的的虚拟布局。

Here, the CardView cardfront , i want to convert that into an image bitmap and i want to show it in ImageView dummy .在这里, CardView卡片正面,我想将其转换为图像 bitmap 并且我想在 ImageView dummy中显示它。

I have checked a lots of, but none of them are working.我检查了很多,但没有一个工作。 And those are also depreciated.而且这些也贬值了。 What are the new ways of achieving it.实现它的新方法是什么。

(image)this layout to an image (图像)此布局到图像

I hope you have tried converting a view to bitmap if not then here is code snippet below try this out and set the bitmap converted into the dummy image view我希望您已尝试将视图转换为 bitmap 如果不是那么这里是下面的代码片段尝试一下并将 bitmap 转换为虚拟图像视图

public static Bitmap getBitmapFromView(View view) {
    view.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
    Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(),
            Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
    view.draw(canvas);
    return bitmap;
}

use the below function to the get bitmap from cardview使用下面的 function 从 cardview 获取 bitmap

protected Bitmap convertToBitmap(CardView view) {
    int totalHeight = view.getHeight;
    int totalWidth = view.getWidth();
    float percent = 0.7f;//use this value to scale bitmap to specific size

    Bitmap canvasBitmap = Bitmap.createBitmap(totalWidth,totalHeight, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(canvasBitmap);
    canvas.scale(percent, percent);
    view.draw(canvas);

    return canvasBitmap;
}

use following to set bitmap as imageview background使用以下设置 bitmap 为 imageview 背景

imageview.setImageBitmap(canvasBitmap);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM