Reputation: 890
I have a CardView Layout with some views inside it. I want to convert that CardView(containing all views inside it) to an image.
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>
Here, the CardView cardfront , i want to convert that into an image bitmap and i want to show it in 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
Upvotes: 1
Views: 925
Reputation: 16
use the below function to the get bitmap from cardview
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
imageview.setImageBitmap(canvasBitmap);
Upvotes: 0
Reputation: 11018
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
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;
}
Upvotes: 1