如何判断何时裁剪图像? [英] How do I tell when an image is clipped?

查看:115
本文介绍了如何判断何时裁剪图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个包含ImageViews的LinearLayout,我该如何编写代码来判断哪个(如果有的话)被屏幕边缘剪裁了?

If I have a LinearLayout containing ImageViews, how could I write code to tell which, if any, is clipped by the edge of the screen?

<LinearLayout android:id="@+id/imagecontainer"
              android:orientation="horizontal"
              android:layoutHeight="wrap_content"
              android:layoutWidth="fill_parent">

    <ImageView android:id="@+id/image1" .../>
    <ImageView android:id="@+id/image2" .../>

     ...

    <ImageView android:id="@+id/imageN" .../>

</LinearLayout>

我想像类似的东西,如果没有人被裁剪,它将返回一个索引或0.函数调用的语义并不是很重要……我只需要某种方法来判断是否存在裁剪,如果存在,那是谁?

I imagine something like, which would return an index or 0 if nobody is clipped. The semantics of the function call aren't really important... I just need some way to tell if there is clipping and if so, who is it?

int whichImageIsClipped(LinearLayout root) { ... }

推荐答案

这可能有些麻烦,但是您可以尝试

This may be a stretch, but you could try getGlobalVisibleRect(android.graphics.Rect, android.graphics.Point) on each of your children. If it returns false, you know it's completely out of view. If it returns true, you will need to compare the returned Rect with the expected size of your image.

能满足您的需求吗?

这是代码,以防万一有人需要它:

Here is the code, in case anyone needs it:

public static Boolean isViewClipped(View view) {
  Rect rect = new Rect();
  Boolean completelyObscured = !view.getGlobalVisibleRect(rect);
  return completelyObscured || rect.width() < view.getWidth();
}

这篇关于如何判断何时裁剪图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆