电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

Android Studio設置顏色拾色器工具Color Picker教程

瀏覽:86日期:2022-09-26 13:58:19

你可能下載過一些獲取顏色值的一些小工具,

這種方式獲取顏色,需要先切換窗口轉跳到桌面,然后打開拾色器小工具程序,然后去拾取顏色;

你可能也知道Android Studio自帶一個顏色拾色器,通過下面這種方式才能找到

這種方式獲取顏色值限制性很強,需要特定的窗口,需要點擊那么一小塊顏色才能彈出窗口,才能使用

Android Studio設置顏色拾色器工具Color Picker教程

那有沒有更好的方式? 答案是肯定的,不然這些個干嘛~~

不用向上面那樣去打開拾色器小工具程序,不用在特定的窗口點擊特定的位置彈出拾色器工具,是用我們最喜歡的快捷鍵的方式打開

Android Studio自帶了顏色拾色器工具,但是它沒有設置快捷鍵,也沒有告訴我們, 這才是問題,

不羅嗦了,跟著下面的設置去設置快捷鍵吧

Android Studio設置顏色拾色器工具Color Picker教程

Android Studio設置顏色拾色器工具Color Picker教程

設置好之后Apply應用 + OK確認下就好了

下面就能愉快的玩耍了,Alt+C~~

Android Studio設置顏色拾色器工具Color Picker教程

補充知識:Android 自定義一個簡版的取色器ColorPicker

最近在一個項目中要用到修改顏色功能,于是搜索了一波android自定義取色器,雖然搜索結果很多,但是都不是令人十分滿意(可能是用久了AndroidStudio自帶取色器的原因,真的是太好用了有沒有?)。

既然這么喜歡AS的調色板,何不自己擼一個?心動不如行動,馬上動手!

常規操作,先上效果圖,簡版取色器效果如下:

Android Studio設置顏色拾色器工具Color Picker教程

項目地址:

https://github.com/shixiuwen/colorpicker

布局單位使用的是dp,沒有做其他過多適配操作,程序的源碼很簡單,可以直接clone下來修改成自己想要的效果或者做其他定制操作,直接使用的話,集成參考如下:

Step 1. Add the JitPack repository to your build file

//在根 build.gradle 中添加allprojects { repositories { ... maven { url ’https://jitpack.io’ } } }

Step 2. Add the dependency

//在模塊 build.gradle 中添加dependencies { //v1.0.3是版本號,博客不會經常更新,最新版本見github implementation ’com.github.shixiuwen:colorpicker:v1.0.3’ }

調用示例:

// .xml文件中<com.shixia.colorpickerview.ColorPickerView android: android:layout_width='match_parent' android:layout_height='wrap_content' />

// Activity中final TextView tvTest = findViewById(R.id.tv_test);ColorPickerView colorPicker = findViewById(R.id.cpv_color_picker);//對控件進行回調監聽,獲取顏色值colorcolorPicker.setOnColorChangeListener(new OnColorChangeListener() { @Override public void colorChanged(int color) { tvTest.setBackgroundColor(color); }});

該控件的顏色變化過程是通過觀察AndroidStudio取色板顏色變化規律而得到的,因為項目沒有其他要求,所以目前沒有提供其他公開方法可以供外部調用,有這方面需求的可以自己把庫下載下來自動修改,有修改困難的可以郵件聯系。另外,如果后面該庫會收到幾個start的話會考慮加一些其他功能,比如 布局適配、顏色初始化功能、常用顏色記錄功能…… 等等。

以下是關鍵類代碼:

package com.shixia.colorpickerview;import android.content.Context;import android.graphics.Color;import android.graphics.drawable.GradientDrawable;import android.util.AttributeSet;import android.view.LayoutInflater;import android.view.MotionEvent;import android.view.View;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.RelativeLayout;public class ColorPickerView extends LinearLayout { private final View llColorProgress; private final View vColorBar; private final View rlTransBar; private final View vTransBar; private final RelativeLayout.LayoutParams transBarLayoutParams; private int red = 255, green = 0, blue = 0; private int index = 0; private ColorPreviewView cpvColorPreview; private View vLocation; private View vBgColor; private final RelativeLayout.LayoutParams colorBarLayoutParams; private int transValue = 255; //透明度 private final ImageView vTransPreview; private OnColorChangeListener onColorChangeListener; private RelativeLayout.LayoutParams vLocationLayoutParams; public ColorPickerView(Context context, AttributeSet attrs) { super(context, attrs); View view = LayoutInflater.from(context).inflate(R.layout.view_color_picker, this); vBgColor = view.findViewById(R.id.fl_color); vLocation = view.findViewById(R.id.view_location); vLocationLayoutParams = (RelativeLayout.LayoutParams) vLocation.getLayoutParams(); llColorProgress = findViewById(R.id.ll_color_progress); cpvColorPreview = view.findViewById(R.id.cpv_color_preview); vColorBar = view.findViewById(R.id.view_color_bar); colorBarLayoutParams = (RelativeLayout.LayoutParams) vColorBar.getLayoutParams(); rlTransBar = view.findViewById(R.id.rl_trans_bar); vTransBar = view.findViewById(R.id.view_trans_bar); transBarLayoutParams = (RelativeLayout.LayoutParams) vTransBar.getLayoutParams(); vTransPreview = view.findViewById(R.id.view_trans_preview); /*調整顏色*/ llColorProgress.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { int action = event.getAction(); int width = llColorProgress.getWidth(); switch (action) { case MotionEvent.ACTION_DOWN: break; case MotionEvent.ACTION_MOVE: break; case MotionEvent.ACTION_UP: break; } float leftMargin = event.getX(); float x = 0; if (leftMargin < vColorBar.getWidth() / 2) { colorBarLayoutParams.leftMargin = 0; } else if (leftMargin > width - vColorBar.getWidth() / 2) { x = 100; colorBarLayoutParams.leftMargin = width - vColorBar.getWidth(); } else { x = event.getX() / width * 100; colorBarLayoutParams.leftMargin = (int) (leftMargin - vColorBar.getWidth() / 2); } vColorBar.setLayoutParams(colorBarLayoutParams); onProgressChanged((int) x); return true; } }); /*調整透明度*/ rlTransBar.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { int action = event.getAction(); int width = rlTransBar.getWidth(); switch (action) { case MotionEvent.ACTION_DOWN: break; case MotionEvent.ACTION_MOVE: break; case MotionEvent.ACTION_UP: break; } float leftMargin = event.getX(); float x = 0; if (leftMargin < vTransBar.getWidth() / 2) { transBarLayoutParams.leftMargin = 0; } else if (leftMargin > width - vTransBar.getWidth() / 2) { x = 100; transBarLayoutParams.leftMargin = width - vTransBar.getWidth(); } else { x = event.getX() / width * 100; transBarLayoutParams.leftMargin = (int) (leftMargin - vTransBar.getWidth() / 2); } vTransBar.setLayoutParams(transBarLayoutParams); changeTransparency((int) x); return true; } }); /*調整顏色明暗*/ vBgColor.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { int width = vBgColor.getWidth(); int height = vBgColor.getHeight(); int action = event.getAction(); int leftMargin; int topMargin; switch (action) { case MotionEvent.ACTION_DOWN: break; case MotionEvent.ACTION_MOVE: //防止越界處理 if (event.getX() > width - vLocation.getWidth() / 2F) { leftMargin = width - vLocation.getWidth(); } else if (event.getX() < vLocation.getWidth() / 2F) { leftMargin = 0; } else { leftMargin = (int) (event.getX() - vLocation.getWidth() / 2F); } if (event.getY() > height - vLocation.getHeight() / 2F) { topMargin = height - vLocation.getHeight(); } else if (event.getY() <= vLocation.getHeight() / 2F) { topMargin = 0; } else { topMargin = (int) (event.getY() - vLocation.getHeight() / 2F); } vLocationLayoutParams.leftMargin = leftMargin; vLocationLayoutParams.topMargin = topMargin; vLocation.setLayoutParams(vLocationLayoutParams); changeColor(); break; case MotionEvent.ACTION_UP: break; } return true; } }); } /** * 顏色值調整 * * @param progressColor */ private void onProgressChanged(int progressColor) { red = 0; green = 0; blue = 0; index = (int) (progressColor / (100 / 6F)); float v = progressColor % (100 / 6F) / (100 / 6F); switch (index) { case 0: //紅<-->中--綠 red = 255; green = (int) (255 * v); break; case 1://紅--中<-->綠 red = (int) (255 * (1 - v)); green = 255; break; case 2: //綠<-->中--藍 green = 255; blue = (int) (255 * v); break; case 3://綠--中<-->藍 green = (int) (255 * (1 - v)); blue = 255; break; case 4: //藍<-->中--紅 blue = 255; red = (int) (255 * v); break; case 5://藍--中<-->紅 blue = (int) (255 * (1 - v)); red = 255; break; default: red = 255; break; } vBgColor.setBackgroundColor(Color.rgb(red, green, blue)); changeColor(); } /** * 顏色明暗度調整 */ private void changeColor() { int tempRed = red; int tempGreen = green; int tempBlue = blue; float hPercent = 1 - (vLocation.getX() / (vBgColor.getWidth() - vLocation.getWidth())); float vPercent = vLocation.getY() / (vBgColor.getHeight() - vLocation.getHeight()); switch (index) { case 0: tempGreen = (int) (green + hPercent * (255 - green)); tempBlue = (int) (blue + hPercent * (255 - blue)); break; case 1: tempRed = (int) (red + hPercent * (255 - red)); tempBlue = (int) (blue + hPercent * (255 - blue)); break; case 2: tempRed = (int) (red + hPercent * (255 - red)); tempBlue = (int) (blue + hPercent * (255 - blue)); break; case 3: tempRed = (int) (red + hPercent * (255 - red)); tempGreen = (int) (green + hPercent * (255 - green)); break; case 4: tempRed = (int) (red + hPercent * (255 - red)); tempGreen = (int) (green + hPercent * (255 - green)); break; case 5: case 6: tempGreen = (int) (green + hPercent * (255 - green)); tempBlue = (int) (blue + hPercent * (255 - blue)); break; } tempRed = (int) (tempRed - tempRed * vPercent); tempGreen = (int) (tempGreen - tempGreen * vPercent); tempBlue = (int) (tempBlue - tempBlue * vPercent); int color = Color.argb(transValue, tempRed, tempGreen, tempBlue); cpvColorPreview.setColor(color); if (onColorChangeListener != null) { onColorChangeListener.colorChanged(color); } int[] gradientColor = {Color.argb(0, 0, 0, 0), Color.rgb(tempRed, tempGreen, tempBlue)}; GradientDrawable drawable = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT, gradientColor); vTransPreview.setBackground(drawable); } /** * 改變透明度 * * @param progress */ private void changeTransparency(int progress) { transValue = (int) (progress / 100F * 255); int color = Color.argb(transValue, red, green, blue); cpvColorPreview.setColor(color); if (onColorChangeListener != null) { onColorChangeListener.colorChanged(color); } } @Override public void onWindowFocusChanged(boolean hasWindowFocus) { super.onWindowFocusChanged(hasWindowFocus); RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) vLocation.getLayoutParams(); layoutParams.leftMargin = vBgColor.getWidth() - vLocation.getWidth(); vLocation.setLayoutParams(layoutParams); colorBarLayoutParams.leftMargin = llColorProgress.getWidth() - vColorBar.getWidth(); vColorBar.setLayoutParams(colorBarLayoutParams); transBarLayoutParams.leftMargin = rlTransBar.getWidth() - vTransBar.getWidth(); vTransBar.setLayoutParams(transBarLayoutParams); int[] color = {Color.argb(0, 0, 0, 0), Color.rgb(255, 0, 0)}; GradientDrawable drawable = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT, color); vTransPreview.setBackground(drawable); } /** * 設置該方法,顏色改變的時候會回調顏色值 * * @param onColorChangeListener */ public void setOnColorChangeListener(OnColorChangeListener onColorChangeListener) { this.onColorChangeListener = onColorChangeListener; }}

以上這篇Android Studio設置顏色拾色器工具Color Picker教程就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Android
相關文章:
主站蜘蛛池模板: 北京自然绿环境科技发展有限公司专业生产【洗车机_加油站洗车机-全自动洗车机】 | 密封无忧网 _ 专业的密封产品行业信息网| 天一线缆邯郸有限公司_煤矿用电缆厂家_矿用光缆厂家_矿用控制电缆_矿用通信电缆-天一线缆邯郸有限公司 | 原色会计-合肥注册公司_合肥代理记账公司_营业执照代办 | 废旧物资回收公司_广州废旧设备回收_报废设备物资回收-益美工厂设备回收公司 | 长沙印刷厂-包装印刷-画册印刷厂家-湖南省日大彩色印务有限公司 青州搬家公司电话_青州搬家公司哪家好「鸿喜」青州搬家 | 新能源汽车教学设备厂家报价[汽车教学设备运营18年]-恒信教具 | 透平油真空滤油机-变压器油板框滤油机-滤油车-华之源过滤设备 | 北京包装设计_标志设计公司_包装设计公司-北京思逸品牌设计 | 传爱自考网_传爱自学考试网| 磁棒电感生产厂家-电感器厂家-电感定制-贴片功率电感供应商-棒形电感生产厂家-苏州谷景电子有限公司 | 背压阀|减压器|不锈钢减压器|减压阀|卫生级背压阀|单向阀|背压阀厂家-上海沃原自控阀门有限公司 本安接线盒-本安电路用接线盒-本安分线盒-矿用电话接线盒-JHH生产厂家-宁波龙亿电子科技有限公司 | 云杂志网-学术期刊-首页| 北京易通慧公司从事北京网站优化,北京网络推广、网站建设一站式服务商-北京网站优化公司 | 丹佛斯变频器-丹佛斯压力开关-变送器-广州市风华机电设备有限公司 | 美国HASKEL增压泵-伊莱科elettrotec流量开关-上海方未机械设备有限公司 | 薪动-人力资源公司-灵活用工薪资代发-费用结算-残保金优化-北京秒付科技有限公司 | TwistDx恒温扩增-RAA等温-Jackson抗体-默瑞(上海)生物科技有限公司 | 企典软件一站式企业管理平台,可私有、本地化部署!在线CRM客户关系管理系统|移动办公OA管理系统|HR人事管理系统|人力 | 热缩管切管机-超声波切带机-织带切带机-无纺布切布机-深圳市宸兴业科技有限公司 | 真空泵厂家_真空泵机组_水环泵_旋片泵_罗茨泵_耐腐蚀防爆_中德制泵 | 岩棉板|岩棉复合板|聚氨酯夹芯板|岩棉夹芯板|彩钢夹芯板-江苏恒海钢结构 | 脉冲布袋除尘器_除尘布袋-泊头市净化除尘设备生产厂家 | 经济师考试_2025中级经济师报名时间_报名入口_考试时间_华课网校经济师培训网站 | 10吨无线拉力计-2吨拉力计价格-上海佳宜电子科技有限公司 | 食安观察网| 外观设计_设备外观设计_外观设计公司_产品外观设计_机械设备外观设计_东莞工业设计公司-意品深蓝 | CE认证_FCC认证_CCC认证_MFI认证_UN38.3认证-微测检测 CNAS实验室 | 旋振筛|圆形摇摆筛|直线振动筛|滚筒筛|压榨机|河南天众机械设备有限公司 | 碳化硅,氮化硅,冰晶石,绢云母,氟化铝,白刚玉,棕刚玉,石墨,铝粉,铁粉,金属硅粉,金属铝粉,氧化铝粉,硅微粉,蓝晶石,红柱石,莫来石,粉煤灰,三聚磷酸钠,六偏磷酸钠,硫酸镁-皓泉新材料 | 宝鸡市人民医院| 金联宇电缆总代理-金联宇集团-广东金联宇电缆实业有限公司 | 棉柔巾代加工_洗脸巾oem_一次性毛巾_浴巾生产厂家-杭州禾壹卫品科技有限公司 | 爆破器材运输车|烟花爆竹运输车|1-9类危险品厢式运输车|湖北江南专用特种汽车有限公司 | 手术室净化厂家_成都实验室装修公司_无尘车间施工单位_洁净室工程建设团队-四川华锐16年行业经验 | 衢州装饰公司|装潢公司|办公楼装修|排屋装修|别墅装修-衢州佳盛装饰 | 广东佛电电器有限公司|防雷开关|故障电弧断路器|智能量测断路器 广东西屋电气有限公司-广东西屋电气有限公司 | 邢台人才网_邢台招聘网_邢台123招聘【智达人才网】 | 东莞韩创-专业绝缘骨架|马达塑胶零件|塑胶电机配件|塑封电机骨架厂家 | 北京网站建设公司_北京网站制作公司_北京网站设计公司-北京爱品特网站建站公司 | 螺旋压榨机-刮泥机-潜水搅拌机-电动泥斗-潜水推流器-南京格林兰环保设备有限公司 |