我需要在一個片段中畫一條線。我沒有問題,但我不知道爲什麼這條線有左右邊距(頂部和底部都很好)。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