Android简单自定义View——获取图片颜色的ImageView - Go语言中文社区

Android简单自定义View——获取图片颜色的ImageView


1.自定义View
原理:继承ImageView 获取ImageView在XML中的背景图片或者资源图片,通过触碰事件获得触碰点的坐标
然后取得该坐标的像素点颜色color,然后通过回掉接口,将color传递给外部使用。
无法实现即设置背景图片又设置资源图片,只能设置其中一种。
public class ColorImageView extends AppCompatImageView {
    //颜色值
    int color;
    Bitmap bitmap;
    Drawable drawable;


    public ColorImageView(Context context) {
        super(context);
        init();
    }

    public ColorImageView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();

    }

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

    private void init() {
        //Drawable包括colorDrawable
        if (getBackground() != null && getDrawable() == null) {
            //只设置了背景图片
            drawable = getBackground();
        } else if (getBackground() == null && getDrawable() != null) {
            //只设置了资源图片
            drawable = getDrawable();
        } else {
            //即设置了背景图片,又设置了资源图片,这样无法准确确认颜色
            //未设置背景图片,以及资源图片src
            return;
        }
        //Drawable包括colorDrawable
        if (drawable instanceof ColorDrawable) {
            ColorDrawable colorDrawable = (ColorDrawable) drawable;
            color = colorDrawable.getColor();
        } else {
            bitmap = ((BitmapDrawable) drawable).getBitmap();
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (bitmap != null) {
            int x = (int) event.getX();
            int y = (int) event.getY();
            if (x < 0 || x > bitmap.getWidth() || y < 0 || y > bitmap.getHeight()) {
                return false;
            }
            color = bitmap.getPixel(x, y);
        }
        if (color == 0) {
            return false;
        } else {
            if (mOnColorSelectedListener != null) {
                mOnColorSelectedListener.onColorSelectedL(color);
            }
        }
        return true;
    }

    //回调接口
    public interface OnColorSelectedListener {
        void onColorSelectedL(int color);
    }

    private OnColorSelectedListener mOnColorSelectedListener;
    //set方法方便外部调用接口

    public void setOnColorSelectedListener(OnColorSelectedListener onColorSelectedListener) {
        this.mOnColorSelectedListener = onColorSelectedListener;
    }
}
2.ColorImageView的使用
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_marginTop="20dp"
        android:id="@+id/tv_test"
        android:textSize="20sp"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="测试颜色"
        android:textColor="#000"
        />

    <com.xykj.test.ColorImageView
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:id="@+id/iv_civ"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@mipmap/n_image_colorful"
        />
</LinearLayout>
通过点击ColorImageView中设置的图片,然后改变TextView的背景颜色
public class MainActivity extends AppCompatActivity {
    ColorImageView mColorImageView;
    TextView mTextView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //找到控件
        mColorImageView = (ColorImageView) findViewById(R.id.iv_civ);
        mTextView = (TextView) findViewById(R.id.tv_test);
        //设置监听
        mColorImageView.setOnColorSelectedListener(new ColorImageView.OnColorSelectedListener() {
            @Override
            public void onColorSelectedL(int color) {
                mTextView.setBackgroundColor(color);
            }
        });
    }
}
效果图




版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/ww897532167/article/details/56334122
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-03-01 17:42:54
  • 阅读 ( 806 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢