为什么 Android 的自定义视图中没有调用 onDraw?

Why is the onDraw Not Being Called in Custom View for Android?

我正在 Android Canvas.

开发

但是 onDraw()invalidate();

之后还没有调用

layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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:orientation="vertical"
    tools:context="com.wen.MainActivity">

    <com.wen.Drawpath_view
        android:id="@+id/view_path"
        android:layout_weight="0.7"
        android:layout_width="match_parent"
        android:layout_height="0dp"/>

    <RelativeLayout
        android:layout_weight="0.3"
        android:layout_width="match_parent"
        android:layout_height="0dp">

        <Button
            android:id="@+id/test"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

    </RelativeLayout>

</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private static final String TAG = "MainActivity";
    private Drawpath_view View_path;
    private Button test_btn;

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

        View_path = findViewById(R.id.view_path);
        View_path = new Drawpath_view(MainActivity.this);

        test_btn = findViewById(R.id.test);
        test_btn.setOnClickListener(this);



        View_path.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {

                getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

                View_path.getViewTreeObserver().removeOnPreDrawListener(this);
               View_path.setMinimumHeight(View_path.getHeight());
                View_path.setMinimumWidth(View_path.getWidth());

                return true;

            }
        });
    }


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.test){

            View_path.Testdraw();

        }
    }
}

在客户View.

    public class Drawpath_view extends View {

        private static final String TAG = "view";
        private Paint paint;
        private Path path = new Path();

        public Drawpath_view(Context context) {
            super(context);
            setWillNotDraw(false);
            initView();
        }

        public Drawpath_view(Context context, AttributeSet attrs) {
            super(context, attrs);
        }


        private void initView() {
            paint = new Paint();
            paint.setAntiAlias(true);
            paint.setDither(true);
            paint.setStyle(Paint.Style.STROKE);
            paint.setColor(Color.GREEN);
            paint.setStrokeWidth(3);
        }

        public void Testdraw(){
            Log.d(TAG,"draw");
            path.moveTo(60,60);
            path.lineTo(460,460);
            invalidate();
        }

        @Override
        protected void onDraw(Canvas canvas) {
            Log.d(TAG,"onDraw");
            super.onDraw(canvas);
            canvas.drawPath(path,paint);
        }
    }

当我点击 Activity 中的按钮时。它会调用

        View_path.Testdraw();

但是Drawpath_view中的onDraw没有被调用。我错过了什么吗? 提前致谢。

默认所有 ViewGroup sub-类 不调用它们的 onDraw 方法,您将通过在 View_path 上调用 setWillNotDraw(false) 来启用它。

也将 setWillNotDraw(false); initView(); 添加到第二个构造函数中。

你可以删除 View_path = new Drawpath_view(MainActivity.this);从你的代码, 因为您已经在 xml.

中附加了视图