我有一个 Activity ,其中有3个不同 View 的复选框。这些复选框用于拾取颜色。

在DrawingView类中,我必须在 Canvas 上使用选中的颜色进行绘制。我想要的是将一个整数值从 Activity 传递到 View 类,并相应地设置绘画的颜色。使用getter和setter进行迭代,但是我得到黑色。我想这是因为颜色是在构造函数本身中设置的,并且当我选中任何框时颜色都不会改变。

请引用this以获取以下代码中的更新

代码:

MainActivity :在此处选择了颜色/复选框。绘制工作将在此 Activity 本身的布局中完成。

        carImageView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            drawingView=new DrawingView(carImageView.getContext());
            drawingView=new DrawingView(carImageView.getContext(),null);
            drawingView.setColor(color);
            return false;
        }
    });


  scratchesCb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
      @Override
      public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
          if(b)
          {
              color=1;
              chipsCb.setChecked(false);
              dentsCb.setChecked(false);
          }
      }
  });
    chipsCb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            if(b)
            {
                color=2;
                scratchesCb.setChecked(false);
                dentsCb.setChecked(false);
            }
        }
    });
    dentsCb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            if(b)
            {
                color=3;
                chipsCb.setChecked(false);
                scratchesCb.setChecked(false);
            }
        }
    });
}

查看类别:
   public DrawingView(Context context, @Nullable AttributeSet attrs) {
   super(context, attrs);
    mPaint=new Paint();
    if(color==1)
        mPaint.setColor(Color.RED);
    else if(color==2)
        mPaint.setColor(Color.BLUE);
    else if(color==3)
        mPaint.setColor(Color.GREEN);
    this.context=context;
    mPath=new Path();
    mPaint.setAntiAlias(true);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeJoin(Paint.Join.MITER);
    mPaint.setStrokeWidth(5f);
}
     public void setColor(int color){
    this.color=color;
}
public int getColor(){
    return this.color;
}

编辑

我不一定要使用完全相同的代码。我只想在选中一个复选框以在 ImageView 上绘制时更改绘画颜色。任何其他方法都欢迎。

最佳答案

在MainActivity中,您正在创建一个DrawingView,该DrawingView与显示的 ImageView 没有任何关系。因此,更改颜色时,不是在更改显示的 ImageView 的颜色,而是在更改未连接的ojit_code的颜色。 ImageView 永远不会定义新的颜色,并且始终默认为黑色。

这是一个基于您最近提供的代码的小型工作应用程序的视频。单击新复选框时,也许所有颜色都不会更改,但是您将能够单独解决该问题。

我对Java代码所做的更改已被注释为这样。还对XML进行了更改,以允许您的代码在我的环境中运行,但是未对这些更改进行注释。

android - 将值(value)从 Activity 传递到 View 类-LMLPHP

MainActivity.java(更新)

public class MainActivity extends AppCompatActivity {
    ImageView caricon;
    int itemSelected = 0;
    private DrawingView carImageView;
    Bitmap bitmap;
    ImageView backarrow;
    TextView nextcheckinAB2;
    Bitmap bmp;
    public static int color;
    CheckBox scratchesCb, chipsCb, dentsCb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        carImageView = (DrawingView) findViewById(R.id.carImageView);
        scratchesCb = (CheckBox) findViewById(R.id.scratchesCheckBox);
        chipsCb = (CheckBox) findViewById(R.id.ChipCheckbx);
        dentsCb = (CheckBox) findViewById(R.id.DentsCheckBox);

        // Change: Make sure to initialize the color
        color = 1;
        carImageView.setColor(color);

        carImageView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                // drawingView = new DrawingView(carImageView.getContext(),null);
                carImageView.setColor(color);
                return false;
            }
        });


        scratchesCb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    color = 1;
                    carImageView.clearCanvas();
                    carImageView.setColor(1); //
                    chipsCb.setChecked(false);
                    dentsCb.setChecked(false);
                }
            }
        });
        chipsCb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    color = 2;
                    carImageView.setColor(2);
                    scratchesCb.setChecked(false);
                    dentsCb.setChecked(false);

                }
            }
        });
        dentsCb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    color = 3;
                    // Change: Do like the other check boxes althogh not really needed.
                    carImageView.setColor(3);
                    chipsCb.setChecked(false);
                    scratchesCb.setChecked(false);

                }
            }
        });
    }
}

DrawingView.java(更新)
public class DrawingView extends android.support.v7.widget.AppCompatImageView {
    private Path mPath;
    private Paint mPaint;
    private float mX, mY;
    private static final float TOLERANCE = 5;
    int color;
    Context context;

    public DrawingView(Context context) {
        super(context);
        this.context = context;
        init();
    }

    public DrawingView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();


    }

    public void init() {
        mPath = new Path();
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.MITER);
        mPaint.setStrokeWidth(5f);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawPath(mPath, mPaint);
    }

    public void setColor(int color) {
        if (color == 1) {
            mPaint.setColor(Color.RED);
            this.color = color;
            invalidate();
        } else if (color == 2) {
            mPaint.setColor(Color.BLUE);
            this.color = color;
            invalidate();
        } else if (color == 3) {
            mPaint.setColor(Color.GREEN);
            this.color = color;
            invalidate();
        }

    }

    public int getColor() {
        return this.color;
    }

    private void onStartTouch(float x, float y) {
        mPath.moveTo(x, y);
        mX = x;
        mY = y;
    }

    private void moveTouch(float x, float y) {
        float dx = Math.abs(x - mX);
        float dy = Math.abs(y - mY);
        if (dx >= TOLERANCE || dy >= TOLERANCE) {
            mPath.quadTo(mX, mY, (mX + x) / 2, (mY + y) / 2);
            mX = x;
            mY = y;
        }
    }

    public void clearCanvas() {
        mPath.reset();
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        invalidate();
    }

    private void upTouch() {
        mPath.lineTo(mX, mY);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                onStartTouch(x, y);
                invalidate();
                break;
            case MotionEvent.ACTION_MOVE:
                moveTouch(x, y);
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                upTouch();
                invalidate();
                break;
        }

        return true;

    }
}

activity_main.xml(已更新)

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:orientation="horizontal"
        android:weightSum="3">

        <HorizontalScrollView
            android:id="@+id/horizontalSrollView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="5dp"
                    android:layout_weight="1">

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="5dp"

                        android:orientation="vertical">

                        <LinearLayout
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:orientation="horizontal"
                            android:paddingLeft="15dp"
                            android:paddingRight="15dp"
                            android:paddingTop="5dp"
                            android:weightSum="2">

                            <ImageView
                                android:layout_width="20dp"
                                android:layout_height="20dp"
                                android:layout_marginRight="2dp"
                                android:layout_weight="0.5"
                                android:background="@android:color/holo_red_light" />

                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginLeft="2dp"
                                android:layout_weight="1.5"
                                android:text="Scratches"
                                android:textColor="#000" />
                        </LinearLayout>

                        <CheckBox
                            android:id="@+id/scratchesCheckBox"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center"
                            android:checked="true" />
                    </LinearLayout>

                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1">

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="5dp"
                        android:orientation="vertical">

                        <LinearLayout
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:orientation="horizontal"
                            android:paddingLeft="15dp"
                            android:paddingRight="15dp"
                            android:paddingTop="5dp"
                            android:weightSum="2">

                            <ImageView
                                android:layout_width="20dp"
                                android:layout_height="20dp"
                                android:layout_marginRight="2dp"
                                android:layout_weight="0.5"
                                android:background="@android:color/holo_blue_light" />

                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginLeft="2dp"
                                android:layout_weight="1.5"
                                android:text="Chips"
                                android:textColor="#000" />
                        </LinearLayout>

                        <CheckBox
                            android:id="@+id/ChipCheckbx"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center" />
                    </LinearLayout>

                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1">

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="5dp"
                        android:orientation="vertical">

                        <LinearLayout
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:orientation="horizontal"
                            android:paddingLeft="15dp"
                            android:paddingRight="15dp"
                            android:paddingTop="5dp"
                            android:weightSum="2">

                            <ImageView
                                android:layout_width="20dp"
                                android:layout_height="20dp"
                                android:layout_marginRight="2dp"
                                android:layout_weight="0.5"
                                android:background="@android:color/holo_green_light" />

                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginLeft="2dp"
                                android:layout_weight="1.5"
                                android:text="Dings/Dents"
                                android:textColor="#000" />
                        </LinearLayout>

                        <CheckBox
                            android:id="@+id/DentsCheckBox"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center" />
                    </LinearLayout>

                </RelativeLayout>
            </LinearLayout>

        </HorizontalScrollView>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="8.7">

        <[your package name].DrawingView
            android:id="@+id/carImageView"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:src="@mipmap/ic_launcher"
            android:layout_gravity="center_vertical" />
        <!--<ImageView
            android:id="@+id/carImageView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center_vertical"/>-->
    </LinearLayout>


</LinearLayout>

关于android - 将值(value)从 Activity 传递到 View 类,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47426849/

10-09 04:30