安卓進(jìn)階第八篇之根據(jù)手指觸摸的位置獲取圖片的顏色值

這兩天整理了一個(gè)自己項(xiàng)目中用到的獲取圖片顏色的效果鸵熟,話不多說(shuō)副编,先看下效果:

</br>


效果圖
如上,就是根據(jù)手指觸摸的位置獲取顏色值旅赢,色盤是一張圖片齿桃,代碼也很簡(jiǎn)單,如下:

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout    
xmlns:android="http://schemas.android.com/apk/res/android"    
android:id="@+id/activity_main"    
android:layout_width="match_parent"    
android:layout_height="match_parent"    >    
<FrameLayout        
android:id="@+id/frame_layout"        
android:layout_width="wrap_content"        
android:layout_height="wrap_content"        
android:layout_centerInParent="true"/>    
<Button        
android:id="@+id/btn"        
android:layout_width="match_parent"        
android:layout_height="45dp"        
android:text="選擇的顏色"        
android:layout_alignParentBottom="true"/>
</RelativeLayout>

MainActivity :

public class MainActivity extends AppCompatActivity {    
private static final String TAG = "MainActivity";    
private FrameLayout frame_layout;    
private Button btn;    
private GetPictureColorView mGetPictureColorView;    
@Override    
protected void onCreate(Bundle savedInstanceState) {        
super.onCreate(savedInstanceState);        
setContentView(R.layout.activity_main);        
initView();        
init();    
}    
private void initView() {       
frame_layout = (FrameLayout) findViewById(R.id.frame_layout);        
btn = (Button) findViewById(R.id.btn);    
}    
private void init() {        
mGetPictureColorView = new GetPictureColorView(MainActivity.this);        
frame_layout.addView(mGetPictureColorView);        
mGetPictureColorView.setOnUpdateColorListener(new 
GetPictureColorView.OnUpdateColorListener() {            
@Override            
public void changeColor(int color) {                
btn.setBackgroundColor(color);            
}        
});    
}

GetPictureColorView:自定義view獲取圖片顏色

public class GetPictureColorView extends View {    
private static final String TAG = "GetPictureColorView";    
private Paint mPaint;    
private Bitmap bigIcon, smallIcon;    
private int radius;    
private int size;    
private int color = Color.WHITE;//顏色煮盼,默認(rèn)為白色    
public OnUpdateColorListener onUpdateColorListener;    
private Bitmap mBackgroundBitmap;    
private Paint paint;    
private float nowX, nowY;    
private ColorBean bean;    
private int nowAction;    
public interface OnUpdateColorListener {        
void changeColor(int color);    
}    
public void setOnUpdateColorListener(OnUpdateColorListener mOnUpdateColorListener) {        
this.onUpdateColorListener = mOnUpdateColorListener;    
}    
public GetPictureColorView(Context context) {        
super(context, null);        
init();        
initView();    
}    
public GetPictureColorView(Context context, AttributeSet attrs) {        
super(context, attrs);        
init();       
initView();    
}    
private void init() {        
paint = new Paint();        
mPaint = new Paint();        
mPaint.setAntiAlias(true);        
paint.setAntiAlias(true);    
}    
private void initView() {        
mBackgroundBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.light_group_bg);        
size = Math.min(mBackgroundBitmap.getWidth(), 
mBackgroundBitmap.getHeight());        
radius = size / 2;        
bigIcon = BitmapFactory.decodeResource(getResources(), R.drawable.positioning_big_target);        
smallIcon = BitmapFactory.decodeResource(getResources(), R.drawable.positioning_target);        
setData();    
}    
private void setData() {        
bean = new ColorBean();        
Integer col = Color.WHITE;        
bean.x = radius;        
bean.y = radius;        
bean.color = col;        
bean.type = true;        
nowX = bean.x;        
nowY = bean.y;        
color = bean.color;    
}    
public GetPictureColorView(Context context, AttributeSet attrs, int defStyleAttr) {        
super(context, attrs, defStyleAttr);    
}    
@Override    
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {       
setMeasuredDimension(size, size);    
}    
@Override    protected void onDraw(Canvas canvas) {        
//畫背景色盤        
canvas.drawBitmap(mBackgroundBitmap, 0, 0, mPaint);        
if (bean.type && (Math.hypot(nowX - radius, nowY - radius) < (radius) * 8 / 10)) {            
bean.x = (int) nowX;            
bean.y = (int) nowY;            
canvas.drawBitmap(bigIcon, bean.x - bigIcon.getWidth() / 2, bean.y - bigIcon.getHeight() / 2, mPaint);        
} else {            
canvas.drawBitmap(smallIcon, bean.x - smallIcon.getWidth() / 2, bean.y - smallIcon.getHeight() / 2, mPaint);        
}        
if (nowAction == MotionEvent.ACTION_UP || nowAction == MotionEvent.ACTION_CANCEL) {            
if (onUpdateColorListener != null) {         
onUpdateColorListener.changeColor(color);            
}        
}    
}    
@Override    
public boolean onTouchEvent(MotionEvent event) {        
nowX = event.getX();        
nowY = event.getY();        
//手指按下        
if (event.getAction() == MotionEvent.ACTION_DOWN) {            
if (Math.hypot(nowX - radius, nowY - radius) < (radius) * 8 / 10) {                
bean.type = true;                
int pixel = mBackgroundBitmap.getPixel((int) event.getX(), (int) event.getY());                
color = pixel;                
bean.color = pixel;            
} else {                
return true;            
}        
}        
//手指滑動(dòng)或者離開(kāi)        
else if (event.getAction() == MotionEvent.ACTION_MOVE || event.getAction() == MotionEvent.ACTION_UP) {            
if ((Math.hypot(nowX - radius, nowY - radius) < (radius) * 7 / 10)) {                
int pixel = mBackgroundBitmap.getPixel((int) event.getX(), (int) 
event.getY());                
color = pixel;                
bean.color = pixel;            
} else {                
return true;            
}        
} else {            
return true;        
}        
nowAction = event.getAction();        
nowX = event.getX();        
nowY = event.getY();        
invalidate();        
return true;    
}    
private class ColorBean {        
boolean type = false;//是否點(diǎn)擊        
int color;        
int x, y;//位置    
}
}
代碼就這么多,其中最主要的還是Bitmap.getPixel(int x,int y)這個(gè)方法带污,參數(shù)x,y就是你手指點(diǎn)擊的位置僵控,這個(gè)位置一定要在圖片的范圍之內(nèi),返回值就是圖片當(dāng)前觸摸點(diǎn)的顏色值鱼冀,怎么樣报破,是不是挺簡(jiǎn)單的呢,喜歡就一下贊千绪,謝謝啦3湟住!荸型!
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末盹靴,一起剝皮案震驚了整個(gè)濱河市括改,隨后出現(xiàn)的幾起案子睬捶,更是在濱河造成了極大的恐慌区转,老刑警劉巖臀突,帶你破解...
    沈念sama閱讀 216,402評(píng)論 6 499
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件右冻,死亡現(xiàn)場(chǎng)離奇詭異绢慢,居然都是意外死亡婴梧,警方通過(guò)查閱死者的電腦和手機(jī)矗积,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,377評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門蔓倍,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)悬钳,“玉大人,你說(shuō)我怎么就攤上這事偶翅∧矗” “怎么了?”我有些...
    開(kāi)封第一講書人閱讀 162,483評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵倒堕,是天一觀的道長(zhǎng)灾测。 經(jīng)常有香客問(wèn)我,道長(zhǎng),這世上最難降的妖魔是什么媳搪? 我笑而不...
    開(kāi)封第一講書人閱讀 58,165評(píng)論 1 292
  • 正文 為了忘掉前任铭段,我火速辦了婚禮,結(jié)果婚禮上秦爆,老公的妹妹穿的比我還像新娘序愚。我一直安慰自己,他們只是感情好等限,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,176評(píng)論 6 388
  • 文/花漫 我一把揭開(kāi)白布爸吮。 她就那樣靜靜地躺著,像睡著了一般望门。 火紅的嫁衣襯著肌膚如雪形娇。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書人閱讀 51,146評(píng)論 1 297
  • 那天筹误,我揣著相機(jī)與錄音桐早,去河邊找鬼。 笑死厨剪,一個(gè)胖子當(dāng)著我的面吹牛哄酝,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播祷膳,決...
    沈念sama閱讀 40,032評(píng)論 3 417
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼陶衅,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了直晨?” 一聲冷哼從身側(cè)響起搀军,我...
    開(kāi)封第一講書人閱讀 38,896評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎抡秆,沒(méi)想到半個(gè)月后奕巍,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,311評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡儒士,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,536評(píng)論 2 332
  • 正文 我和宋清朗相戀三年的止,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片着撩。...
    茶點(diǎn)故事閱讀 39,696評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡诅福,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出拖叙,到底是詐尸還是另有隱情氓润,我是刑警寧澤,帶...
    沈念sama閱讀 35,413評(píng)論 5 343
  • 正文 年R本政府宣布薯鳍,位于F島的核電站咖气,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜崩溪,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,008評(píng)論 3 325
  • 文/蒙蒙 一浅役、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧伶唯,春花似錦觉既、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 31,659評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至粹断,卻和暖如春符欠,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背姿染。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 32,815評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工背亥, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人悬赏。 一個(gè)月前我還...
    沈念sama閱讀 47,698評(píng)論 2 368
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像娄徊,于是被迫代替她去往敵國(guó)和親闽颇。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,592評(píng)論 2 353

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,071評(píng)論 25 707
  • ¥開(kāi)啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開(kāi)一個(gè)線程寄锐,因...
    小菜c閱讀 6,401評(píng)論 0 17
  • 一兵多、Android開(kāi)發(fā)初體驗(yàn) 二、Android與MVC設(shè)計(jì)模式模型對(duì)象存儲(chǔ)著應(yīng)用的數(shù)據(jù)和業(yè)務(wù)邏輯橄仆。模型類通常用來(lái)...
    為夢(mèng)想戰(zhàn)斗閱讀 885評(píng)論 0 3
  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理剩膘,服務(wù)發(fā)現(xiàn),斷路器盆顾,智...
    卡卡羅2017閱讀 134,651評(píng)論 18 139
  • 妻問(wèn)讀《水滸》何感怠褐,我回復(fù)讀后只想大口吃肉大口喝酒。中文專業(yè)的我您宪,讀名著卻不以文字為意奈懒,實(shí)屬不務(wù)正業(yè)∠芫蓿可這確是我真...
    走不了閱讀 384評(píng)論 2 3