关于 onDraw 方法

About onDraw method

我想在屏幕上画矩形。但是我不会运行编程。因为,我写错了代码。因此,我不在下面写 MainActivity。你能帮我使用onDraw方法吗?

此致。

MainActivity.java

public class MainActivity extends AppCompatActivity {

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


}

}

Deneme.java

public class Deneme extends View {

Paint myPaint;

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

    myPaint = new Paint();
    myPaint.setColor(Color.BLACK);
    myPaint.setStyle(Paint.Style.FILL);
}

@Override
protected void  onDraw(Canvas canvas) {
    super.onDraw(canvas);
    Rect ourRect = new Rect();
    ourRect.set(0,0,canvas.getWidth(),canvas.getHeight()/2);
    canvas.drawRect(ourRect,myPaint);
}

}

你可以试试这个:

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;

public class Deneme extends View {

    private Rect mRectangle;
    private Paint mPaint;

    public Deneme(Context context) {
        super(context);
        int x = 50;
        int y = 50;
        int sideLength = 200;

        // create a rectangle that we'll draw later
        mRectangle = new Rect(x, y, sideLength, sideLength);

        // create the Paint and set its color        
        mPaint = new Paint();
        mPaint.setColor(Color.GRAY);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(Color.BLUE);
        canvas.drawRect(mRectangle, mPaint);
    }

}

快捷方式

还有你的activity:

import android.support.v7.app.AppCompatActivity;


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new Deneme(this));
    }

}

你也可以尝试这种优雅的方式:

在你的布局中activity_main:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main_relative_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.package.RectangleActivity">

</RelativeLayout>

在你的activity中:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.package.Deneme;
import com.package.R;

public class MainActivity extends AppCompatActivity {

    private RelativeLayout mRelativeLayout;

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

        //add RelativeLayout
        mRelativeLayout =(RelativeLayout) findViewById(R.id.activity_main_relative_layout);

        //add LayoutParams
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);

        Deneme rectangle = new Deneme(this);
        rectangle.setLayoutParams(params);
        mRelativeLayout.addView(rectangle);

    }
}

You'll find the supporting medium article!!! 这个例子在 GitHub

如果你想画一个矩形使用这个代码

public class Deneme extends View {

Paint myPaint;

public Deneme(Context context) {
    super(context);
    init();


}

public Deneme(Context context, AttributeSet attributeSet)
{
    super(context,attributeSet);
    init();
}

@Override
protected void  onDraw(Canvas canvas) {
    super.onDraw(canvas);
    Rect ourRect = new Rect();
    ourRect.set(0,0,canvas.getWidth(),canvas.getHeight()/2);
    canvas.drawRect(ourRect,myPaint);
}

public void init()
{
    myPaint = new Paint();
    myPaint.setColor(Color.BLACK);
    myPaint.setStyle(Paint.Style.FILL);
}
}

在设计 xml 文件时使用此 class 并且会出现矩形