版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
【移動應(yīng)用開發(fā)技術(shù)】AndroidStudio怎么設(shè)置顏色拾色器工具ColorPicker
這篇文章主要介紹AndroidStudio怎么設(shè)置顏色拾色器工具ColorPicker,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!Android是一種基于Linux內(nèi)核的自由及開放源代碼的操作系統(tǒng),主要使用于移動設(shè)備,如智能手機和平板電腦,由美國Google公司和開放手機聯(lián)盟領(lǐng)導(dǎo)及開發(fā)。你可能下載過一些獲取顏色值的一些小工具,這種方式獲取顏色,需要先切換窗口轉(zhuǎn)跳到桌面,然后打開拾色器小工具程序,然后去拾取顏色;你可能也知道AndroidStudio自帶一個顏色拾色器,通過下面這種方式才能找到這種方式獲取顏色值限制性很強,需要特定的窗口,需要點擊那么一小塊顏色才能彈出窗口,才能使用那有沒有更好的方式?答案是肯定的,不然這些個干嘛~~不用向上面那樣去打開拾色器小工具程序,不用在特定的窗口點擊特定的位置彈出拾色器工具,是用我們最喜歡的快捷鍵的方式打開AndroidStudio自帶了顏色拾色器工具,但是它沒有設(shè)置快捷鍵,也沒有告訴我們,這才是問題,不羅嗦了,跟著下面的設(shè)置去設(shè)置快捷鍵吧設(shè)置好之后Apply應(yīng)用+OK確認下就好了下面就能愉快的玩耍了,Alt+C~~補充知識:Android自定義一個簡版的取色器ColorPicker最近在一個項目中要用到修改顏色功能,于是搜索了一波android自定義取色器,雖然搜索結(jié)果很多,但是都不是令人十分滿意(可能是用久了AndroidStudio自帶取色器的原因,真的是太好用了有沒有?)。既然這么喜歡AS的調(diào)色板,何不自己擼一個?心動不如行動,馬上動手!常規(guī)操作,先上效果圖,簡版取色器效果如下:項目地址:/shixiuwen/colorpicker布局單位使用的是dp,沒有做其他過多適配操作,程序的源碼很簡單,可以直接clone下來修改成自己想要的效果或者做其他定制操作,直接使用的話,集成參考如下:Step1.AddtheJitPackrepositorytoyourbuildfile//在根
build.gradle
中添加
allprojects
{
repositories
{
...
maven
{
url
'https://jitpack.io'
}
}
}Step2.Addthedependency//在模塊
build.gradle
中添加
dependencies
{
//v1.0.3是版本號,博客不會經(jīng)常更新,最新版本見github
implementation
'com.github.shixiuwen:colorpicker:v1.0.3'
}調(diào)用示例://
.xml文件中
<com.shixia.colorpickerview.ColorPickerView
android:id="@+id/cpv_color"
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);
//對控件進行回調(diào)監(jiān)聽,獲取顏色值color
colorPicker.setOnColorChangeListener(new
OnColorChangeListener()
{
@Override
public
void
colorChanged(int
color)
{
tvTest.setBackgroundColor(color);
}
});該控件的顏色變化過程是通過觀察AndroidStudio取色板顏色變化規(guī)律而得到的,因為項目沒有其他要求,所以目前沒有提供其他公開方法可以供外部調(diào)用,有這方面需求的可以自己把庫下載下來自動修改,有修改困難的可以郵件聯(lián)系。另外,如果后面該庫會收到幾個start的話會考慮加一些其他功能,比如布局適配、顏色初始化功能、常用顏色記錄功能……等等。以下是關(guān)鍵類代碼: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);
/*調(diào)整顏色*/
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;
}
});
/*調(diào)整透明度*/
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;
}
});
/*調(diào)整顏色明暗*/
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;
}
});
}
/**
*
顏色值調(diào)整
*
*
@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();
}
/**
*
顏色明暗度調(diào)整
*/
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)
{
on
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 南京交安考試試題及答案
- 系統(tǒng)工程師考試題及答案
- 呼和浩特安全員b證考試題及答案
- 農(nóng)村信用社筆試試題及答案
- 黨紀知識競賽題庫及答案
- 質(zhì)檢員專業(yè)管理實務(wù)復(fù)習(xí)模擬試題及答案
- 重慶中職計算機題庫及答案
- 鐵路職業(yè)技能鑒定試題預(yù)測試卷附答案詳解
- 醫(yī)技三基三嚴模考試題+答案
- 保育員高級理論知識試卷及答案2
- 中華人民共和國職業(yè)分類大典是(專業(yè)職業(yè)分類明細)
- 2025年中考英語復(fù)習(xí)必背1600課標詞匯(30天記背)
- 資產(chǎn)管理部2025年工作總結(jié)與2025年工作計劃
- 科技成果轉(zhuǎn)化技術(shù)平臺
- 下腔靜脈濾器置入術(shù)的護理查房
- 基建人員考核管理辦法
- 2025體育與健康課程標準深度解讀與教學(xué)實踐
- 礦山救援器材管理制度
- 2025西南民族大學(xué)輔導(dǎo)員考試試題及答案
- T/CSPSTC 17-2018企業(yè)安全生產(chǎn)雙重預(yù)防機制建設(shè)規(guī)范
- 2025年《三級物業(yè)管理師》考試復(fù)習(xí)題(含答案)
評論
0/150
提交評論