回答編集履歴

2

コード追加

2021/09/18 11:05

投稿

jimbe
jimbe

スコア12672

test CHANGED
@@ -17,3 +17,237 @@
17
17
  離す→ MotionEvent.ACTION_UP(MotionEvent.ACTION_CANCEL もかな?)
18
18
 
19
19
  となってそれぞれ処理が出来たように思います。
20
+
21
+
22
+
23
+ ----
24
+
25
+
26
+
27
+ AlphaButton.java
28
+
29
+ ```java
30
+
31
+ package com.teratail.q360078;
32
+
33
+
34
+
35
+ import android.content.Context;
36
+
37
+ import android.util.AttributeSet;
38
+
39
+ import android.widget.ImageView;
40
+
41
+
42
+
43
+ import androidx.annotation.*;
44
+
45
+
46
+
47
+ /*** 半透明になるエフェクトのボタン ***/
48
+
49
+ public class AlphaButton extends androidx.appcompat.widget.AppCompatButton {
50
+
51
+ public AlphaButton(@NonNull Context context) {
52
+
53
+ super(context);
54
+
55
+ }
56
+
57
+
58
+
59
+ public AlphaButton(@NonNull Context context, @Nullable AttributeSet attrs) {
60
+
61
+ super(context, attrs);
62
+
63
+ }
64
+
65
+
66
+
67
+ public AlphaButton(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
68
+
69
+ super(context, attrs, defStyleAttr);
70
+
71
+ }
72
+
73
+
74
+
75
+ @Override
76
+
77
+ public void setPressed(boolean pressed) {
78
+
79
+ if(pressed) {
80
+
81
+ this.setAlpha(0.75f);
82
+
83
+ } else {
84
+
85
+ this.setAlpha(1.0f);
86
+
87
+ }
88
+
89
+ super.setPressed(pressed);
90
+
91
+ }
92
+
93
+
94
+
95
+ @Override
96
+
97
+ public boolean performClick() {
98
+
99
+ super.performClick();
100
+
101
+ return true;
102
+
103
+ }
104
+
105
+ }
106
+
107
+ ```
108
+
109
+ MainActivity.java
110
+
111
+ ```java
112
+
113
+ package com.teratail.q360078;
114
+
115
+
116
+
117
+ import androidx.appcompat.app.AppCompatActivity;
118
+
119
+ import androidx.appcompat.widget.AppCompatButton;
120
+
121
+
122
+
123
+ import android.os.Bundle;
124
+
125
+ import android.util.Log;
126
+
127
+ import android.view.*;
128
+
129
+ import android.widget.*;
130
+
131
+
132
+
133
+ public class MainActivity extends AppCompatActivity {
134
+
135
+ private static final String TAG = "MainActivity";
136
+
137
+
138
+
139
+ @Override
140
+
141
+ protected void onCreate(Bundle savedInstanceState) {
142
+
143
+ super.onCreate(savedInstanceState);
144
+
145
+ setContentView(R.layout.activity_main);
146
+
147
+
148
+
149
+ ImageView comment_img = findViewById(R.id.comment_img);
150
+
151
+ AlphaButton button = findViewById(R.id.button);
152
+
153
+ button.setOnTouchListener(new View.OnTouchListener() {
154
+
155
+ @Override
156
+
157
+ public boolean onTouch(View v, MotionEvent event) {
158
+
159
+ //Log.d(TAG, "event="+event);
160
+
161
+ switch(event.getAction()) {
162
+
163
+ case MotionEvent.ACTION_UP:
164
+
165
+ case MotionEvent.ACTION_CANCEL: //念の為
166
+
167
+ comment_img.setImageDrawable(null);
168
+
169
+ break;
170
+
171
+ case MotionEvent.ACTION_DOWN:
172
+
173
+ comment_img.setImageResource(R.drawable.image_6483441);
174
+
175
+ break;
176
+
177
+ }
178
+
179
+ return false;
180
+
181
+ }
182
+
183
+ });
184
+
185
+ }
186
+
187
+ }
188
+
189
+ ```
190
+
191
+ レイアウト: activity_main.xml
192
+
193
+ ```xml
194
+
195
+ <?xml version="1.0" encoding="utf-8"?>
196
+
197
+ <androidx.constraintlayout.widget.ConstraintLayout
198
+
199
+ xmlns:android="http://schemas.android.com/apk/res/android"
200
+
201
+ xmlns:app="http://schemas.android.com/apk/res-auto"
202
+
203
+ xmlns:tools="http://schemas.android.com/tools"
204
+
205
+ android:layout_width="match_parent"
206
+
207
+ android:layout_height="match_parent"
208
+
209
+ tools:context=".MainActivity">
210
+
211
+
212
+
213
+ <ImageView
214
+
215
+ android:id="@+id/comment_img"
216
+
217
+ android:layout_width="0dp"
218
+
219
+ android:layout_height="100dp"
220
+
221
+ app:layout_constraintBottom_toTopOf="@id/button"
222
+
223
+ app:layout_constraintLeft_toLeftOf="parent"
224
+
225
+ app:layout_constraintRight_toRightOf="parent"
226
+
227
+ app:layout_constraintTop_toTopOf="parent" />
228
+
229
+
230
+
231
+ <com.teratail.q360078.AlphaButton
232
+
233
+ android:id="@+id/button"
234
+
235
+ android:layout_width="wrap_content"
236
+
237
+ android:layout_height="wrap_content"
238
+
239
+ android:text="Hello World!"
240
+
241
+ app:layout_constraintBottom_toBottomOf="parent"
242
+
243
+ app:layout_constraintLeft_toLeftOf="parent"
244
+
245
+ app:layout_constraintRight_toRightOf="parent"
246
+
247
+ app:layout_constraintTop_toBottomOf="@id/comment_img" />
248
+
249
+
250
+
251
+ </androidx.constraintlayout.widget.ConstraintLayout>
252
+
253
+ ```

1

修正

2021/09/18 11:05

投稿

jimbe
jimbe

スコア12672

test CHANGED
@@ -10,6 +10,10 @@
10
10
 
11
11
 
12
12
 
13
- setOnTouchListener を Button に登録することで、MotionEvent の getAction()が 押す→
13
+ setOnTouchListener を Button に登録することで、MotionEvent の getAction()が
14
14
 
15
+ 押す→ MotionEvent.ACTION_DOWN
16
+
15
- MotionEvent.ACTION_DOWN 、離す→ MotionEvent.ACTION_UP(MotionEvent.ACTION_CANCEL もかな?) となって判断出来たように思います。
17
+ 離す→ MotionEvent.ACTION_UP(MotionEvent.ACTION_CANCEL もかな?)
18
+
19
+ となってそれぞれ処理が出来たように思います。