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

时间:2018-02-05 10:26:11

标签: java android canvas android-canvas ondraw

我正在开发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);
        }
    }

单击“活动”中的按钮。它会调用

        View_path.Testdraw();

onDraw中的Drawpath_view尚未被调用。我错过了什么吗? 提前谢谢。

3 个答案:

答案 0 :(得分:3)

您可以删除View_path = new Drawpath_view(MainActivity.this);从你的代码, 因为你已经在xml中附加了视图。

答案 1 :(得分:0)

默认情况下,所有ViewGroup子类都不会调用onDraw方法,您可以通过setWillNotDraw(false)上的View_path调用来启用它。

答案 2 :(得分:0)

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