【目的】
viewのキャプチャを行いたい!
(下記のアプローチではなく、スクリーンショットで範囲を指定する方法等あればご教授願いたいです、、、)
【現状】
以下のサイトを参照し、kotlinに置き換えた。
LineatLayoutの範囲と画面全体の2種類の画面キャプチャを試みた。
DCIMに所定のファイルを作り、画像の保存は一応できている。
http://developer.wonderpla.net/entry/blog/engineer/Android_CaptureView/
(上記URL内容のメールにて送信の部分は今回は書いていない)
【問題】
・範囲を指定したつもりだが、全体のスクリーンショットを撮ってしまっている
・保存した画像の背景が黒くなっている。
【コード】
MainActivity.kt
package com.example.user1.captureapplication import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.graphics.Bitmap import android.os.Environment import android.view.View import java.io.File import java.io.FileOutputStream import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val file = File(Environment.getExternalStorageDirectory().path + "/DCIM/capture_app/","test.jpg") // 指定したファイル名が無ければ作成する。 file.parentFile.mkdir() captureAll.setOnClickListener { saveCapture(findViewById(android.R.id.content), file) } captureView.setOnClickListener{ saveCapture(layoutView, file) } } } fun saveCapture(view: View,file: File) { // キャプチャを撮る val capture: Bitmap? = getViewCapture(view) try { val fos = FileOutputStream(file, false) // 画像のフォーマットと画質と出力先を指定して保存 capture?.compress(Bitmap.CompressFormat.JPEG, 100, fos) fos.flush() fos.close() }catch (e: Exception){ e.printStackTrace() } } fun getViewCapture(view: View): Bitmap? { view.isDrawingCacheEnabled = true // Viewのキャプチャを取得 val cache = view.drawingCache ?: return null val screenShot = Bitmap.createBitmap(cache) view.isDrawingCacheEnabled = false return screenShot
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.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="#008b8b" tools:context=".MainActivity" tools:layout_editor_absoluteY="81dp"> <LinearLayout android:id="@+id/layoutView" android:layout_width="368dp" android:layout_height="wrap_content" android:layout_marginBottom="8dp" android:layout_marginEnd="8dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:orientation="horizontal" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" app:srcCompat="@android:drawable/btn_dialog" /> <ImageView android:id="@+id/imageView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" app:srcCompat="@android:drawable/btn_radio" /> <ImageView android:id="@+id/imageView3" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" app:srcCompat="@android:drawable/btn_star_big_on" /> </LinearLayout> <Button android:id="@+id/captureAll" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="8dp" android:layout_marginLeft="8dp" android:layout_marginStart="8dp" android:text="ALL" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" /> <Button android:id="@+id/captureView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="8dp" android:layout_marginEnd="8dp" android:layout_marginRight="8dp" android:text="VIEW" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" /> </android.support.constraint.ConstraintLayout>
なんとしても全体ではなく、ある範囲を指定して画像をキャプチャしたいです、、、
何かアドバイスあれば、ぜひともご教授願います、、、
「範囲を指定したつもりだが、全体のスクリーンショットを撮ってしまっている」というのは、LinearLayoutの範囲外のViewが画像に含まれてしまっているということですか?
そう思っておりましたが、自分の勘違いでできておりました、、、せっかく回答いただいたにも関わらず、申し訳ありません。1つ疑問が残っているのですが、キャプチャした画像がギャラリーアプリで開くと背景色が黒くなっているのですが、これは改善できますでしょうか?
そうなのですね、範囲については解決されたようでよかったです。背景色が黒いのはLinearLayoutの背景色を指定していないからだと思います。android:backgroundで指定してください。
ご指摘ありがとうございます!解決いたしました!
回答1件
あなたの回答
tips
プレビュー