我需要在一个片段中画一条线。我没有问题,但我不知道为什么这条线有左右边距(顶部和底部都很好)。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线路全宽度大)
你能告诉你的根布局?不是片段,活动。 – azizbekian
根布局是一个viewpager作为布局(match_parent宽度和高度)的活动。重视是非常漫长的。在片段中,我记录了宽度和高度,并且这些值与屏幕大小相匹配。 – masgar