Wiki_Android_获取ImageView的最大最小宽度和高度

来源:互联网 发布:免费装修软件app 编辑:程序博客网 时间:2024/06/06 01:44

一、通过ImageView对象直接获取,需考虑API的兼容问题,因为getMaxWidth()方法要API 16以上才可以使用

/* 获取ImageView的实际宽度* */int width = imageView.getWidth();int height = imageView.getHeight();

二、通过反射获取,这种方法不用考虑兼容API的问题

    /**     * 通过反射获取imageview的某个属性值     *     * @param object    ImageView对象     * @param fieldName mMaxWidth 最大宽度  mMaxHeight 最大高度     * @return     */    private static int getImageViewFieldValue(Object object, String fieldName) {        int value = 0;        try {            Field field = ImageView.class.getDeclaredField(fieldName);            field.setAccessible(true);            int fieldValue = field.getInt(object);            if (fieldValue > 0 && fieldValue < Integer.MAX_VALUE) {                value = fieldValue;            }        } catch (Exception e) {            e.printStackTrace();        }        return value;    }

调用:

 int width = getImageViewFieldValue(imageView, "mMaxWidth"); //最大宽度 int height = getImageViewFieldValue(imageView, "mMaxHeight");// 最大高度





0 0
原创粉丝点击