从ImageView中获取android java的颜色

时间:2015-11-29 20:29:08

标签: java android

我正在使用最新的Android IDE。我的UI中有一个吸管图标,用户可以从应用程序抛出的位图中选择一种颜色。我在图标上设置了一个监听器,但我找不到让它等待下一次触摸的方法,从中可以计算出像素颜色。到目前为止,这是一个简单的代码:

//Open the eyedropper
    dropBtn = (ImageButton) findViewById(R.id.drop_btn);
    dropBtn.setOnTouchListener( new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                Toast toast = Toast.makeText(getApplicationContext(), colPickMsg, Toast.LENGTH_SHORT);
                toast.show();

                float x = event.getX();
                float y = event.getY();

                switch (event.getAction()) {

                    case MotionEvent.ACTION_DOWN:

                        p1.x = (int) x;
                        p1.y = (int) y;

                        final int sourceColor = drawView.drawableBitmap.getPixel((int) x, (int) y);
                }
            }
            return true;
        }
    });

问题是代码刚刚爆破,它报告的像素是选择吸管的图标中的一个。我需要代码等待下一次点击并报告。所有建议都将受到重视。

这是绘图视图的代码。

package com.softwareandgeneral.drawtutorial01;

import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;

public class DrawingView extends View {

    //drawing path
    private Path drawPath;
    //drawing and canvas paint
    private Paint drawPaint, canvasPaint;
    //initial color
    private int paintColor = 0xFF660000;
    //canvas
    private Canvas drawCanvas;
    //canvas bitmap
    public Bitmap canvasBitmap, drawableBitmap;
    //brush sizes
    private float brushSize, lastBrushSize;
    //erase flag
    private boolean erase = false, paint_mode = false;

    final Point p1 = new Point();
    private Path path;

    public DrawingView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setupDrawing();
    }

    //setup drawing
    private void setupDrawing() {

        //prepare for drawing and setup paint stroke properties
        brushSize = getResources().getInteger(R.integer.medium_size);
        lastBrushSize = brushSize;
        drawPath = new Path();
        drawPaint = new Paint();
        drawPaint.setColor(paintColor);
        drawPaint.setAntiAlias(true);
        drawPaint.setStrokeWidth(brushSize);
        drawPaint.setStyle(Paint.Style.STROKE);
        drawPaint.setStrokeJoin(Paint.Join.ROUND);
        drawPaint.setStrokeCap(Paint.Cap.ROUND);
        canvasPaint = new Paint(Paint.DITHER_FLAG);
    }

    //size assigned to view
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        canvasBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.doodle_01);
        drawableBitmap = canvasBitmap.copy(Bitmap.Config.ARGB_8888, true);
        drawCanvas = new Canvas(drawableBitmap);
    }

    //draw the view - will be called after touch event
    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawBitmap(drawableBitmap, 0, 0, canvasPaint);
        canvas.drawPath(drawPath, drawPaint);
    }

    //update color
    public void setColor(String newColor) {
        invalidate();
        paintColor = Color.parseColor(newColor);
        drawPaint.setColor(paintColor);
    }

    //set brush size
    public void setBrushSize(float newSize) {
        float pixelAmount = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                newSize, getResources().getDisplayMetrics());
        brushSize = pixelAmount;
        drawPaint.setStrokeWidth(brushSize);
    }

    //get and set last brush size
    public void setLastBrushSize(float lastSize) {
        lastBrushSize = lastSize;
    }

    public float getLastBrushSize() {
        return lastBrushSize;
    }

    //set erase true or false
    public void setErase(boolean isErase) {
        erase = isErase;
        if (erase) drawPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        else drawPaint.setXfermode(null);
    }

    //start new drawing
    public void startNew() {
        drawCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
        invalidate();
    }

    // JRL:  function to return current paint color
    public int getPaintColor() {
        return drawPaint.getColor();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        if (paint_mode) {
            float touchX = event.getX();
            float touchY = event.getY();
            //respond to down, move and up events
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    drawPath.moveTo(touchX, touchY);
                    break;
                case MotionEvent.ACTION_MOVE:
                    drawPath.lineTo(touchX, touchY);
                    break;
                case MotionEvent.ACTION_UP:
                    drawPath.lineTo(touchX, touchY);
                    drawCanvas.drawPath(drawPath, drawPaint);
                    drawPath.reset();
                    break;
                default:
                    return false;
            }
            //redraw
            invalidate();
            return true;

        } else {

            float x = event.getX();
            float y = event.getY();

            switch (event.getAction()) {

                case MotionEvent.ACTION_DOWN:

                    p1.x = (int) x;
                    p1.y = (int) y;
                    final int sourceColor = drawableBitmap.getPixel((int) x, (int) y);
                    final int targetColor = drawPaint.getColor();
                    new TheTask(drawableBitmap, p1, sourceColor, targetColor).execute();
                    invalidate();
            }
            return true;
        }
    }

}

这越来越近了。我已根据此代码片段在MainActivity.onClick(View视图)中实现了onTouch侦听器:

else if (view.getId() == R.id.drop_btn) {
        //JRL:  color picker button clicked
        Toast toast = Toast.makeText(getApplicationContext(), colPickMsg, Toast.LENGTH_SHORT);
        toast.show();

        drawView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    float x = event.getX();
                    float y = event.getY();

                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN:
                            p1.x = (int) x;
                            p1.y = (int) y;
                            final int sourceColor = drawView.drawableBitmap.getPixel((int) x, (int) y);
                            currPaint.setBackgroundColor(sourceColor);
                            String hexColor = String.format("#FF%06X", (0xFFFFFF & sourceColor));
                            currPaint.setTag(hexColor);
                            drawView.setColor(hexColor);
                    }
                }
                return true;
            }
        });
    }

这是有效的。我可以选择一种颜色,对UI的更改工作正常。什么不起作用是我被困在这个监听器 - 我想要回到的原始onTouchEventlistener。我还没有办法在Android中停止听众。

1 个答案:

答案 0 :(得分:0)

我认为你倒退了。问题是OnTouchListener的意思是“当有什么东西触动我时,不要”当我触摸某事时,这样做。“

听众看起来不错,但是把这段代码放在OnTouchListener的颜色位图中,而不是吸管图标。

试试这个:

drawView.setOnTouchListener( new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            Toast toast = Toast.makeText(getApplicationContext(), colPickMsg, Toast.LENGTH_SHORT);
            toast.show();

            float x = event.getX();
            float y = event.getY();

            switch (event.getAction()) {

                case MotionEvent.ACTION_DOWN:

                    p1.x = (int) x;
                    p1.y = (int) y;

                    final int sourceColor = drawView.drawableBitmap.getPixel((int) x, (int) y);
            }
        }
        return true;
    }
});