2017-04-06 39 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