从活动传递值到视图类

9
我有一个活动,其中有3个不同视图中的复选框。这些复选框用于选择颜色。
在DrawingView类中,我必须使用选定的颜色在画布上绘制。我想要的是从活动中传递一个整数值到视图类,并相应地设置绘画的颜色。我尝试使用getter和setter,但我得到的是黑色。我认为这是因为颜色在构造函数中被设置,当我勾选任何框时它不会改变。
请参阅下面的代码更新:此处 代码:
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;
}

编辑

我并不一定想使用完全相同的代码。我只想在选择复选框时改变绘画颜色,以便能够在图像视图上绘制。欢迎采用任何其他方法。


1
这个setColor()函数只会更新变量color的值,但是不会影响视图,因为你从未使用新颜色使视图失效。 - TheLittleNaruto
在同一个方法中吗? - TheLittleNaruto
仍然不起作用。 - Harshita
https://gist.github.com/anonymous/16140c59c001f22c2ffbf78f4dcfd488 - Harshita
同样的代码?我只得到黑线。尝试过调试,复选框的值被传递到drawingview类中的setColor方法,但它的值为0。请帮忙扩展一些帮助。 - Harshita
显示剩余5条评论
5个回答

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
做了完全相同的事情,仍然出现黑线。 - Harshita
1
@Harshita 上面的代码确实可以工作。你还在MainActivity中创建DrawingView吗?你能否发布最新版本的代码,包括XML和上述建议的更改,但仍保留黑线? - Cheticamp
我没有添加全部代码,因为它非常长。但添加了与此问题相关的所有内容。如果还有遗漏,请告诉我。https://gist.github.com/anonymous/a0427f35c566d6a167497352a5002e13 - Harshita
我应该创建一个新的Gist并粘贴整个代码吗? - Harshita
我已经放置了一个可工作的代码,没有任何绘图或其他东西。我经验不太丰富,请相应地帮助/合作。如果这不是正确的分享方式,请告诉我。 - Harshita
显示剩余5条评论

1
你需要更改函数setColor。
1.更改mPaint的颜色。
2.添加invalidate()以重新绘制视图。
public void setColor(int color){
    this.color=color;
     mPaint.setColor(color);
    invalidate();
}

0
你需要在 View 上调用 invalidate() 方法来更新它。
试试这个,
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();
}

1
我仍然只能得到黑色。 - Harshita
即使我在主活动中传递一个静态值,比如3,在drawingview.setColor(int)中它仍然是黑色。这意味着Drawingview中的setcolor函数没有起作用。 - Harshita

0
即使我在主活动中传递静态值,比如3,在Drawingview中使用drawingview.setColor(int)会变成黑色。这意味着Drawingview中的setColor函数没有起作用。
这是否意味着它将调用paint.setColor(3)
如果是的话,这肯定会把你的颜色变成黑色。尝试传递Color.GREEN

0

在你的DrawingView中定义静态数据成员

static int color = 1; //默认值

然后,从你的活动中简单地调用

DrawingView.color = someValue;

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接