简体   繁体   中英

How can I prevent Android from cropping my ImageView?

I have an ImageView, which will fill the entire width of the screen and will also be animated up and down like a wave at the bottom of the screen. I have used scaleType="matrix" since it should not stretch to fit within the screen.

However, once it has been layouted, Android crops everything away that was outside at the time of layout-update, so once it starts to animate the bottom part is missing.

So my question is, how can I prevent Android from cropping my ImageView?

I wrote a class that seems to solve the problem, thought I´d share it if someone else faces this problem..

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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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