Android:我绘制了很多形状。我点击它时需要更改形状的颜色

时间:2016-12-19 11:57:32

标签: java android android-activity view ondraw

我有两个类:CustomView扩展View和MainActivity扩展Activity。在CustomView中,我使用循环绘制一系列圆角正方形(canvas.drawRoundRect)。我知道如何检测任何给定方块上的点击,但我不知道如何更改方块的颜色。如何从MainActivity调用onDraw方法?或者,如果有更新方法,我可以使用来从MainActivity类中使invalidate()。底线是我想知道每当我点击它时如何改变我的形状颜色。谢谢。

3 个答案:

答案 0 :(得分:0)

使用以下内容:一旦您在Java代码中命名并声明了它们的表单,您可以按如下方式更改它,按名称调用该对象并放置以下内容:

"Name of the object" .setbackgroundColor ("Name of the object" .getContext ().GetResources (). GetColor (R.color. "Desired color")

答案 1 :(得分:0)

在onDraw()方法中,通过在变量

中设置绘画颜色来绘制矩形
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    firstPaint.setColor(firstRectColor);
    canvas.drawRoundRect(//..,firstPaint);
    //..
    setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            if(motionEvent.getAction()==MotionEvent.ACTION_DOWN){
               if(((motionEvent.getX()>=firstRectX) && (motionEvent.getX()<=firstRectX+firstRectWidth))&&((motionEvent.getY()>=firstRectY) && (motionEvent.getY()<=firstRectY+firstRectHeight))){
                //touch point is inside first rectangle
                //assign the color to firstRectColor variable and call invalidate to redraw
                firstRectColor=getColorToChange();
                invalidate();
               }//..else if(){}
            } 
            return true;
        }
    });
}

答案 2 :(得分:0)

为此,你需要获得点击像素的颜色,然后使用下面的Flood-fill算法并传递你的位图,你点击了Bitmap,Target和替换颜色代码。

  private void FloodFill(Bitmap bmp, Point pt, int targetColor, int replacementColor) 
    {
        Queue<Point> q = new LinkedList<Point>();
        q.add(pt);
        while (q.size() > 0) {
            Point n = q.poll();
            if (bmp.getPixel(n.x, n.y) != targetColor)
                continue;

            Point w = n, e = new Point(n.x + 1, n.y);
            while ((w.x > 0) && (bmp.getPixel(w.x, w.y) == targetColor)) {
                bmp.setPixel(w.x, w.y, replacementColor);
                if ((w.y > 0) && (bmp.getPixel(w.x, w.y - 1) == targetColor))
                    q.add(new Point(w.x, w.y - 1));
                if ((w.y < bmp.getHeight() - 1)
                        && (bmp.getPixel(w.x, w.y + 1) == targetColor))
                    q.add(new Point(w.x, w.y + 1));
                w.x--;
            }
            while ((e.x < bmp.getWidth() - 1)
                    && (bmp.getPixel(e.x, e.y) == targetColor)) {
                bmp.setPixel(e.x, e.y, replacementColor);

                if ((e.y > 0) && (bmp.getPixel(e.x, e.y - 1) == targetColor))
                    q.add(new Point(e.x, e.y - 1));
                if ((e.y < bmp.getHeight() - 1)
                        && (bmp.getPixel(e.x, e.y + 1) == targetColor))
                    q.add(new Point(e.x, e.y + 1));
                e.x++;
            }
        }
    }

您可以搜索更多关于洪水填充算法的信息,以便了解击球手。

https://github.com/latemic/ColorBooth/blob/master/src/com/colorbooth/FloodFill.java