繁体   English   中英

如何防止Android裁剪ImageView?

[英]How can I prevent Android from cropping my ImageView?

我有一个ImageView,它将填充屏幕的整个宽度,并且还将像在屏幕底部的波浪一样上下动画。 我使用了scaleType =“ matrix”,因为它不应该拉伸以适合屏幕。

但是,一旦进行了布局,Android就会在布局更新时将外部的所有内容都裁剪掉,因此一旦开始为底部进行动画处理,就会丢失。

所以我的问题是,如何防止Android裁剪ImageView?

我写了一个看来可以解决问题的课程,以为如果其他人遇到这个问题,我也会分享。

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.ImageView;

public class NoCropImageView extends ImageView
{
    private int fixedWidth = 0;
    private int fixedHeight = 0;

    private int[] attrsArray = new int[] { android.R.attr.layout_width, android.R.attr.layout_height };

    public NoCropImageView(final Context context, final AttributeSet attrs)
    {
        super(context, attrs);

        TypedArray ta = context.obtainStyledAttributes(attrs, attrsArray);
        int layout_width = ta.getDimensionPixelSize(0, ViewGroup.LayoutParams.MATCH_PARENT);
        int layout_height = ta.getDimensionPixelSize(1, ViewGroup.LayoutParams.MATCH_PARENT);
        ta.recycle();

        fixedWidth = layout_width;
        fixedHeight = layout_height;
    }

    @Override
    protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec)
    {
        this.setMeasuredDimension(fixedWidth, fixedHeight);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM