0

I'm trying to create a rectangle within a canvas so that it appears on the right hand side of the screen but the app seems to crash every time I run it. I seriously don't know am I doing wrong here + what needs to change within my code?

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" />

    <com.apptacularapps.customview.DrawView
        android:id="@+id/diagram"
        android:layout_width="fill_parent"
        android:layout_height="52dp" />

</LinearLayout>

MainActivity.java

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends ActionBarActivity {

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

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

DrawView.java

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

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

    public DrawView(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    public void onDraw(Canvas canvas) {

        //Code to Measure the Screen width in pixels

        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.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(canvas.getWidth()-width, 0, 5, canvas.getHeight(), paint );
    }
}
wbk727
  • 8,017
  • 12
  • 61
  • 125

2 Answers2

0

try this..

<com.example.mystackoverflow.MyView
    android:id="@+id/myview"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_alignParentTop="true" />

MyView class

public class MyView extends View {

private Paint mPaint;
private int view_width, view_height;

public MyView(Context context, AttributeSet attrs) {
    super(context, attrs);
    mPaint = new Paint();
    mPaint.setColor(getResources().getColor(color.Red));
    mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    //canvas.drawRect(0, 0, view_width, view_height, mPaint);
      canvas.drawRect(view_width/2, 0, view_width, view_height, mPaint);
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    super.onLayout(changed, left, top, right, bottom);
    view_width = right - left;
    view_height = bottom - top;
}

}

KomalG
  • 808
  • 9
  • 18
  • Nope. My rectangle needs to be 5 pixels wide so using view width will not help + I want the width and height of my view to stay the same + the colour to remain the same also. – wbk727 Jan 08 '15 at 13:13
  • I don't want to change any of that Komal. All I want is for the rectangle to be drawn on the right. `paint.setColor(Color.RED); canvas.drawRect(0, 0, 5, canvas.getHeight(), paint );` This code draws it on the left so obviously something here needs to change. What about the first number on the left? After all, it represents pixels from the left. – wbk727 Jan 08 '15 at 13:22
  • how much width you want..... have you tried my code. oncetry it you can change myview in the layout as according to ur requirement. In the onLayout i am calculating the view width and height from xml layout... – KomalG Jan 08 '15 at 13:27
  • I want the rectangle to have a width of 5 and I want my view to have the width of the screen (`fill_parent`). I will be adding more shapes later on. – wbk727 Jan 08 '15 at 13:49
  • Don't forget to make the pixel independent from resolution. See DisplayMetrics for additional information. – Martin Pfeffer Jan 08 '15 at 14:26
  • What I am trying to achieve is this (the image under **Final desired result**) and so if you think there is a better / more efficient way of doing this then please let me know. Many thanks. :-) – wbk727 Jan 08 '15 at 18:43
  • i have done changes in ondraw() and in XML file once try the updated code canvas.drawRect(view_width/2, 0, view_width, view_height, mPaint); in xml: android:layout_width="match_parent" – KomalG Jan 09 '15 at 05:30
0

My earlier answer was because of my lack of understanding of your problem. Sorry for the inconvenience caused, if any. I am not sure if i can help you, but try this.

public void onDraw(Canvas canvas) {

    //Code to Measure the Screen width in pixels

    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.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(canvas.getWidth()-width, 0, 5, canvas.getHeight(), paint );
}

In the above piece of code, in the last line, instead of canvas.getWidth()-width, type canvas.getWidth()-(float) width. Because width is integer and canvas.getWidth() returns float and float-int returns a recurring floating number.

Check the value of width. Is it as expected ? Also try setting the value width manually and see if the problem persists.

Hope this helps and solves the trouble !

Have a look at this question and its accepted answer.

Community
  • 1
  • 1
Pranit Bankar
  • 453
  • 1
  • 7
  • 21