最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android自定義view實(shí)現(xiàn)圖片選色器

 更新時間:2018年06月02日 08:34:56   作者:oden.su  
這篇文章主要為大家詳細(xì)介紹了Android自定義view實(shí)現(xiàn)圖片選色器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

簡介

本文介紹該自定義view的使用及實(shí)現(xiàn)的方法,主要實(shí)現(xiàn)以下幾個功能:

- 選取圓盤選色圖片上的顏色,實(shí)時監(jiān)聽
- 可設(shè)置選色指示圖片,跟隨觸摸位置、指示所選顏色,示例中為白色圓環(huán)
- 可自己設(shè)置選色圖片(目前只支持圓形圖片)

github鏈接

使用效果

首先看下使用效果:


使用示例

在項(xiàng)目中導(dǎo)入該庫

在工程的 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); //最后一個參數(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) {

   }
  });
 }

對外公開的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)過程

attrs屬性

可通過picture_resource屬性設(shè)置用來選色的資源id,現(xiàn)僅支持圓形圖片

 <declare-styleable name="ColorPickerView">
  <attr name="picture_resource" format="reference"/>
 </declare-styleable>

xml

布局中就是放入一個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)一處理用來選色的view及指示選色位置的view(imgPicker),imgPicker為指示顯示位置的圓框,若設(shè)置了則跟隨手指移動。

 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è)置了則移動到點(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),同時根據(jù)圖標(biāo)的大小設(shè)置控件的padding避免在邊界處顯示不全的問題。

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);
 }

總結(jié)

ok,至此,一個比較簡單的選色器就完成了。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • flutter 動手?jǐn)]一個城市選擇citypicker功能

    flutter 動手?jǐn)]一個城市選擇citypicker功能

    在一些項(xiàng)目開發(fā)中經(jīng)常會用到城市選擇器功能,今天小編動手?jǐn)]一個基于flutter 城市選擇citypicker功能,具體實(shí)現(xiàn)過程跟隨小編一起看看吧
    2021-08-08
  • Android自定義Toast之WindowManager

    Android自定義Toast之WindowManager

    這篇文章主要為大家詳細(xì)介紹了Android自定義Toast之WindowManager的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • Android漲姿勢知識點(diǎn)之你沒用過的BadgeDrawable

    Android漲姿勢知識點(diǎn)之你沒用過的BadgeDrawable

    現(xiàn)在Android中有許多的應(yīng)用仿蘋果的在應(yīng)用圖標(biāo)上顯示小紅點(diǎn),下面這篇文章主要給大家介紹了關(guān)于Android漲姿勢知識點(diǎn)之你沒用過的BadgeDrawable的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-09-09
  • Android仿新浪微博/QQ空間滑動自動播放視頻功能

    Android仿新浪微博/QQ空間滑動自動播放視頻功能

    相信用過新浪微博或者QQ空間的朋友都看到過滑動自動播放視頻的效果,那么這篇文章跟大家分享下如何利用Android實(shí)現(xiàn)這一個功能,有需要的朋友們可以參考借鑒。
    2016-09-09
  • flutter實(shí)現(xiàn)點(diǎn)擊事件

    flutter實(shí)現(xiàn)點(diǎn)擊事件

    這篇文章主要為大家詳細(xì)介紹了flutter實(shí)現(xiàn)點(diǎn)擊事件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-08-08
  • Android實(shí)現(xiàn)二級列表購物車功能

    Android實(shí)現(xiàn)二級列表購物車功能

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)二級列表購物車功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • Android仿人人網(wǎng)滑動側(cè)邊欄效果

    Android仿人人網(wǎng)滑動側(cè)邊欄效果

    這篇文章主要為大家詳細(xì)介紹了Android仿人人網(wǎng)滑動側(cè)邊欄效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • Android實(shí)現(xiàn)文件下載

    Android實(shí)現(xiàn)文件下載

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)文件下載,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • android 開發(fā)教程之日歷項(xiàng)目實(shí)踐(一)

    android 開發(fā)教程之日歷項(xiàng)目實(shí)踐(一)

    決定開始學(xué)習(xí) Android 平臺下的軟件開發(fā),以日歷作為實(shí)踐項(xiàng)目,進(jìn)行一周后,基本完成,有需要的朋友可以參考下
    2013-01-01
  • Android系統(tǒng)默認(rèn)對話框添加圖片功能

    Android系統(tǒng)默認(rèn)對話框添加圖片功能

    這篇文章主要介紹了Android系統(tǒng)默認(rèn)對話框添加圖片的相關(guān)資料,非常不錯,具有參考借鑒價(jià)值,需要的朋友可以參考下
    2017-01-01

最新評論

绥宁县| 峨边| 商丘市| 余干县| 湘潭县| 定安县| 沙洋县| 德惠市| 黄龙县| 安宁市| 台南市| 长春市| 洪江市| 南投县| 治县。| 靖州| 红原县| 商水县| 巩留县| 永兴县| 潢川县| 精河县| 定边县| 南汇区| 辉县市| 景泰县| 衡阳市| 蒲江县| 临沧市| 钟山县| 宜宾市| 泉州市| 乐清市| 武平县| 湖南省| 桦川县| 屏东县| 凯里市| 通河县| 商洛市| 陇西县|