Created
September 30, 2020 07:49
-
-
Save vinaysshenoy/7bfa40b7b99db8bb28cbcd748bbd7c49 to your computer and use it in GitHub Desktop.
Render a view to a Bitmap of a specific size
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package `in`.obvious.android.bitmaptest | |
import android.graphics.Bitmap | |
import android.graphics.Canvas | |
import android.graphics.Matrix | |
import android.graphics.RectF | |
import android.os.Bundle | |
import android.os.Handler | |
import android.os.Looper | |
import android.view.LayoutInflater | |
import android.view.View | |
import android.view.ViewGroup | |
import android.widget.Button | |
import android.widget.Toast | |
import androidx.fragment.app.Fragment | |
import androidx.navigation.fragment.findNavController | |
import kotlinx.android.synthetic.main.fragment_first.* | |
import java.io.File | |
/** | |
* A simple [Fragment] subclass as the default destination in the navigation. | |
*/ | |
class FirstFragment : Fragment() { | |
private val handler = Handler(Looper.getMainLooper()) | |
override fun onCreateView( | |
inflater: LayoutInflater, container: ViewGroup?, | |
savedInstanceState: Bundle? | |
): View? { | |
// Inflate the layout for this fragment | |
return inflater.inflate(R.layout.fragment_first, container, false) | |
} | |
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | |
super.onViewCreated(view, savedInstanceState) | |
view.findViewById<Button>(R.id.button_first).setOnClickListener { | |
val image = Bitmap.createBitmap(595, 842, Bitmap.Config.ARGB_8888) | |
val sourceRect = | |
RectF(0F, 0F, textview_first.width.toFloat(), textview_first.height.toFloat()) | |
val destRect = RectF(0F, 0F, image.width.toFloat(), image.height.toFloat()) | |
val matrix = Matrix() | |
matrix.setRectToRect(sourceRect, destRect, Matrix.ScaleToFit.FILL) | |
val canvas = Canvas(image) | |
canvas.concat(matrix) | |
textview_first.draw(canvas) | |
Thread { | |
val context = requireContext() | |
val imageFile = File(context.filesDir, "image.png") | |
imageFile | |
.outputStream() | |
.use { stream -> | |
image.compress(Bitmap.CompressFormat.PNG, 90, stream) | |
stream.flush() | |
handler.post { | |
Toast.makeText(context, "Image Saved!", Toast.LENGTH_SHORT).show() | |
} | |
} | |
}.start() | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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" | |
android:background="#546E7A" | |
tools:context=".FirstFragment"> | |
<TextView | |
android:id="@+id/textview_first" | |
android:layout_width="250dp" | |
android:layout_height="354dp" | |
android:text="@string/hello_first_fragment" | |
app:layout_constraintBottom_toTopOf="@id/button_first" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="parent" | |
android:gravity="center" | |
android:background="#E0E0E0"/> | |
<Button | |
android:id="@+id/button_first" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="@string/next" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toBottomOf="@id/textview_first" /> | |
</androidx.constraintlayout.widget.ConstraintLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment