2015-01-08 91 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