简体   繁体   English

如何防止Android裁剪ImageView?

[英]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. 我有一个ImageView,它将填充屏幕的整个宽度,并且还将像在屏幕底部的波浪一样上下动画。 I have used scaleType="matrix" since it should not stretch to fit within the screen. 我使用了scaleType =“ matrix”,因为它不应该拉伸以适合屏幕。

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

So my question is, how can I prevent Android from cropping my ImageView? 所以我的问题是,如何防止Android裁剪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);
    }
}

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

相关问题 如何防止 ImageView 以编程方式调整大小以适应屏幕? - How can I prevent an ImageView from resizing to fit the screen programmatically? 如何防止任何人与我的服务器通信,除了我的Android应用程序 - How can I prevent anyone from communicating with my server except my Android app Android:如何防止图像在ImageView或ImageButton中缩放? - Android: How to prevent image from being scaled in ImageView or ImageButton? 我想知道如何在Android Studio上检查imageview的颜色 - I want to know how I can check the color of my imageview on Android Studio 如何防止imageview中的图像被裁剪为正方形? - How do I prevent images in imageview from being cropped to a square? Java FXML:为什么我的imageView没有显示,如何从计算机上载图像? - Java FXML: Why is my imageView not Showing, and how can I upload an image from my computer? 如何将Cache目录中的文件放入Android的ImageView中? - How do I put a File from my Cache directory in an ImageView on Android? 如何防止我的jComboBox重置? - How can I prevent my jComboBox from resetting? 如何防止Thyemleaf对模板进行urlencoding? - How can I prevent Thyemleaf from urlencoding my template? 如何防止ArrayList在Java GUI中重置? - How can I prevent my ArrayList from resetting in a Java GUI?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM