关于onDraw方法

时间:2017-02-17 09:42:23

标签: android paint ondraw rect

我想在屏幕上绘制矩形。但是我没有运行程序。因为,我写错了代码。因此,我不是在下面编写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);
}

}

2 个答案:

答案 0 :(得分:0)

你可以试试这个:

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);
    }

}

快捷方式

您的活动:

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>

在您的活动中:

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

中的这个例子

答案 1 :(得分:0)

如果要绘制矩形,请使用此代码

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文件时使用此类,并出现矩形