热门标签 | HotTags
当前位置:  开发笔记 > Android > 正文

ImageView实现AndroidcolorPikcer选择器的示例代码

本文介绍了ImageView 实现Android colorPikcer 选择器的示例代码,分享给大家,具体如下: Android c

本文介绍了ImageView 实现Android colorPikcer 选择器的示例代码,分享给大家,具体如下:

Android colorPikcer 选择器

环形的ColorPicker,主要思路是:

  1. Color 选在放在ImageView 的background上面,根据点击的位置判断选择的颜色。
  2. 重写onTouch,在onTouch 里面判断点击点的颜色。
  3. 根据当前选择的颜色设置图片的src.

获取Bitmap

在 ColorPickerView 构造函数中初始化 Bitmap。因为getBackground有多种drawable,然后获取Bitmap 的方式也不用,

void init(Context context, @Nullable AttributeSet attrs, int defStyleAttr){
    Drawable drawable = getBackground();
    if(drawable instanceof BitmapDrawable){
      mBitmap = ((BitmapDrawable) drawable).getBitmap();
    } else if(drawable instanceof VectorDrawable){
      mBitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
      Canvas vectorCanvas = new Canvas(mBitmap);
      drawable.setBounds(0, 0, vectorCanvas.getWidth(), vectorCanvas.getHeight());
      drawable.draw(vectorCanvas);
    }

重写onTouch

根据Touch 事件的左边获取 Bitmap 对应点的颜色。

需要注意的是如果 View 的宽和高参数是 wrap_content, MotionEvent 的点击的点一定在Bitmap 的坐标内。但是如果不是wrap_content, 需要对坐标转换,利用矩阵Matrix 对点击点转换。

public boolean onTouch(View v, MotionEvent event) {

    if (event.getAction() == MotionEvent.ACTION_DOWN && mBitmap != null) {
  
      float scaleX = mBitmap.getWidth()*1.0f/v.getWidth();
      float scaleY = mBitmap.getHeight()*1.0f/v.getHeight();

      float[] touchPoint = new float[] { event.getX(), event.getY() };
      Matrix matrix = new Matrix();
      matrix.setScale(scaleX, scaleY);
      matrix.mapPoints(touchPoint);

      mSelectColor = mBitmap.getPixel((int) touchPoint[0], (int) touchPoint[1]);
    }

    return false;
  }

完整的代码:

public class ColorPickerView extends android.support.v7.widget.AppCompatImageView implements View.OnTouchListener{

  private Bitmap mBitmap;
  private int mSelectColor = -1;
  private int mIndex = -1;
  private int[] mDrawableSelects;
  private int[] mColorArray;

  private OnColorSelectedListener mOnColorSelectedListener;


  public ColorPickerView(Context context) {
    this(context, null);
  }

  public ColorPickerView(Context context, @Nullable AttributeSet attrs) {
    this(context, attrs, 0);
  }

  public ColorPickerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(context, attrs, defStyleAttr);
  }

  void init(Context context, @Nullable AttributeSet attrs, int defStyleAttr){
    Drawable drawable = getBackground();
    if(drawable instanceof BitmapDrawable){
      mBitmap = ((BitmapDrawable) drawable).getBitmap();
    } else if(drawable instanceof VectorDrawable){
      mBitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
      Canvas vectorCanvas = new Canvas(mBitmap);
      drawable.setBounds(0, 0, vectorCanvas.getWidth(), vectorCanvas.getHeight());
      drawable.draw(vectorCanvas);
    }

    TypedArray resTypeArray = context.obtainStyledAttributes(attrs, R.styleable.ColorPickerView);
    int colorPickerArrayId = resTypeArray.getResourceId(R.styleable.ColorPickerView_cp_selected_drawable_array, 0);
    resTypeArray.recycle();

    if (colorPickerArrayId != 0) {
      TypedArray typeArray = getResources().obtainTypedArray(colorPickerArrayId);
      mDrawableSelects = new int[typeArray.length()];
      for (int i = 0; i  v.getWidth() || event.getX() <0){
        return false;
      }

      if(event.getY() > v.getHeight() || event.getY() <0){
        return false;
      }

      float scaleX = mBitmap.getWidth()*1.0f/v.getWidth();
      float scaleY = mBitmap.getHeight()*1.0f/v.getHeight();

      float[] touchPoint = new float[] { event.getX(), event.getY() };
      Matrix matrix = new Matrix();
      matrix.setScale(scaleX, scaleY);
      matrix.mapPoints(touchPoint);

      mSelectColor = mBitmap.getPixel((int) touchPoint[0], (int) touchPoint[1]);
      mIndex = getColorIndex(mSelectColor);

      if(mDrawableSelects.length > 0 && mIndex >=0 && mIndex 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


推荐阅读
  • Vue 2 中解决页面刷新和按钮跳转导致导航栏样式失效的问题
    本文介绍了如何通过配置路由的 meta 字段,确保 Vue 2 项目中的导航栏在页面刷新或内部按钮跳转时,始终保持正确的 active 样式。具体实现方法包括设置路由的 meta 属性,并在 HTML 模板中动态绑定类名。 ... [详细]
  • 本文探讨了如何通过最小生成树(MST)来计算严格次小生成树。在处理过程中,需特别注意所有边权重相等的情况,以避免错误。我们首先构建最小生成树,然后枚举每条非树边,检查其是否能形成更优的次小生成树。 ... [详细]
  • 优化ListView性能
    本文深入探讨了如何通过多种技术手段优化ListView的性能,包括视图复用、ViewHolder模式、分批加载数据、图片优化及内存管理等。这些方法能够显著提升应用的响应速度和用户体验。 ... [详细]
  • 本文详细介绍如何使用arm-eabi-gdb调试Android平台上的C/C++程序。通过具体步骤和实用技巧,帮助开发者更高效地进行调试工作。 ... [详细]
  • 深入理解 Oracle 存储函数:计算员工年收入
    本文介绍如何使用 Oracle 存储函数查询特定员工的年收入。我们将详细解释存储函数的创建过程,并提供完整的代码示例。 ... [详细]
  • 本文总结了2018年的关键成就,包括职业变动、购车、考取驾照等重要事件,并分享了读书、工作、家庭和朋友方面的感悟。同时,展望2019年,制定了健康、软实力提升和技术学习的具体目标。 ... [详细]
  • 在计算机技术的学习道路上,51CTO学院以其专业性和专注度给我留下了深刻印象。从2012年接触计算机到2014年开始系统学习网络技术和安全领域,51CTO学院始终是我信赖的学习平台。 ... [详细]
  • CSS 布局:液态三栏混合宽度布局
    本文介绍了如何使用 CSS 实现液态的三栏布局,其中各栏具有不同的宽度设置。通过调整容器和内容区域的属性,可以实现灵活且响应式的网页设计。 ... [详细]
  • Linux 系统启动故障排除指南:MBR 和 GRUB 问题
    本文详细介绍了 Linux 系统启动过程中常见的 MBR 扇区和 GRUB 引导程序故障及其解决方案,涵盖从备份、模拟故障到恢复的具体步骤。 ... [详细]
  • 本文介绍了如何使用jQuery根据元素的类型(如复选框)和标签名(如段落)来获取DOM对象。这有助于更高效地操作网页中的特定元素。 ... [详细]
  • 本文将详细介绍如何使用剪映应用中的镜像功能,帮助用户轻松实现视频的镜像效果。通过简单的步骤,您可以快速掌握这一实用技巧。 ... [详细]
  • 深入理解Cookie与Session会话管理
    本文详细介绍了如何通过HTTP响应和请求处理浏览器的Cookie信息,以及如何创建、设置和管理Cookie。同时探讨了会话跟踪技术中的Session机制,解释其原理及应用场景。 ... [详细]
  • 本文介绍如何在 Xcode 中使用快捷键和菜单命令对多行代码进行缩进,包括右缩进和左缩进的具体操作方法。 ... [详细]
  • 本文介绍了一款用于自动化部署 Linux 服务的 Bash 脚本。该脚本不仅涵盖了基本的文件复制和目录创建,还处理了系统服务的配置和启动,确保在多种 Linux 发行版上都能顺利运行。 ... [详细]
  • 在Linux系统中配置并启动ActiveMQ
    本文详细介绍了如何在Linux环境中安装和配置ActiveMQ,包括端口开放及防火墙设置。通过本文,您可以掌握完整的ActiveMQ部署流程,确保其在网络环境中正常运行。 ... [详细]
author-avatar
Paul
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有