微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – 如何根据小部件高度缩放图像视图但是尊重最大高度?

我目前正在开发一个包含图像视图的Android小部件.
该小部件应该可以调整大小.

图像应该填充小部件的整个宽度(这是容易的部分)并且应该填充小部件高度的30%,同时尊重最大高度80dip(这是我无法弄清楚的).

在以下布局中,图像适当缩放到宽度,但不适合高度:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/widget"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/widget_margin">

        <ImageView
            android:id="@+id/promoshelf"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/shelf"
            android:scaleType="fitXY"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true" />

</RelativeLayout>

我尝试使用带权重的LinearLayout来完成此操作,这可以在下面的代码片段中看到.但是,它看起来像android:maxHeight在使用权重时不受尊重.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/widget"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/widget_margin">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="bottom"
        android:orientation="vertical"
        android:weight_sum="1">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:maxHeight="80dip"
            android:src="@drawable/my_image"
            android:scaleType="fitXY"
            android:layout_weight="0.3" />

    </LinearLayout>
</RelativeLayout>

所以,这是(不需要的)结果:

为什么maxHeight不起作用或者是否有人知道如何规避这个?

解决方法:

我不确定是否有一个只使用标准组件的好解决方案.但是可以使用自定义ImageView小部件轻松完成.

public class MyImageView extends ImageView {
    private float weight = 0.3f;
    private int maxHeight;

    public MyImageView(Context context, AttributeSet attrs) {
        super(context, attrs);

        if (attrs != null) {
            final int[] ids = {android.R.attr.maxHeight};
            final TypedArray array = context.obtainStyledAttributes(attrs, ids);

            if (array != null) {
                maxHeight = array.getDimensionPixelSize(0, 0);
                array.recycle();
            }
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (maxHeight > 0) {
            final int height = MeasureSpec.getSize(heightMeasureSpec);
            final int result = Math.min(Math.round(height * weight), maxHeight);

            heightMeasureSpec = MeasureSpec.makeMeasureSpec(result, MeasureSpec.EXACTLY);
        }

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

然后在你的布局中:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#0f0">

    <com.example.MyImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:background="#f00"
        android:maxHeight="80dip"/>

</RelativeLayout>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐