質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

Q&A

解決済

1回答

1323閲覧

kotlin:viewのキャプチャがうまくいきません、、、(一定の画面範囲を画像で保存したい。)

akawo

総合スコア23

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

0グッド

0クリップ

投稿2018/08/17 14:28

【目的】
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>

なんとしても全体ではなく、ある範囲を指定して画像をキャプチャしたいです、、、
何かアドバイスあれば、ぜひともご教授願います、、、

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

kakajika

2018/08/17 23:45

「範囲を指定したつもりだが、全体のスクリーンショットを撮ってしまっている」というのは、LinearLayoutの範囲外のViewが画像に含まれてしまっているということですか?
akawo

2018/08/17 23:57

そう思っておりましたが、自分の勘違いでできておりました、、、せっかく回答いただいたにも関わらず、申し訳ありません。1つ疑問が残っているのですが、キャプチャした画像がギャラリーアプリで開くと背景色が黒くなっているのですが、これは改善できますでしょうか?
kakajika

2018/08/18 00:03

そうなのですね、範囲については解決されたようでよかったです。背景色が黒いのはLinearLayoutの背景色を指定していないからだと思います。android:backgroundで指定してください。
akawo

2018/08/18 00:11

ご指摘ありがとうございます!解決いたしました!
guest

回答1

0

自己解決

追記・修正依頼の場所で解決してしまいました。
kakajikaさんありがとうございました!!

投稿2018/12/31 10:09

akawo

総合スコア23

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問