使用预定义的度量以编程方式膨胀视图

2024-02-02

我有一个像这样的布局资源,我想用布局宽度和高度来填充它:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="75px"
    android:layout_height="25px">

    <TextView
        android:id="@+id/drawable_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="Name" />

    <TextView
        android:id="@+id/drawable_description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/drawable_name"
        android:layout_alignParentLeft="true"
        android:gravity="center_horizontal"
        android:layout_below="@+id/drawable_name"
        android:text="Some Text" />

</RelativeLayout>

视图可以是任何东西,我将其转换为位图。

private Bitmap getBitmap(View v){
    v.measure(MeasureSpec.makeMeasureSpec(mDrawableWidth, MeasureSpec.EXACTLY),
              MeasureSpec.makeMeasureSpec(mDrawableHeight, MeasureSpec.EXACTLY));
    v.layout(0, 0, mDrawableWidth, mDrawableHeight);
    Bitmap returnedBitmap = Bitmap.createBitmap(mDrawableWidth, mDrawableHeight,Bitmap.Config.ARGB_8888);
    Canvas c=new Canvas(returnedBitmap);
    v.draw(c);
    return returnedBitmap;
}

到目前为止,我已经对宽度和高度进行了硬编码,并且我希望能够以编程方式执行此操作,访问layout_width and layout_height。 有什么办法可以实现这一点吗? 如果有另一种方法可以使用此值来扩展视图而不在度量中指定它们,请告诉我。

如果我创建自定义视图,是否有机会指定固定的宽度和高度?


这个例子应该可以工作。根据您的需要,可能需要一些调整才能使其完美,但请尝试一下:

//Get a bitmap from a layout resource. Inflates it into a discarded LinearLayout
//so that the LayoutParams are preserved
public static Bitmap getLayoutBitmap (Context c, int layoutRes, int maxWidth, int maxHeight) {
    View view = LayoutInflater.from(c).inflate(layoutRes, new LinearLayout(c), false);
    return getViewBitmap(view, maxWidth, maxHeight);
}

public static Bitmap getViewBitmap (View v, int maxWidth, int maxHeight) {
    ViewGroup.LayoutParams vParams = v.getLayoutParams();

    //If the View hasn't been attached to a layout, or had LayoutParams set
    //return null, or handle this case however you want
    if (vParams == null) {
        return null;
    }

    int wSpec = measureSpecFromDimension(vParams.width, maxWidth);
    int hSpec = measureSpecFromDimension(vParams.height, maxHeight);

    v.measure(wSpec, hSpec);

    final int width = v.getMeasuredWidth();
    final int height = v.getMeasuredHeight();

    //Cannot make a zero-width or zero-height bitmap
    if (width == 0 || height == 0) {
        return null;
    }

    v.layout(0, 0, width, height);

    Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(result);
    v.draw(canvas);

    return result;
}

private static int measureSpecFromDimension (int dimension, int maxDimension) {
    switch (dimension) {
        case ViewGroup.LayoutParams.MATCH_PARENT:
            return View.MeasureSpec.makeMeasureSpec(maxDimension, View.MeasureSpec.EXACTLY);
        case ViewGroup.LayoutParams.WRAP_CONTENT:
            return View.MeasureSpec.makeMeasureSpec(maxDimension, View.MeasureSpec.AT_MOST);
        default:
            return View.MeasureSpec.makeMeasureSpec(dimension, View.MeasureSpec.EXACTLY);
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用预定义的度量以编程方式膨胀视图 的相关文章

随机推荐