【问题标题】:Method to swap colors of custom rectangle view does not work交换自定义矩形视图颜色的方法不起作用
【发布时间】:2022-01-11 15:18:10
【问题描述】:

我正在学习有关自定义视图的教程,并且正在与讲师一起编写代码,但是我交换自定义视图颜色的方法不起作用。

这是我的 xml 布局:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="20dp"
    tools:context=".MainActivity">


    <com.laila.android.customviews.views.CustomRectangle
        android:id="@+id/my_custom_rectangle"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    </com.laila.android.customviews.views.CustomRectangle>

    <Button
        android:id="@+id/btn_swap"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Swap color"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

这是我的自定义视图类:

public class CustomRectangle extends View {

    private static final int SQUARE_SIZE = 200;
    private Rect myRect;
    private Paint rectPaint;

    public CustomRectangle(Context context) {
        super(context);

        init(null);
    }

    public CustomRectangle(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        init(attrs);
    }

    public CustomRectangle(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        init(attrs);
    }

    public CustomRectangle(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);

        init(attrs);
    }

    // Initialization method
    private void init(AttributeSet attrs) {

        myRect = new Rect();
        rectPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    }

    public void swapColor() {

        // Reminder: ternary conditional operator -> ?
        // boolean statement ? true result : false result;

        rectPaint.setColor(rectPaint.getColor() == Color.GREEN ? Color.RED : Color.GREEN);

        postInvalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        myRect.left = 50;
        myRect.top = 50;
        myRect.right = myRect.left + SQUARE_SIZE;
        myRect.bottom = myRect.top + SQUARE_SIZE;

        rectPaint.setColor(Color.GREEN);

        canvas.drawRect(myRect, rectPaint);

    }
}

这是我的主要活动:

public class MainActivity extends AppCompatActivity {

    private CustomRectangle customRectangle;
    private Button buttonSwap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        customRectangle = findViewById(R.id.my_custom_rectangle);
        buttonSwap = findViewById(R.id.btn_swap);

        buttonSwap.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                customRectangle.swapColor();
            }
        });
    }
}

这是应用程序的外观:

但是当我点击交换按钮时,什么也没有发生。为什么?我忘记了什么或者我做错了什么?

谢谢。

【问题讨论】:

    标签: java android view android-custom-view


    【解决方案1】:

    我想通了。我在onDraw 方法中调用了rectPaint.setColor(Color.GREEN),所以每次重新绘制画布时,颜色都会设置为绿色。

    我所要做的就是将 rectPaint.setColor(Color.GREEN) 放在我的 init 方法中,就像这样:

        private void init(AttributeSet attrs) {
    
            myRect = new Rect();
            rectPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    
            rectPaint.setColor(Color.GREEN);
        }
    

    现在交换颜色方法起作用了。

    【讨论】:

      猜你喜欢
      • 2023-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-24
      • 2021-03-17
      • 2021-04-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多