【问题标题】:Passing value from activity to view class将值从活动传递到视图类
【发布时间】:2018-01-19 02:44:57
【问题描述】:

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

在 DrawingView 类中,我必须使用选中的颜色在画布上进行绘制。我想要的是从活动传递一个整数值到视图类,并相应地设置油漆的颜色。尝试使用 getter 和 setter,但我得到黑色。我想这是因为颜色是在构造函数本身中设置的,当我选中任何框时它不会改变。

以下代码更新请参考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;
}

编辑

我不一定想使用完全相同的代码。我想要的只是在选择复选框以能够在图像视图上绘图时更改油漆颜色。欢迎任何其他方法。

【问题讨论】:

  • 这个 setColor() 只会更新可变颜色值,但不会对视图产生任何影响,因为您从未使用新颜色使视图无效。
  • 同法?
  • 还是不行
  • 您能概括一下您的 DrawingView 课程并在评论中分享链接吗?

标签: android view android-imageview


【解决方案1】:

在 MainActivity 中,您正在创建一个与显示的图像视图无关的 DrawingView。因此,当您更改颜色时,您不会更改显示的图像视图的颜色,而是更改未连接的DrawingView。图像视图永远不会定义新颜色,并且始终默认为黑色。

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

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

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>

【讨论】:

  • 做了同样的事情,仍然出现黑线。
  • @Harshita 上面的代码确实有效。您还在 MainActivity 中创建 DrawingView 吗?您能否发布带有上述建议的更改的代码的最新迭代,其中仍然有包括 XML 在内的黑线?
  • 好的,请一分钟
  • 我没有添加所有代码,因为它很长。但添加了与此问题相关的所有内容。如有遗漏,请告诉我。gist.github.com/anonymous/a0427f35c566d6a167497352a5002e13
  • @Harshita 我看到了你发布的新代码。由于错误,该代码将无法运行。例如,carImageView 必须是 final 才能在匿名类中被引用。您还将onTouch() 侦听器埋在不属于carImageView 的侦听器中。如果您可以发布 1) 编译和运行的代码以及 2) 展示您试图解决的不良行为,这将有所帮助。见this。我已经确定了这个问题。我们只需要将其翻译成您的代码即可。
【解决方案2】:

你需要改变函数 setColor。

1.更改 mPaint 颜色。

2.为重绘视图添加invalidate()。

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

【讨论】:

    【解决方案3】:

    您需要在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();
    }
    

    【讨论】:

    • 我还是黑了而已。
    • 即使我传递了一个静态值,比如在 drawingview.setColor(int) 的主要活动中为 3,它也会给出黑色。这意味着 Drawingview 中的 setcolor 功能不起作用。
    【解决方案4】:

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

    静态 int 颜色 = 1; //默认

    然后,从您的活动中只需调用

    DrawingView.color = someValue;

    可变颜色之前的静态关键字将确保您的 DrawingView 类的所有对象只有一个变量引用。

    【讨论】:

      【解决方案5】:

      “即使我传递了一个静态值,比如 drawingview.setColor(int) 的主要活动中的 3,它也会显示为黑色。这意味着 Drawingview 中的 setColor 函数不起作用。”

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

      如果是,这当然会使您的颜色变黑。尝试改为传递Color.GREEN

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-04
        • 2016-03-01
        • 1970-01-01
        • 2015-07-04
        • 2012-01-24
        相关资源
        最近更新 更多