怎么在Android中自定義view實(shí)現(xiàn)圖片選色器?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。
創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比永吉網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式永吉網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋永吉地區(qū)。費(fèi)用合理售后完善,10余年實(shí)體公司更值得信賴。
在工程的 build.gradle中加入:
allprojects { repositories { ... maven { url "https://jitpack.io" } } }
module的build.gradle中加入依賴:
dependencies { compile 'com.github.autume:ColorPickerView:1.0' }
xml
<RelativeLayout android:id="@+id/rl_picker" android:layout_below="@+id/img_color" android:layout_marginTop="30dp" android:layout_centerHorizontal="true" android:layout_width="wrap_content" android:layout_height="wrap_content"> <colorpickerview.oden.com.colorpicker.ColorPickerView android:id="@+id/color_picker" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <ImageView android:id="@+id/img_picker" android:layout_centerInParent="true" android:src="@mipmap/color_picker" android:layout_width="25dp" android:layout_height="25dp" /> </RelativeLayout>
選色代碼
private void initRgbPicker() { colorPickerView = (ColorPickerView) findViewById(R.id.color_picker); colorPickerView.setImgPicker(MainActivity.this, img_picker, 25); //最后一個(gè)參數(shù)是該顏色指示圈的大小(dp) colorPickerView.setColorChangedListener(new ColorPickerView.onColorChangedListener() { @Override public void colorChanged(int red, int blue, int green) { img_color.setColorFilter(Color.argb(255, red, green, blue)); } @Override public void stopColorChanged(int red, int blue, int green) { } }); }
對(duì)外公開的API
public void setImgPicker(final Context context, final ImageView imgPicker, final int pickerViewWidth) public void setImgResource(final int imgResource) public void setColorChangedListener(onColorChangedListener colorChangedListener)
實(shí)現(xiàn)過(guò)程
attrs屬性
可通過(guò)picture_resource屬性設(shè)置用來(lái)選色的資源id,現(xiàn)僅支持圓形圖片
<declare-styleable name="ColorPickerView"> <attr name="picture_resource" format="reference"/> </declare-styleable>
xml
布局中就是放入一個(gè)ImageView控件
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/rl_root" tools:background="@color/black" android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/img_color_rang" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:src="@mipmap/lights_colors" /> </RelativeLayout>
屬性獲取及view初始化
private void initAttrs(Context context, AttributeSet attrs) { if (null != attrs) { TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ColorPickerView); imgResource = typedArray.getResourceId(R.styleable.ColorPickerView_picture_resource, 0); typedArray.recycle(); } } private void initView(Context context) { View view = LayoutInflater.from(context).inflate(R.layout.color_picker, this); imgColorRang = (ImageView) view.findViewById(R.id.img_color_rang); rl_root = (RelativeLayout) view.findViewById(R.id.rl_root); if (imgResource != 0) imgColorRang.setImageResource(imgResource); bitmap = ((BitmapDrawable) imgColorRang.getDrawable()).getBitmap();//獲取圓盤圖片 }
顏色回調(diào)監(jiān)聽
private onColorChangedListener colorChangedListener;//顏色變換監(jiān)聽 public void setColorChangedListener(onColorChangedListener colorChangedListener) { this.colorChangedListener = colorChangedListener; } /** * 顏色變換監(jiān)聽接口 */ public interface onColorChangedListener { void colorChanged(int red, int blue, int green); void stopColorChanged(int red, int blue, int green); }
觸摸事件
觸摸事件寫在父控件上,可以統(tǒng)一處理用來(lái)選色的view及指示選色位置的view(imgPicker),imgPicker為指示顯示位置的圓框,若設(shè)置了則跟隨手指移動(dòng)。
private void initTouchListener() { rl_root.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (range_radius == 0) { range_radius = imgColorRang.getWidth() / 2; //圓盤半徑 centreX = imgColorRang.getRight() - range_radius; centreY = imgColorRang.getBottom() - imgColorRang.getHeight() / 2; select_radius = range_radius - pickerViewPadding/5; } float xInView = event.getX(); float yInView = event.getY(); Log.d(TAG, "xInView: " + xInView + ",yInView: " + yInView + ",left: " + imgColorRang.getLeft() + ",top: " + imgColorRang.getTop() + ",right: " +imgColorRang.getRight() + ",bottom: " + imgColorRang.getBottom()); //觸摸點(diǎn)與圓盤圓心距離 float diff = (float) Math.sqrt((centreY - yInView) * (centreY - yInView) + (centreX - xInView) * (centreX - xInView)); //在選色圖片內(nèi)則進(jìn)行讀取顏色等操作 if (diff <= select_radius) { //選色位置指示,若設(shè)置了則移動(dòng)到點(diǎn)取的位置 if (imgPicker != null ) { int xInWindow = (int) event.getX(); int yInWindow = (int) event.getY(); int left = xInWindow + v.getLeft() - imgPicker.getWidth() / 2; int top = yInWindow + v.getTop() - imgPicker.getWidth() / 2; int right = left + imgPicker.getWidth(); int bottom = top + imgPicker.getHeight(); imgPicker.layout(left, top, right, bottom); } if ((event.getY() - imgColorRang.getTop()) < 0) return true; //讀取顏色 int pixel = bitmap.getPixel((int) (event.getX() - imgColorRang.getLeft()), (int) (event.getY() - imgColorRang.getTop())); //獲取選擇像素 if (colorChangedListener != null) { if (event.getAction() == MotionEvent.ACTION_UP) { colorChangedListener.stopColorChanged(Color.red(pixel), Color.blue(pixel), Color.green(pixel)); }else { colorChangedListener.colorChanged(Color.red(pixel), Color.blue(pixel), Color.green(pixel)); } } Log.d(TAG, "radValue=" + Color.red(pixel) + " blueValue=" + Color.blue(pixel) + " greenValue" + Color.green(pixel)); } return true; } }); }
設(shè)置指示圖標(biāo)
設(shè)置圖標(biāo),同時(shí)根據(jù)圖標(biāo)的大小設(shè)置控件的padding避免在邊界處顯示不全的問(wèn)題。
public void setImgPicker(final Context context, final ImageView imgPicker, final int pickerViewWidth) { this.imgPicker = imgPicker; pickerViewPadding = dip2px(context, pickerViewWidth/2); new Handler().postDelayed(new Runnable() { @Override public void run() { rl_root.setPadding(pickerViewPadding, pickerViewPadding, pickerViewPadding, pickerViewPadding); bitmap = ((BitmapDrawable) imgColorRang.getDrawable()).getBitmap();//獲取圓盤圖片 } },10); }
Android是一種基于Linux內(nèi)核的自由及開放源代碼的操作系統(tǒng),主要使用于移動(dòng)設(shè)備,如智能手機(jī)和平板電腦,由美國(guó)Google公司和開放手機(jī)聯(lián)盟領(lǐng)導(dǎo)及開發(fā)。
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。
文章標(biāo)題:怎么在Android中自定義view實(shí)現(xiàn)圖片選色器
網(wǎng)頁(yè)地址:http://chinadenli.net/article44/gidihe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁(yè)設(shè)計(jì)公司、手機(jī)網(wǎng)站建設(shè)、建站公司、微信小程序、虛擬主機(jī)、網(wǎng)站營(yíng)銷
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)