在右侧绘制矩形

Drawing rectangles on the right

下面是在距左侧 0 像素处绘制矩形的代码,但是如何设置在距屏幕右侧 0 像素处绘制矩形?

    paint.setColor(Color.RED);
    canvas.drawRect(0, 0, 5, canvas.getHeight(), paint );

activity_main.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <TextView android:text="@string/hello_world" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <package.name.customview.Diagram
        android:id="@+id/diagram"
        android:layout_width="fill_parent"
        android:layout_height="52dp" />

</LinearLayout>

Diagram.java

package com.apptacularapps.customview;

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

public class Diagram extends View {
    Paint paint = new Paint();

    public Diagram(Context context) {
        super(context);
    }

    @Override
    public void onDraw(Canvas canvas) {
        paint.setColor(Color.RED);
        canvas.drawRect(0, 0, 5, canvas.getHeight(), paint );

        paint.setColor(Color.RED);
        canvas.drawRect(canvas.getWidth() - 5, 0, 5, canvas.getHeight(), paint );
    }
}

新建 class - DrawView.java

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

    public class DrawView extends View {
        Paint paint = new Paint();

        public DrawView(Context context) {
            super(context);            
        }

        @Override
        public void onDraw(Canvas canvas) {

    //Code to Measure the Screen width in pixels

    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width = size.x;

            paint.setColor(Color.RED);
    canvas.drawRect(0, 0, 5, canvas.getHeight(), paint );

    paint.setColor(Color.RED);
    canvas.drawRect(width-canvas.getWidth(), 0, width, canvas.getHeight(), paint );
        }
}

在您的 Diagram.java 中,如下更改您的 onDraw 方法。

@Override
    public void onDraw(Canvas canvas) {
        DrawView drawView = new DrawView(this);
        drawView.setBackgroundColor(Color.WHITE);
        setContentView(drawView);
    }

更新 - 要解决您的错误,试试这个

如果您不在 Activity 中,您可以通过 WINDOW_SERVICE:

获取默认显示
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();

而不是

Display display = getWindowManager().getDefaultDisplay();

更新 2 - 解决上下文错误。

在此行 Paint paint = new Paint();

下方添加此行 Context context;

在此行 super(context);

下方添加此行 this.context = context;

更新 3 - 使用此代码示例

Diagram.java

包com.casestudy.intentsandfilter; 导入 android.app.Activity; 导入 android.graphics.Color; 导入 android.os.Bundle;

public class Diagram extends Activity {
    DrawView drawView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        drawView = new DrawView(this);
        drawView.setBackgroundColor(Color.WHITE);
        setContentView(drawView);

    }
}

DrawView.java

package com.casestudy.intentsandfilter;

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

public class DrawView extends View {
    Paint paint = new Paint();

    public DrawView(Context context) {
        super(context);            
    }

    @Override
    public void onDraw(Canvas canvas) {
        paint.setColor(Color.BLACK);
        paint.setStrokeWidth(3);
        canvas.drawRect(30, 30, 80, 80, paint);
        paint.setStrokeWidth(0);
        paint.setColor(Color.CYAN);
        canvas.drawRect(33, 60, 77, 77, paint );
        paint.setColor(Color.YELLOW);
        canvas.drawRect(33, 33, 77, 60, paint );

    }

}