2017-04-06 42 views
0

我需要在一個片段中畫一條線。我沒有問題,但我不知道爲什麼這條線有左右邊距(頂部和底部都很好)。Android畫布drawLine不是全寬

這裏是我的代碼:

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     context = container.getContext(); 
     utili = new Utilities(context); 
     View rootView = inflater.inflate(R.layout.statsdepth_layout, container, false); 
     gfxView = (ImageView) rootView.findViewById(R.id.gfxview); 
     int width = context.getResources().getDisplayMetrics().widthPixels; 
     int height = context.getResources().getDisplayMetrics().heightPixels; 
     Log.i("LOGTAG", "W: " + width + " H: " + height); 
     Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 
     Canvas canvas = new Canvas(bitmap); 

     Paint paint = new Paint(); 
     paint.setColor(Color.rgb(255, 153, 51)); 
     paint.setStrokeWidth(10); 
     int startx = 0; 
     int starty = 90; 
     int endx = width; 
     int endy = 90; 
     canvas.drawLine(startx, starty, endx, endy, paint); 
     gfxView.setImageBitmap(bitmap); 

     return rootView; 
    } 

這裏的XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/gfxview"/> 

</LinearLayout> 

**** *** UPDATE

如下更改佈局,它固定:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

<ImageView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/gfxview" 
    android:scaleType="matrix"/> 
</LinearLayout> 

這裏是結果(我exp ECT線路全寬度大)

enter image description here

+1

你能告訴你的根佈局?不是片段,活動。 – azizbekian

+0

根佈局是一個viewpager作爲佈局(match_parent寬度和高度)的活動。重視是非常漫長的。在片段中,我記錄了寬度和高度,並且這些值與屏幕大小相匹配。 – masgar

回答

0

讓您的寬度這樣並嘗試

 Display display = getWindowManager().getDefaultDisplay(); 
width = display.getWidth(); 
+0

沒有運氣,我不認爲是寬度問題,因爲我的startX是0,所以我期望水平線開始在屏幕的開始。 (和getWidth()已被棄用) – masgar

+0

你確定沒有填充給視圖尋呼機的任何父級佈局? – Journey

+0

是的,我敢肯定,即使viewpager的第一頁是一個列表視圖,寬度很好。 – masgar