티스토리 뷰
반응형
뷰 화면을 캡쳐해서 이미지 뷰에 넣어보는 기능을 추가할 때 사용하는 코드이다.
간단한 레이아웃 만들기.
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<import type="android.view.View" />
</data>
<LinearLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
android:gravity="center"
android:orientation="vertical">
<Button
android:id="@+id/captureScreenShot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Capture" />
<ImageView
android:id="@+id/imageView"
android:layout_width="200dp"
android:layout_height="250dp"
android:background="#ccc"
android:padding="5dp" />
</LinearLayout>
</layout>
캡쳐 기능 메소드 및 사용 코드. (canvas 를 사용.)
binding.captureScreenShot.setOnClickListener {
capture()
}
private fun capture() {
val now = SimpleDateFormat("yyyyMMdd_hhmmss").format(Date(System.currentTimeMillis()))
val mPath = cacheDir.absolutePath + "/$now.jpg"
var bitmap: Bitmap? = null
val captureView = binding.captureScreenShot //캡처할 뷰
bitmap = Bitmap.createBitmap(captureView.width, captureView.height, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
captureView.draw(canvas)
if (bitmap == null) {
} else {
val imageFile = File(mPath)
val outputStream = FileOutputStream(imageFile)
outputStream.use {
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream)
outputStream.flush()
}
binding.imageView.setImageBitmap(bitmap)
}
}
반응형
'안드로이드' 카테고리의 다른 글
[Android Studio] ViewPager2 다음 페이지 보이게 하기 (0) | 2024.03.04 |
---|---|
[Android Studio] 바텀 네비게이션 뱃지 활용하기 (코틀린) (0) | 2024.01.19 |
[Android Studio] TextWatcher Utils 로 사용하기 (코틀린) (1) | 2023.11.03 |
[Android Studio] TextInputLayout Custom 사용하기 ! (0) | 2023.11.03 |
[Android Studio] TextView Drawable 동적으로 수정하기 (코틀린) (0) | 2023.10.12 |
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- Custom
- java
- ec2
- Flutter
- direction
- Crop
- Android Studio
- GitHub
- Kotlin
- Token
- message
- FCM
- 코딩테스트
- bitmap
- Hilt
- ScrollView
- Firebase
- error
- API
- node.js
- ExoPlayer
- 재귀함수
- android
- retrofit
- app bundle
- https
- listener
- flutter_new_badger
- 알고리즘
- ios
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
글 보관함