在右边绘制矩形

时间:2015-01-07 22:16:41

标签: java android android-activity android-studio

下面是从左边开始绘制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 );
    }
}

1 个答案:

答案 0 :(得分:0)

创建一个新类 - 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);
    }

更新 - 要解决您的错误,请尝试

如果您不在活动中,可以通过WINDOW_SERVICE获取默认显示:

WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();

而不是

Display display = getWindowManager().getDefaultDisplay();

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

在此行Context context;

下方添加此行Paint paint = new Paint();

在此行this.context = context;

下方添加此行super(context);

更新3 - 使用此代码示例

Diagram.java

package com.casestudy.intentsandfilter; import android.app.Activity; import android.graphics.Color; import 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 );

    }

}