2015-01-08 63 views
0

我试图在画布内创建一个矩形,以便它出现在屏幕的右侧,但每次运行应用程序似乎都会崩溃。我真的不知道我在这里做错了+我的代码中需要改变什么?矩形没有出现在右边

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

崩溃?请张贴你的logcat,今天早上我忘了我的水晶球...... – shkschneider

+2

为什么我们需要你的开发环境屏幕截图并停止消息? – weston

+0

为什么问这样的问题呢? – MacaronLover

回答

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。因为宽度为integer,并且canvas.getWidth()返回float,并且float-int返回循环浮动数字。

检查值width。是否如预期的那样?另外尝试手动设置值宽度,并查看问题是否存在。

希望这有助于解决麻烦!

看一看this question and its accepted answer

+0

那里应该有什么,为什么? – MacaronLover

+0

我的矩形在java中定义不是xml – MacaronLover

+0

我的矩形只有5dp的宽度(如我的代码所述) – MacaronLover

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

}

+0

没有。我的矩形需要5个像素宽,所以使用视图宽度不会有帮助+我希望我的视图的宽度和高度保持不变+颜色保持不变。 – MacaronLover

+0

宽度和高度根据您的要求,您可以更改.... – KomalG

+0

我不想改变任何该Komal。我想要的只是矩形被绘制在右边。 'paint.setColor(Color.RED); canvas.drawRect(0,0,5,canvas.getHeight(),paint);'这段代码在左边绘制它,所以显然这里需要改变。左边的第一个数字呢?毕竟,它代表了左侧的像素。 – MacaronLover