将位图或ImageView的宽度和高度设置为CustomView

时间:2019-06-03 17:16:27

标签: java android view android-custom-view

我正在尝试获取Bitmap Width Height 并将它们设置为CustomView的布局参数

JAVA:

 private void setBg() {
        drawView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        drawView.getLayoutParams().height = orginalBitmap.getHeight();
        drawView.getLayoutParams().width = orginalBitmap.getWidth();
        relativeLayout.addView(drawView);
    }

XML:

   <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/bottom"
        android:layout_below="@+id/clear">

        <ImageView
            android:id="@+id/fullimg"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:layout_gravity="center_horizontal|center_vertical"
            android:adjustViewBounds="true" />

        <RelativeLayout
            android:id="@+id/relative"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:layout_gravity="center_horizontal|center_vertical"
            android:gravity="center_horizontal|center_vertical"
            android:orientation="vertical" />
    </RelativeLayout>

但是上面的代码不起作用,当我运行应用程序时,我得到了

enter image description here

例如,

表情符号应在图像视图的一角停下来,但它仍然出现在两个空白处。

2 个答案:

答案 0 :(得分:1)

改为使用此方法,您必须以这种方式设置布局参数,然后将其添加到drawView

 private void setBg(){

    android.view.ViewGroup.LayoutParams params = drawView.getLayoutParams();
    params.width = orginalBitmap.getWidth();
    params.height = orginalBitmap.getHeight();
    drawView.setLayoutParams(params);
    relativeLayout.addView(drawView);

   }

答案 1 :(得分:1)

这就是我所做的,而且效果很好

  mDensity = getResources().getDisplayMetrics().density;
    actionBarHeight = (int) (110 * mDensity);
    bottombarHeight = (int) (60 * mDensity);
    viewWidth = getResources().getDisplayMetrics().widthPixels;
    viewHeight = getResources().getDisplayMetrics().heightPixels - actionBarHeight - bottombarHeight;
    viewRatio = (double) viewHeight / (double) viewWidth;
    bmRatio = (double) orginalBitmap.getHeight() / (double) orginalBitmap.getWidth();
    if (bmRatio < viewRatio) {
        bmWidth = viewWidth;
        bmHeight = (int) (((double) viewWidth) * ((double) (orginalBitmap.getHeight()) / (double) (orginalBitmap.getWidth())));
    } else {
        bmHeight = viewHeight;
        bmWidth = (int) (((double) viewHeight) * ((double) (orginalBitmap.getWidth()) / (double) (orginalBitmap.getHeight())));
    }
    final RelativeLayout.LayoutParams lparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    lparams.addRule(RelativeLayout.CENTER_IN_PARENT);
    lparams.height = bmHeight;
    lparams.width = bmWidth;
    drawView.setLayoutParams(lparams);
相关问题