teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

5

2021/07/31 12:00

投稿

schoolstudent
schoolstudent

スコア5

title CHANGED
@@ -1,1 +1,1 @@
1
- [Android java]画像をタップして色コードを取得
1
+ 画像をタップして色コードを取得し、近い色を探して色名を得プログラム
body CHANGED
File without changes

4

教えてくださったことを参考にプログラムを編集しました。 カラーコードから色名の取得について質問を追加しました。

2021/07/31 12:00

投稿

schoolstudent
schoolstudent

スコア5

title CHANGED
File without changes
body CHANGED
@@ -4,11 +4,16 @@
4
4
  また、座標を求めることはできたのですが、やりたいことは同じなので画像とか関係なく座標の数値だけで色コードを取得することはできませんか?(取得範囲を画面全体にするということ)
5
5
  どうかご教授お願いします。
6
6
  (下記プログラムは端末内の画像を選択し出力、そして、タップした座標を求めるプログラムです。)
7
+ ◎追加記
8
+ タップした場所のカラーコードを取得するプログラムにしました。
9
+ カラーコードから色名を取得、出力する方法をご教授お願いします。
7
10
  ```java
11
+
8
12
  import androidx.appcompat.app.AppCompatActivity;
9
13
  import androidx.core.content.ContextCompat;
10
14
 
11
15
 
16
+ import android.annotation.SuppressLint;
12
17
  import android.content.Intent;
13
18
  import android.graphics.Bitmap;
14
19
  import android.graphics.BitmapFactory;
@@ -31,7 +36,8 @@
31
36
  public class MainActivity extends AppCompatActivity {
32
37
  private static final int RESULT_PICK_IMAGEFILE = 1000;
33
38
  private ImageView imageView;
39
+ private Bitmap bmp;
34
- private TextView tap = null;
40
+ private TextView colorRGB;
35
41
 
36
42
 
37
43
  @Override
@@ -40,7 +46,7 @@
40
46
  setContentView(R.layout.activity_main);
41
47
  imageView = (ImageView) findViewById(R.id.image_view);
42
48
 
43
- this.tap = (TextView) findViewById(R.id.tap_id);
49
+ colorRGB = (TextView)findViewById(R.id.colorrgb);
44
50
 
45
51
 
46
52
  findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
@@ -65,7 +71,7 @@
65
71
  uri = resultData.getData();
66
72
 
67
73
  try {
68
- Bitmap bmp = getBitmapFromUri(uri);
74
+ bmp = getBitmapFromUri(uri);
69
75
  imageView.setImageBitmap(bmp);
70
76
  } catch (IOException e) {
71
77
  e.printStackTrace();
@@ -85,13 +91,21 @@
85
91
 
86
92
  }
87
93
 
94
+ @SuppressLint("SetTextI18n")
88
95
  @Override
89
96
  public boolean onTouchEvent(MotionEvent event) {
97
+ switch (event.getAction()) {
98
+ case MotionEvent.ACTION_DOWN:
90
99
  int x = (int) event.getRawX(); //タッチしたX座標
91
100
  int y = (int) event.getRawY(); //タッチしたY座標
92
101
 
93
- tap.setText("X座標 " + x + "Y座標 " + y);
102
+ int touchedRGB = bmp.getPixel(x, y);
94
103
 
104
+ colorRGB.setText("touched color: " + "#" + Integer.toHexString(touchedRGB));
105
+ colorRGB.setTextColor(touchedRGB);
106
+ break;
107
+ }
108
+
95
109
  return true;
96
110
 
97
111
  }

3

誤字

2021/07/29 16:21

投稿

schoolstudent
schoolstudent

スコア5

title CHANGED
File without changes
body CHANGED
@@ -3,7 +3,7 @@
3
3
  どうか、public boolean onTouchEvent(MotionEvent event) { 内の動作を教えていただけないでしょうか、、、。
4
4
  また、座標を求めることはできたのですが、やりたいことは同じなので画像とか関係なく座標の数値だけで色コードを取得することはできませんか?(取得範囲を画面全体にするということ)
5
5
  どうかご教授お願いします。
6
- (下記プログラムは端末内の画像を選択し出力るプログラムです。)
6
+ (下記プログラムは端末内の画像を選択し出力、そして、タップした座標を求めるプログラムです。)
7
7
  ```java
8
8
  import androidx.appcompat.app.AppCompatActivity;
9
9
  import androidx.core.content.ContextCompat;

2

座標を求めるプログラムを追加しておきました。

2021/07/28 22:40

投稿

schoolstudent
schoolstudent

スコア5

title CHANGED
File without changes
body CHANGED
@@ -31,6 +31,7 @@
31
31
  public class MainActivity extends AppCompatActivity {
32
32
  private static final int RESULT_PICK_IMAGEFILE = 1000;
33
33
  private ImageView imageView;
34
+ private TextView tap = null;
34
35
 
35
36
 
36
37
  @Override
@@ -39,7 +40,9 @@
39
40
  setContentView(R.layout.activity_main);
40
41
  imageView = (ImageView) findViewById(R.id.image_view);
41
42
 
43
+ this.tap = (TextView) findViewById(R.id.tap_id);
42
44
 
45
+
43
46
  findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
44
47
  @Override
45
48
  public void onClick(View v) {
@@ -81,8 +84,16 @@
81
84
  return image;
82
85
 
83
86
  }
87
+
84
88
  @Override
85
89
  public boolean onTouchEvent(MotionEvent event) {
90
+ int x = (int) event.getRawX(); //タッチしたX座標
91
+ int y = (int) event.getRawY(); //タッチしたY座標
86
92
 
93
+ tap.setText("X座標 " + x + "Y座標 " + y);
94
+
95
+ return true;
96
+
87
97
  }
98
+ }
88
99
  ```

1

誤字

2021/07/28 22:38

投稿

schoolstudent
schoolstudent

スコア5

title CHANGED
File without changes
body CHANGED
@@ -3,6 +3,7 @@
3
3
  どうか、public boolean onTouchEvent(MotionEvent event) { 内の動作を教えていただけないでしょうか、、、。
4
4
  また、座標を求めることはできたのですが、やりたいことは同じなので画像とか関係なく座標の数値だけで色コードを取得することはできませんか?(取得範囲を画面全体にするということ)
5
5
  どうかご教授お願いします。
6
+ (下記プログラムは端末内の画像を選択し、出力するプログラムです。)
6
7
  ```java
7
8
  import androidx.appcompat.app.AppCompatActivity;
8
9
  import androidx.core.content.ContextCompat;
@@ -30,7 +31,6 @@
30
31
  public class MainActivity extends AppCompatActivity {
31
32
  private static final int RESULT_PICK_IMAGEFILE = 1000;
32
33
  private ImageView imageView;
33
- private TextView colorRGB;
34
34
 
35
35
 
36
36
  @Override