Android 安卓帆布抽绳不全宽
我需要在片段中画一条线。我没有问题,但我不知道为什么这条线有一个左右边距(顶部和底部都可以) 这是我的密码:Android 安卓帆布抽绳不全宽,android,android-canvas,android-bitmap,Android,Android Canvas,Android Bitmap,我需要在片段中画一条线。我没有问题,但我不知道为什么这条线有一个左右边距(顶部和底部都可以) 这是我的密码: @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { context = container.getContext(); utili = new Utilities(context);
@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>
结果如下(我希望这条线是全宽的)
以这种方式获得您的宽度,然后再试一次
Display display = getWindowManager().getDefaultDisplay();
width = display.getWidth();
你能显示你的根布局吗?不是片段,而是活动。根布局是一个以viewpager作为布局(匹配父级宽度和高度)的活动。连接起来很长。在片段中,我记录了宽度和高度,值与屏幕大小匹配。运气不好,我认为宽度没有问题,因为我的startX为0,所以我希望水平线从屏幕的开始处开始。(getWidth()已被弃用)您确定视图分页器的任何父布局都没有填充吗?是的,我确定,即使是因为视图分页器的第一页是listview且宽度很好。我将ImageView缩放类型设置为matrix,并将其修复。这是不确定的。