diff --git a/README.md b/README.md new file mode 100644 index 0000000..742d9de --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +### Screenshot - Take Screenshot Programmatically \ No newline at end of file diff --git a/app/src/main/java/com/mindorks/sample/MainActivity.kt b/app/src/main/java/com/mindorks/sample/MainActivity.kt index 7d36990..5972de1 100644 --- a/app/src/main/java/com/mindorks/sample/MainActivity.kt +++ b/app/src/main/java/com/mindorks/sample/MainActivity.kt @@ -20,8 +20,7 @@ class MainActivity : AppCompatActivity() { Screenshot.with(this) .setView(view) - .setQuality(Quality.AVERAGE) - .setRotation(Rotate.DEGREE_180) + .setQuality(Quality.HIGH) .setFlip(Flip.HORIZONTALLY) .getScreenshot() ) diff --git a/app/src/main/res/drawable/ic_android_black_24dp.xml b/app/src/main/res/drawable/ic_android_black_24dp.xml new file mode 100644 index 0000000..60d3c7a --- /dev/null +++ b/app/src/main/res/drawable/ic_android_black_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 50b9d07..4377290 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -18,13 +18,13 @@ app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" android:layout_marginTop="32dp" android:layout_marginBottom="8dp" app:layout_constraintBottom_toTopOf="@+id/view"/> - + - + \ No newline at end of file diff --git a/library/src/main/java/com/mindorks/utils/BitmapUtils.kt b/library/src/main/java/com/mindorks/utils/BitmapUtils.kt index 996d46c..f766e9c 100644 --- a/library/src/main/java/com/mindorks/utils/BitmapUtils.kt +++ b/library/src/main/java/com/mindorks/utils/BitmapUtils.kt @@ -1,14 +1,12 @@ package com.mindorks.utils -import android.graphics.Bitmap -import android.graphics.BitmapFactory -import android.graphics.Canvas -import android.graphics.Matrix +import android.graphics.* import android.view.View import com.mindorks.properties.Flip import com.mindorks.properties.Quality import com.mindorks.properties.Rotate import java.io.ByteArrayOutputStream +import java.io.OutputStream /** * @BitmpaUtils have all the bitmap properties and operations @@ -53,18 +51,16 @@ object BitmapUtils { * @param flip */ fun getScreenshot(view: View, rotate: Rotate, quality: Quality, flip: Flip): Bitmap { + val stream = ByteArrayOutputStream() val returnedBitmap = Bitmap.createBitmap(view.width, view.height, Bitmap.Config.ARGB_8888) val canvas = Canvas(returnedBitmap) - view.background.also { - it.draw(canvas) - } - view.run { - draw(canvas) - } - + val bgDrawable = view.background + if (bgDrawable != null) bgDrawable.draw(canvas) + else canvas.drawColor(Color.WHITE) + view.draw(canvas) returnedBitmap.run { - compress(Bitmap.CompressFormat.JPEG, quality.quality, stream) + compress(Bitmap.CompressFormat.JPEG, quality.quality, stream as OutputStream?) } val byteArray = stream.toByteArray() val bitmapAfterFlip = flip(BitmapFactory.decodeByteArray(byteArray, 0, byteArray.size), flip)