服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android实现颜色选取圆盘

Android实现颜色选取圆盘

2022-02-28 15:10ZhengJingLe Android

这篇文章主要为大家详细介绍了Android实现颜色选取圆盘,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Android实现颜色选取圆盘的具体代码,供大家参考,具体内容如下

先看效果图

Android实现颜色选取圆盘

xml布局

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 >
<TextView
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="@string/hello"
 android:id="@+id/tv_rgb"/>
<RelativeLayout android:id="@+id/relativeLayout1" android:layout_height="fill_parent" android:layout_width="fill_parent">
 
 <com.myview.ColorPickerView
  android:id="@+id/cpv"
  android:layout_width="230dp"
  android:layout_height="230dp"
  android:layout_centerInParent="true"
  android:scaleType="center"
  android:src="@drawable/rgb" />
 
</RelativeLayout>
</LinearLayout>

ColorPickerView颜色选取圆盘

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package com.myview;
 
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;
 
public class ColorPickerView extends ImageView {
 Context context;
 private Bitmap iconBitMap;
 float iconRadius;// 吸管圆的半径
 float iconCenterX;
 float iconCenterY;
 PointF iconPoint;// 点击位置坐标
 
 public ColorPickerView(Context context) {
  this(context, null);
 }
 
 public ColorPickerView(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
  this.context = context;
  init();
 }
 
 public ColorPickerView(Context context, AttributeSet attrs) {
  this(context, attrs, 0);
  init();
 }
 
 Paint mBitmapPaint;
 Bitmap imageBitmap;
 float viewRadius;// 整个view半径
 float radius;// 图片半径
 
 /**
  * 初始化画笔
  */
 private void init() {
  iconBitMap = BitmapFactory.decodeResource(context.getResources(),
    R.drawable.pickup);// 吸管的图片
  iconRadius = iconBitMap.getWidth() / 2;// 吸管的图片一半
 
  mBitmapPaint = new Paint();
  iconPoint = new PointF();
 
  imageBitmap = ((BitmapDrawable) getDrawable()).getBitmap();
  radius = imageBitmap.getHeight() / 2;// 图片半径
 
  // // 初始化
  iconPoint.x = radius;
  iconPoint.y = radius;
 
 }
 
 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  // TODO Auto-generated method stub
  super.onMeasure(widthMeasureSpec, heightMeasureSpec);
 }
 
 Canvas canvas;
 
 @Override
 protected void onDraw(Canvas canvas) {
  // TODO Auto-generated method stub
  super.onDraw(canvas);
  this.canvas = canvas;
 
  viewRadius = this.getWidth() / 2;// 整个view半径
 
  canvas.drawBitmap(iconBitMap, iconPoint.x - iconRadius, iconPoint.y
    - iconRadius, mBitmapPaint);
 }
 
 @SuppressLint("ClickableViewAccessibility")
 @Override
 public boolean onTouchEvent(MotionEvent event) {
  float x = event.getX();
  float y = event.getY();
  int pixel;
  int r;
  int g;
  int b;
  switch (event.getAction()) {
  case MotionEvent.ACTION_MOVE:
   proofLeft(x, y);
   pixel = getImagePixel(iconPoint.x, iconPoint.y);
   r = Color.red(pixel);
   g = Color.green(pixel);
   b = Color.blue(pixel);
   if (mChangedListener != null) {
    mChangedListener.onMoveColor(r, g, b);
   }
   if (isMove) {
    isMove = !isMove;
    invalidate();
   }
   break;
  case MotionEvent.ACTION_UP:
   pixel = getImagePixel(iconPoint.x, iconPoint.y);
   r = Color.red(pixel);
   g = Color.green(pixel);
   b = Color.blue(pixel);
   if (mChangedListener != null) {
    mChangedListener.onColorChanged(r, g, b);
   }
   break;
 
  default:
   break;
  }
  return true;
 }
 
 public int getImagePixel(float x, float y) {
 
  Bitmap bitmap = imageBitmap;
  // 为了防止越界
  int intX = (int) x;
  int intY = (int) y;
  if (intX < 0)
   intX = 0;
  if (intY < 0)
   intY = 0;
  if (intX >= bitmap.getWidth()) {
   intX = bitmap.getWidth() - 1;
  }
  if (intY >= bitmap.getHeight()) {
   intY = bitmap.getHeight() - 1;
  }
  int pixel = bitmap.getPixel(intX, intY);
  return pixel;
 
 }
 
 /**
  * R = sqrt(x * x + y * y)
  * point.x = x * r / R + r
  * point.y = y * r / R + r
  */
 private void proofLeft(float x, float y) {
 
  float h = x - viewRadius; // 取xy点和圆点 的三角形宽
  float w = y - viewRadius;// 取xy点和圆点 的三角形长
  float h2 = h * h;
  float w2 = w * w;
  float distance = (float) Math.sqrt((h2 + w2)); // 勾股定理求 斜边距离
  if (distance > radius) { // 如果斜边距离大于半径,则取点和圆最近的一个点为x,y
   float maxX = x - viewRadius;
   float maxY = y - viewRadius;
   x = ((radius * maxX) / distance) + viewRadius; // 通过三角形一边平行原理求出x,y
   y = ((radius * maxY) / distance) + viewRadius;
  }
  iconPoint.x = x;
  iconPoint.y = y;
 
  isMove = true;
 }
 
 boolean isMove;
 
 public void setOnColorChangedListenner(OnColorChangedListener l) {
  this.mChangedListener = l;
 }
 
 private OnColorChangedListener mChangedListener;
 
 // 内部接口 回调颜色 rgb值
 public interface OnColorChangedListener {
  // 手指抬起,确定颜色回调
  void onColorChanged(int r, int g, int b);
 
  // 移动时颜色回调
  void onMoveColor(int r, int g, int b);
 }
}

MyViewActivity主界面

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.myview;
 
import com.myview.ColorPickerView.OnColorChangedListener;
 
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
 
public class MyViewActivity extends Activity {
  
 TextView tv_rgb;
  
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
   
  tv_rgb=(TextView)this.findViewById(R.id.tv_rgb);
   
  ColorPickerView cpv=(ColorPickerView)this.findViewById(R.id.cpv);
  cpv.setOnColorChangedListenner(new OnColorChangedListener() {
   /**
    * 手指抬起,选定颜色时
    */
   @Override
   public void onColorChanged(int r, int g, int b) {
    if(r==0 && g==0 && b==0){
     return;
    }
    Toast.makeText(MyViewActivity.this, "选取 RGB:"+r+","+g+","+b, Toast.LENGTH_SHORT).show();
   }
 
   /**
    * 颜色移动的时候
    */
   @Override
   public void onMoveColor(int r, int g, int b) {
    if(r==0 && g==0 && b==0){
     return;
    }
    tv_rgb.setText("RGB:"+r+","+g+","+b);
   }
  });
 }
}

详细项目代码:

源码下载:Android实现颜色选取圆盘

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/ZhengJingLe/article/details/50884444

延伸 · 阅读

精彩推荐
  • AndroidAndroid实现Service获取当前位置(GPS+基站)的方法

    Android实现Service获取当前位置(GPS+基站)的方法

    这篇文章主要介绍了Android实现Service获取当前位置(GPS+基站)的方法,较为详细的分析了Service基于GPS位置的技巧,具有一定参考借鉴价值,需要的朋友可以参考下...

    Ruthless8342021-03-31
  • AndroidAndroid实现固定屏幕显示的方法

    Android实现固定屏幕显示的方法

    这篇文章主要介绍了Android实现固定屏幕显示的方法,实例分析了Android屏幕固定显示所涉及的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...

    鉴客6192021-03-27
  • AndroidAndroid界面效果UI开发资料汇总(附资料包)

    Android界面效果UI开发资料汇总(附资料包)

    android ui界面设计,友好的界面会提高用户体验度;同时也增强了android ui界面设计的难度,本文提供了一些常用开发资料(有下载哦)感兴趣的朋友可以了解下...

    Android开发网4672021-01-03
  • Android汇总Android视频录制中常见问题

    汇总Android视频录制中常见问题

    这篇文章主要汇总了Android视频录制中常见问题,帮助大家更好地解决Android视频录制中常见的问题,需要的朋友可以参考下...

    yh_thu5192021-04-28
  • AndroidAndroid中AsyncTask详细介绍

    Android中AsyncTask详细介绍

    这篇文章主要介绍了Android中AsyncTask详细介绍,AsyncTask是一个很常用的API,尤其异步处理数据并将数据应用到视图的操作场合,需要的朋友可以参考下...

    Android开发网7452021-03-11
  • AndroidAndroid编程解析XML方法详解(SAX,DOM与PULL)

    Android编程解析XML方法详解(SAX,DOM与PULL)

    这篇文章主要介绍了Android编程解析XML方法,结合实例形式详细分析了Android解析XML文件的常用方法与相关实现技巧,需要的朋友可以参考下...

    liuhe68810052021-05-03
  • AndroidAndroid CardView+ViewPager实现ViewPager翻页动画的方法

    Android CardView+ViewPager实现ViewPager翻页动画的方法

    本篇文章主要介绍了Android CardView+ViewPager实现ViewPager翻页动画的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    Abby代黎明9602022-03-02
  • AndroidAndroid程序设计之AIDL实例详解

    Android程序设计之AIDL实例详解

    这篇文章主要介绍了Android程序设计的AIDL,以一个完整实例的形式较为详细的讲述了AIDL的原理及实现方法,需要的朋友可以参考下...

    Android开发网4642021-03-09