将值从活动传递到查看课程

时间:2017-11-22 04:31:27

标签: android view android-imageview

我有一项活动,其中不同视图中有3个复选框。这些复选框用于拾取颜色。

在DrawingView类中,我必须使用选中的颜色在画布上绘图。我想要的是将活动中的整数值传递给视图类并相应地设置绘制的颜色。使用吸气剂和二传手,但我得到黑色。我想这是因为颜色是在构造函数本身中设置的,当我选中任何框时它不会改变。

请参阅this以获取以下代码中的更新

代码:

MainActivity :此处选择颜色/复选框。绘图将在此活动本身的布局中完成。

        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;
}

修改

我不一定要使用完全相同的代码。我想要的是当选中一个复选框以便能够在图像视图上绘制时更改绘画颜色。任何其他方法都是受欢迎的。

5 个答案:

答案 0 :(得分:4)

在MainActivity中,您创建的DrawingView与显示的图像视图没有任何关系。因此,当您更改颜色时,您不会更改显示的图像视图的颜色,而是更改未连接的DrawingView的颜色。图像视图从未定义新颜色,并且始终默认为黑色。

以下是基于您最近提供的代码的小型工作应用的视频。单击新复选框时,可能不会更改所有颜色,但您可以单独解决该问题。

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

enter image description here

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>

答案 1 :(得分:1)

你需要改变功能setColor。

1.改变mPaint颜色。

2.add invalidate()用于重绘视图。

public void setColor(int color){
    this.color=color;
     mPaint.setColor(color);
    invalidate();
}

答案 2 :(得分:0)

您需要致电invalidate()上的View进行更新。

试试这个,

final DrawingView drawingView = new DrawingView(carImageView.getContext());

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


scratchesCb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  @Override
  public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
      if(b)
      {
          color = 1;
          drawingView.setColor(color);

          chipsCb.setChecked(false);
          dentsCb.setChecked(false);
      }
  }
});

chipsCb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        if(b)
        {
            color = 2;
            drawingView.setColor(color);

            scratchesCb.setChecked(false);
            dentsCb.setChecked(false);
        }
    }
});


dentsCb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        if(b)
        {
            color = 3;
            drawingView.setColor(color);

            chipsCb.setChecked(false);
            scratchesCb.setChecked(false);
        }
    }
});

DrawingView.java

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

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

private void init() {
    mPath=new Path();

    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeJoin(Paint.Join.MITER);
    mPaint.setStrokeWidth(5f);
    setColor(color);
}


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

    // Call invalidate
    invalidate();
}

答案 3 :(得分:0)

&#34;即使我传递一个静态值,在drawingview.setColor(int)的主要活动中说3,它也会给出黑色。这意味着Drawingview中的setColor函数不起作用。 &#34;

这是否意味着它会调用paint.setColor(3)

如果是,这当然会使你的颜色变黑。尝试传递Color.GREEN而不是

答案 4 :(得分:0)

在DrawingView中定义静态数据成员

static int color = 1; //默认

然后,从您的活动中直接致电

DrawingView.color = someValue;

变色前的

静态关键字将确保DrawingView类的所有对象只有一个变量引用。