回答編集履歴

2

マークダウン修正

2022/03/28 11:03

投稿

jimbe
jimbe

スコア12648

test CHANGED
@@ -38,7 +38,8 @@
38
38
  }
39
39
  ```
40
40
  res/layout/activity_main.xml
41
+ ```xml
41
- ```xml<?xml version="1.0" encoding="utf-8"?>
42
+ <?xml version="1.0" encoding="utf-8"?>
42
43
  <androidx.constraintlayout.widget.ConstraintLayout
43
44
  xmlns:android="http://schemas.android.com/apk/res/android"
44
45
  xmlns:app="http://schemas.android.com/apk/res-auto"

1

コード追加

2022/03/28 10:58

投稿

jimbe
jimbe

スコア12648

test CHANGED
@@ -6,3 +6,111 @@
6
6
  TrimView で使用しようとしている TextView はコンストラクト時に必要なわけでは無く、単にタッチイベント時にデータを表示するだけのようです。
7
7
  であれば、 "指を離した場合の処理" を行うリスナを定義し、それを TrimView に設定できるようにすることで、 TrimView が特定の View に依存する形を止めることが出来るのではないでしょうか。
8
8
 
9
+ ※以下のコードはイメージです。コンパイルエラーは出ていませんが動作確認はしていません。
10
+
11
+ MainActivity.java
12
+ ```java
13
+ package com.teratail.q_o11p3cl1yt1nyy;
14
+
15
+ import androidx.appcompat.app.AppCompatActivity;
16
+
17
+ import android.os.Bundle;
18
+ import android.widget.TextView;
19
+
20
+ public class MainActivity extends AppCompatActivity {
21
+ @Override
22
+ protected void onCreate(Bundle savedInstanceState) {
23
+ super.onCreate(savedInstanceState);
24
+ setContentView(R.layout.activity_main);
25
+
26
+ TextView textView = findViewById(R.id.txtRect);
27
+
28
+ TrimView trimView = findViewById(R.id.trim_view);
29
+ trimView.setOnActionUpListener(rect -> {
30
+ // 矩形のサイズをテキストに変換してxmlに反映する
31
+ String str = "left="+rect.left
32
+ +"\ntop="+rect.top
33
+ +"\nright="+rect.right
34
+ +"\nbottom="+rect.bottom;
35
+ textView.setText(str); // 矩形のサイズを反映
36
+ });
37
+ }
38
+ }
39
+ ```
40
+ res/layout/activity_main.xml
41
+ ```xml<?xml version="1.0" encoding="utf-8"?>
42
+ <androidx.constraintlayout.widget.ConstraintLayout
43
+ xmlns:android="http://schemas.android.com/apk/res/android"
44
+ xmlns:app="http://schemas.android.com/apk/res-auto"
45
+ xmlns:tools="http://schemas.android.com/tools"
46
+ android:layout_width="match_parent"
47
+ android:layout_height="match_parent"
48
+ tools:context=".MainActivity">
49
+
50
+ <com.teratail.q_o11p3cl1yt1nyy.TrimView
51
+ android:id="@+id/trim_view"
52
+ android:layout_width="wrap_content"
53
+ android:layout_height="wrap_content"
54
+ android:text="Hello World!"
55
+ app:layout_constraintBottom_toTopOf="@id/txtRect"
56
+ app:layout_constraintLeft_toLeftOf="parent"
57
+ app:layout_constraintRight_toRightOf="parent"
58
+ app:layout_constraintTop_toTopOf="parent" />
59
+
60
+ <TextView
61
+ android:id="@+id/txtRect"
62
+ android:layout_width="match_parent"
63
+ android:layout_height="wrap_content"
64
+ app:layout_constraintBottom_toBottomOf="parent"
65
+ app:layout_constraintLeft_toLeftOf="parent"
66
+ app:layout_constraintRight_toRightOf="parent"
67
+ app:layout_constraintTop_toBottomOf="@id/trim_view" />
68
+
69
+ </androidx.constraintlayout.widget.ConstraintLayout>
70
+ ```
71
+ TrimView.java
72
+ ```java
73
+ package com.teratail.q_o11p3cl1yt1nyy;
74
+
75
+ import android.content.Context;
76
+ import android.graphics.Rect;
77
+ import android.util.AttributeSet;
78
+ import android.view.MotionEvent;
79
+
80
+ import androidx.annotation.*;
81
+ import androidx.appcompat.widget.AppCompatImageView;
82
+
83
+ public class TrimView extends AppCompatImageView {
84
+ interface OnActionUpListener {
85
+ void onActionUp(Rect rect);
86
+ }
87
+
88
+ public TrimView(Context context, @Nullable AttributeSet attrs) {
89
+ super(context, attrs);
90
+ }
91
+
92
+ private Rect rect;
93
+
94
+ private OnActionUpListener onActionUpListener;
95
+
96
+ void setOnActionUpListener(OnActionUpListener listener) {
97
+ this.onActionUpListener = listener;
98
+ }
99
+
100
+ //@Override
101
+ //@SuppressLint("ClickableViewAccessibility")
102
+ public boolean onTouchEvent(@NonNull MotionEvent event) {
103
+ // 押した場合の処理
104
+ if (event.getAction() == MotionEvent.ACTION_DOWN) {
105
+ }
106
+ // ドラッグした場合の処理
107
+ else if (event.getAction() == MotionEvent.ACTION_MOVE) {
108
+ }
109
+ // 指を離した場合の処理
110
+ else if (event.getAction() == MotionEvent.ACTION_UP) {
111
+ if(onActionUpListener != null) onActionUpListener.onActionUp(rect);
112
+ }
113
+ return true;
114
+ }
115
+ }
116
+ ```