Last active
August 6, 2017 15:35
-
-
Save amyu/64fba4c1a26e28cf211c0d1be78786a8 to your computer and use it in GitHub Desktop.
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
<style name="Theme.AppCompat.Translucent" parent="Theme.AppCompat.NoActionBar"> | |
<item name="android:background">#33000000</item> | |
<item name="android:windowNoTitle">true</item> | |
<item name="android:windowBackground">@android:color/transparent</item> | |
<item name="android:colorBackgroundCacheHint">@null</item> | |
<item name="android:windowIsTranslucent">true</item> | |
<item name="android:windowAnimationStyle">@android:style/Animation</item> | |
</style> |
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
class TransparentActivity : AppCompatActivity() { | |
companion object { | |
fun createIntent(context: Context): Intent = Intent(context, TransparentActivity::class.java) | |
} | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_transparent) | |
val rootView = window.decorView.findViewById<View>(android.R.id.content).rootView | |
rootView.viewTreeObserver.let { observer -> | |
observer.addOnGlobalLayoutListener { | |
Completable.create { | |
val file = File(Environment.getExternalStoragePublicDirectory( | |
Environment.DIRECTORY_PICTURES), "Screenshots") | |
val date = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date()) | |
try { | |
rootView.isDrawingCacheEnabled = true | |
val bitmap = Bitmap.createBitmap(rootView.drawingCache) | |
rootView.isDrawingCacheEnabled = false | |
val imgFile = File(file.path + File.separator + | |
"IMG_" + date + ".jpg") | |
val outputStream = FileOutputStream(imgFile) | |
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream) | |
outputStream.flush() | |
outputStream.close() | |
it.onComplete() | |
} catch (e: Throwable) { | |
it.onError(e) | |
e.printStackTrace() | |
} | |
} | |
.subscribeOn(AndroidSchedulers.mainThread()) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribe({ | |
Toast.makeText(applicationContext, "take photo", Toast.LENGTH_SHORT).show() | |
}, { | |
Log.d("log", it.message) | |
Toast.makeText(applicationContext, "fail take photo\n ${it.message}", Toast.LENGTH_SHORT).show() | |
}) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment