Преобразование карты в изображение (растровое изображение) - PullRequest
0 голосов
/ 19 апреля 2020

У меня есть макет CardView с некоторыми видами внутри него. Я хочу преобразовать этот CardView (содержащий все виды внутри него) в изображение.

CHECK OUT 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>
  • Этот макет является фиктивным макетом для демонстрационных целей ,

Здесь, CardView cardfront , я хочу преобразовать это в растровое изображение и показать его в ImageView Пустышка .

Я проверил много, но ни один из них не работает. И это тоже амортизируется. Каковы новые способы достижения этого.

(изображение) этого макета в изображение

Ответы [ 2 ]

0 голосов
/ 19 апреля 2020

используйте приведенную ниже функцию для получения растрового изображения из карты

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;
}

используйте следующее, чтобы установить растровое изображение в качестве фона просмотра изображения

imageview.setImageBitmap(canvasBitmap);
0 голосов
/ 19 апреля 2020

Я надеюсь, что вы попытались преобразовать представление в растровое изображение, если нет, то вот фрагмент кода ниже, попробуйте это и установите растровое изображение, преобразованное в dummy представление изображения

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;
}
...