矩形没有出现在右边

时间:2015-01-08 12:58:05

标签: java android xml android-activity

我正在尝试在画布中创建一个矩形,使其显示在屏幕的右侧,但每次运行时应用程序似乎都会崩溃。我真的不知道我在这里做错了什么+在我的代码中需要改变什么?

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

2 个答案:

答案 0 :(得分:0)

试试这个..

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

MyView类

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

}

答案 1 :(得分:0)

我之前的回答是因为我对你的问题缺乏了解。很抱歉给您带来不便,如果有的话。我不确定我是否可以帮助你,但试试这个。

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

在上面的代码中,在最后一行,而不是canvas.getWidth()-width,键入canvas.getWidth()-(float) width。因为width是integer而canvas.getWidth()返回float并且float-int返回一个重复的浮点数。

检查width的值。这是预期的吗?还可以尝试手动设置值宽度,看看问题是否仍然存在。

希望这有助于解决问题!

查看this question and its accepted answer