Android canvas drawLine不是全宽

时间:2017-04-06 15:47:49

标签: android android-canvas android-bitmap

我需要在片段中画一条线。我没有问题,但我不知道为什么这条线有左右边距(顶部和底部都很好)。

这是我的代码:

@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>

****更新***

更改布局如下,修复:

<?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>

这是结果(我希望这条线是全宽的)

enter image description here

1 个答案:

答案 0 :(得分:0)

以这种方式获得宽度并尝试

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