AndroidのアプリでService上に配置したボタンをタップすると、画面のキャプチャをとり、特定の箇所に
DrawRectで矩形を付けたいのですが、スクリーン上のすべての領域を使用できず、
右端のエリアになると矩形が欠けて表示できないエリアがありました。
調べてみると、キャプチャで取得したbitmapのサイズとcanvasのサイズが異なることに気が付きました。
DrawRectでcanvasサイズを指定すると、やはり、右端部分に届いていませんでした。
サイズは下記の通りです。
画面サイズ:1080×1920
bitmapサイズ:1080×1794
canvasサイズ:840×1730
上記について、canvasサイズをbitmapサイズまたは画面サイズにすることはできますでしょうか?
どなたかご教示お願いいたします。
該当のソースコード
java
1package com.example.sumsu.overlaytest2; 2 3import android.content.Context; 4import android.graphics.Canvas; 5import android.graphics.Color; 6import android.graphics.Paint; 7import android.graphics.Rect; 8import android.util.AttributeSet; 9import android.util.DisplayMetrics; 10import android.view.View; 11import android.view.WindowManager; 12 13public class AnalyzeResultView extends View { 14 15 private Rect rect = null; 16 private Paint mPaint = new Paint(); 17 private int displayWidth ; 18 private int displayHeight ; 19 20 public AnalyzeResultView(Context context) { 21 super(context); 22 } 23 24 public AnalyzeResultView(Context context,AttributeSet attrs) { 25 super(context,attrs); 26 } 27 28 public AnalyzeResultView(Context context, AttributeSet attrs, int defStyle){ 29 super(context, attrs, defStyle); 30 } 31 32 // 結果をセットし描画を更新するためのメソッド 33 public void setAnalyzeResult(Rect rect) { 34 this.rect = rect; 35 36 postInvalidate(); 37 } 38 39 /*@Override 40 protected void onLayout(boolean changed, int l, int t, int r, int b) { 41 42 invalidate(); 43 }*/ 44 45 @Override 46 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 47 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 48 49 // 画面の縦横サイズとdpを取得 50 WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE); 51 DisplayMetrics displayMetrics = new DisplayMetrics(); 52 if(wm != null) { 53 wm.getDefaultDisplay().getMetrics(displayMetrics); 54 } 55 displayWidth = displayMetrics.widthPixels; 56 displayHeight = displayMetrics.heightPixels; 57 58 setMeasuredDimension(displayWidth,displayHeight); 59 60 } 61 62 63 @Override 64 public void onDraw(Canvas canvas) { 65 super.onDraw(canvas); 66 67 68 if(rect != null) { 69 70 //描画のペイント定義 71 mPaint.setAntiAlias(true); 72 mPaint.setStrokeWidth(12); 73 mPaint.setStyle(Paint.Style.STROKE); 74 mPaint.setColor(Color.BLUE); 75 76 // 矩形の描画処理... 77 canvas.drawRect(rect, mPaint); 78 } 79 } 80 81} 82
xml
1activity_main.xml 2 3<?xml version="1.0" encoding="utf-8"?> 4<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 5 xmlns:app="http://schemas.android.com/apk/res-auto" 6 xmlns:tools="http://schemas.android.com/tools" 7 android:layout_width="match_parent" 8 android:layout_height="match_parent" 9 tools:context=".MainActivity"> 10 11 <Button 12 android:id="@+id/button" 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:layout_marginStart="8dp" 16 android:layout_marginTop="8dp" 17 android:onClick="button1_click" 18 android:text="@string/btnStart" 19 app:layout_constraintStart_toStartOf="parent" 20 app:layout_constraintTop_toTopOf="parent" /> 21 22 <Button 23 android:id="@+id/button2" 24 android:layout_width="wrap_content" 25 android:layout_height="wrap_content" 26 android:layout_marginStart="8dp" 27 android:layout_marginTop="8dp" 28 android:onClick="button2_click" 29 android:text="@string/btnEnd" 30 app:layout_constraintStart_toStartOf="parent" 31 app:layout_constraintTop_toBottomOf="@+id/button" /> 32</android.support.constraint.ConstraintLayout>
xml
1overlay.xml 2 3<?xml version="1.0" encoding="utf-8"?> 4<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 5 xmlns:app="http://schemas.android.com/apk/res-auto" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent"> 8 9 <com.example.sumsu.overlaytest2.AnalyzeResultView 10 android:id="@+id/ResultView" 11 android:layout_width="match_parent" 12 android:layout_height="match_parent" /> 13</LinearLayout>
xml
1overlay2.xml 2 3<?xml version="1.0" encoding="utf-8"?> 4<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 5 android:id="@+id/linearLayout" 6 android:layout_width="wrap_content" 7 android:layout_height="wrap_content" 8 android:gravity="top|left" 9 android:orientation="vertical"> 10 11 <LinearLayout 12 android:id="@+id/linearLayout1" 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:orientation="horizontal"> 16 17 <Button 18 android:id="@+id/start" 19 android:layout_width="match_parent" 20 android:layout_height="wrap_content" 21 android:layout_weight="1" 22 android:background="#99FF0000" 23 android:text="Start" /> 24 25 <Button 26 android:id="@+id/stop" 27 android:layout_width="match_parent" 28 android:layout_height="wrap_content" 29 android:layout_weight="1" 30 android:background="#9900ff00" 31 android:text="Stop" /> 32 33 </LinearLayout> 34 35</LinearLayout>
試したこと
2018/08/08 18:07追記
onMeasureを追加することで、Viewのサイズの変更ができました。
ただ、ナビゲーションバーとステータスバーのエリアは使用できませんでした。
こちらを使用することはできるのでしょうか?
2018/08/11 02:36追記
Viewのサイズを画面サイズ(getRealSize):1080×1920と同じにすると、
ステータスバーの下から矩形が描かれ、下側が表示できませんでした。
フルスクリーンにゲームなどを起動すると、ステータスバーが消えるため、矩形も上に詰まる形で
移動するのですが、下側の枠が描かれることはなく、矩形がそのまま上に移動するだけでした。
特定の箇所にマークを付けたいので、四角い枠を画面いっぱいに表現したいのですが、できないものでしょうか?
ご存知の方、ご教示ください。よろしくお願いいたします。
2018/08/18 02:37追記
ナビゲーションバーに割り込むも半分まで・・・ステータスバーより上にずれて描画されました。
回答2件
あなたの回答
tips
プレビュー