如何创建自定义光标来抓取ImageView的颜色

时间:2017-11-16 08:49:06

标签: android imageview color-picker

颜色抓取应用程序图像的示例

image

您好,我想创建一个Android应用程序来识别图像中的颜色。我想使用自定义光标或视觉触摸让用户知道最后触摸的区域,或者只是让用户在触摸图像时知道所拾取的颜色。换句话说,我想为图像创建类似放大镜光标的东西。我无法想出一个正确的名称,以便我的解释清楚,请查看附图:https://i.stack.imgur.com/iOA1p.jpg。谢谢

1 个答案:

答案 0 :(得分:0)

看看这个类,这是一个自定义的ImageView。在onTouch()中获取坐标,并从该坐标获得像素颜色。随意根据您的使用进行修改。

    <com.xbadal.views.MyImageView
            android:id="@+id/signature_canvas"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:adjustViewBounds="true"
            android:scaleType="fitCenter"
            android:visibility="gone"
            tools:visibility="visible" />



public class MyImageView extends android.support.v7.widget.AppCompatImageView {
private final String TAG = getClass().getName();
private final float TOLERANCE = 20;
private final int defaultWidth = 30;

private Bitmap mBitmap;
private Canvas mCanvas;
private Context context;

private Paint paint;
private Point point;

private float mLastTouchX;
private float mLastTouchY;
private int imageX;
private int imageY;

//Parameter Constructor
public MyImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.context = context;
}

// override onSizeChanged
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    Log.d(TAG, "onSizeChanged: " + h + "-" + w);
    mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    mCanvas = new Canvas(mBitmap);
}

//Overriding onTouch
@Override
public boolean onTouchEvent(MotionEvent event) {
    float x = event.getX();
    float y = event.getY();
    //Invert Matrix to get proper coordinate
    float[] eventXY = new float[]{x, y};
    Matrix invertMatrix = new Matrix();
    getImageMatrix().invert(invertMatrix);
    invertMatrix.mapPoints(eventXY);
    imageX = (int) eventXY[0];
    imageY = (int) eventXY[1];

    switch (event.getAction() & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:
            mLastTouchX = x;
            mLastTouchY = y;
            invalidate();
            break;
        case MotionEvent.ACTION_MOVE:
            final float dx = x - mLastTouchX;
            final float dy = y - mLastTouchY;

            if (Math.abs(dx) >= TOLERANCE || Math.abs(dy) >= TOLERANCE) {
                mLastTouchX = x;
                mLastTouchY = y;
                invalidate();
            }
            break;
        case MotionEvent.ACTION_UP:
            mLastTouchX = x;
            mLastTouchY = y;
            invalidate();
            break;
    }
    return true;
}

@Override
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    drawShape(canvas, mLastTouchX, mLastTouchY, defaultWidth);
}

//this method will detemine which shape to be drawn on the basis of ShapeId
public void drawShape(Canvas canvas, float x, float y, int width) {
    paint = new Paint();
    point = new Point();
    point.set((int) x, (int) y);


    Bitmap bitmap = ((BitmapDrawable) this.getDrawable()).getBitmap();
    if (x >= 0 && x < bitmap.getWidth() && y >= 0 && y < bitmap.getHeight()) {
        if (imageX >= 0 && imageX < bitmap.getWidth() && imageY >= 0 && imageY < bitmap.getHeight()) {
            int pixel = (bitmap.getPixel(imageX, imageY));
            int redValue = Color.red(pixel);
            int blueValue = Color.blue(pixel);
            int greenValue = Color.green(pixel);
            int alpha = Color.alpha(pixel);

        }
    }
}

}