android 无法更改绘图颜色

jvlzgdj9  于 2023-05-05  发布在  Android
关注(0)|答案(1)|浏览(153)

我创建相机应用程序与网格,我希望用户能够通过点击他想要使用的颜色按钮来改变网格的颜色。网格是使用onDraw创建的,它在DrawGrid类中,该类是从GridMenuFragment片段调用的,而颜色按钮在ColorFragment片段中。
现在,我成功地将想要使用的颜色传递给了DrawGrid,但它没有更新,网格的颜色也没有改变。我在setCurrentColor方法中使用invalidate()。我试着用改变网格的方法来改变颜色,但它不起作用。任何帮助将不胜感激。
以下是DrawGird.java代码:

public class DrawGrid extends View {
    int whichGrid;
    Paint paint;
    int currentColor=Color.WHITE;

    public DrawGrid(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
        paint.setColor(currentColor);
        paint.setStrokeWidth(5);
        paint.setStyle(Paint.Style.STROKE);
    }
    
    public void setWhichGrid(int number) {
        whichGrid = number;
        invalidate();
    }

    public void setCurrentColor(int color) {
        switch(color){
            case 0:
                currentColor=Color.WHITE;
                break;
            case 1:
                currentColor=Color.RED;
                break;
            case 2:
                currentColor=Color.BLUE;
                break;
        }
        paint.setColor(currentColor);
        invalidate();
        Log.d("DrawGrid", "currentColor is now1 " + currentColor);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Path path = new Path();
        int width = getWidth();
        int height = getHeight();
        switch (whichGrid) {
            case 1:
                float lineV1 = width / 3f;
                float lineV2 = (width / 3f) * 2f;
                float lineH1 = height / 3f;
                float lineH2 = (height / 3f) * 2f;
                Log.d("DrawGrid", "currentColor is now3 " + currentColor);
                canvas.drawLine(lineV1, 0, lineV1, height, paint);
                canvas.drawLine(lineV2, 0, lineV2, height, paint);
                canvas.drawLine(0, lineH1, width, lineH1, paint);
                canvas.drawLine(0, lineH2, width, lineH2, paint);
                break;
            case 2:
                float lineV = width / 3f;
                float lineH = height / 3f;
                canvas.drawLine(lineV, 0, lineV, height, paint);
                canvas.drawLine(0, lineH, width, lineH, paint);
                break;
            
        }
    }
}

编辑:从ColorFragment添加OnCreateView,我在其中调用DrawGrid.setCurrentColor():

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_color, container, false);

        drawGrid=new DrawGrid(getContext(), null);
        FrameLayout mainLayout = ((MainActivity)getActivity()).getRelativeLayout();
        mainLayout.removeView(drawGrid);
        mainLayout.addView(drawGrid);

        Button red = view.findViewById(R.id.button1);
        Button blue = view.findViewById(R.id.button2);
        red.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d("ColorClass", "Button clicked");
                drawGrid.setCurrentColor(Color.RED);
                FragmentManager fragmentManager = requireActivity().getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.hide(ColorFragment.this).commit();
            }
        });

        blue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                drawGrid.setCurrentColor(Color.BLUE);
                FragmentManager fragmentManager = requireActivity().getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.hide(ColorFragment.this).commit();
            }
        });
        return view;
    }
kxkpmulp

kxkpmulp1#

我只是复制了你的代码,它工作。
你能附上你用来调用DrawGrid.setCurrentColor(color)的代码吗?我敢打赌,你只是把不同于(0,1,2)的值传递给它,在视觉上没有任何变化。即使传递了正确的值,问题也不在DrawGrid类中,因为它工作正常。
如果你将传递0、1或2,那么视图将用传递的颜色重新绘制。
为什么不直接为这个方法提供颜色。而调用方将决定它想要使用哪种颜色。就像这样:

public void setCurrentColor(@ColorInt int color) {
        this.currentColor = color;
        paint.setColor(color);
        invalidate();
        Log.d("DrawGrid", "currentColor is now1 " + currentColor);
}

相关问题