Android · 2017年5月2日 0

自定义ImageView 宽度自适应屏幕,高度等比缩放

实现方法很简单,根据图片文件的宽度与ImageView的宽度比例关系算出ImageView的高度。

package com.etongwl.commonlibs.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.NinePatchDrawable;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.ImageView;

/*
 * @Override
    public void setImageDrawable(final Drawable drawable) {
        super.setImageDrawable(drawable);
        if (drawable != null) {
            post(new Runnable() {
                @Override public void run() {
                    float mWidth = getWidth();
                    float pWidth = drawable.getBounds().width();
                    float pHeight = drawable.getBounds().height();
                    int mHeight = (int) ((mWidth / pWidth) * pHeight);

                    LayoutParams params = getLayoutParams();
                    params.height = mHeight;
                    params.width = (int) mWidth;
                    setLayoutParams(params);
                }
            });
        }
    }
    首先, 如果drawable不为空, 则通过drawable.getBounds()去获取drawable边界的宽和高. 在根据ImageView的宽与drawable的宽的比例去计算高度, 最后通过LayoutParams设置ImageView的高度
 *
 */

/*
<com.etongwl.commonlibs.view.AdaptiveImageView
          android:id="@+id/detail_image"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="center"
          android:layout_marginTop="15dp"
          android:scaleType="centerCrop"
          android:background="@drawable/diandianxinwen"
          android:visibility="visible" />
 */
/**
 * 按比例缩放
 */

@SuppressLint("AppCompatCustomView")
public class AdaptiveImageView extends ImageView {
    // 控件默认长、宽
    private int defaultWidth = 0;
    private int defaultHeight = 0;
    // 比例
    private float scale = 0;

    public AdaptiveImageView(Context context) {
        super(context);
    }

    public AdaptiveImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public AdaptiveImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override

    protected void onDraw(Canvas canvas) {
        Drawable drawable = getDrawable();
        if (drawable == null) {
            return;
        }

        if (getWidth() == 0 || getHeight() == 0) {
            return;
        }

        this.measure(0, 0);
        if (drawable.getClass() == NinePatchDrawable.class)
            return;

        Bitmap b = ((BitmapDrawable) drawable).getBitmap();
        Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);
        if (bitmap.getWidth() == 0 || bitmap.getHeight() == 0) {
            return;
        }
        if (defaultWidth == 0) {
            defaultWidth = getWidth();
        }
        if (defaultHeight == 0) {
            defaultHeight = getHeight();
        }
        scale = (float) defaultWidth / (float) bitmap.getWidth();
        defaultHeight = Math.round(bitmap.getHeight() * scale);
        ViewGroup.LayoutParams params = this.getLayoutParams();
        params.width = defaultWidth;
        params.height = defaultHeight;
        this.setLayoutParams(params);
        super.onDraw(canvas);
    }
}

简单实现ImageView宽度填满屏幕,高度自适应的两种方式

两种方式 1.重写View的onMeasure方法参考这里easion_zms的专栏 核心代码

protectedvoidonMeasure(intwidthMeasureSpec,intheightMeasureSpec){ Drawable d = getDrawable();if(d!=null){// ceil not round – avoid thin vertical gaps along the left/right edgesintwidth = MeasureSpec.getSize(widthMeasureSpec);//高度根据使得图片的宽度充满屏幕计算而得intheight = (int) Math.ceil((float) width * (float) d.getIntrinsicHeight() / (float) d.getIntrinsicWidth()); setMeasuredDimension(width, height); }else{super.onMeasure(widthMeasureSpec, heightMeasureSpec); }

2.设置ImageView的属性: //宽度填满屏幕 android:layout_width=”match_parent” android:scaleType=”fitXY” android:layout_height=”wrap_content” //保持比例,一定要设置 android:adjustViewBounds=”true”

 

Android ImageView图片自适应

<ImageView

   android:id=”@+id/dynamic_item_image”

   android:layout_width=”wrap_content”

   android:layout_height=”wrap_content”

   android:layout_gravity=”top”

   android:layout_marginTop=”5dip”

   android:adjustViewBounds=”true”

   android:background=”@drawable/imageview_background” />
另外,android:background=”@drawable/imageview_background”是给图片加了一个边框,其中

imageview_background.xml:
<?xml version=”1.0″ encoding=”utf-8″?> <shape  xmlns:android=”http://schemas.android.com/apk/res/android”> <solid android:color=”@color/white”/> <stroke android:width=”2.0dip” android:color=”#99D9D9D9″ /> <corners android:radius=”2.0dip” /> <padding android:left=”5.0dip” android:top=”5.0dip” android:right=”5.0dip” android:bottom=”5.0dip” /> </shape>

ImageView属性说明:

1、类概述

                显示任意图像,例如图标。ImageView类可以加载各种来源的图片(如资源或图片库),需要计算图像的尺寸,比便它可以在其他布局中使用,并提供例如缩放和着色(渲染)各种显示选项。

2、XML属性

属性名称

描述

android:adjustViewBounds

是否保持宽高比。需要与maxWidth、MaxHeight一起使用,否则单独使用没有效果。

android:cropToPadding

是否截取指定区域用空白代替。单独设置无效果,需要与scrollY一起使用,效果如下,实现代码见代码部分:

Android ImageView图片自适应 

 

android:maxHeight

设置View的最大高度,单独使用无效,需要与setAdjustViewBounds一起使用。如果想设置图片固定大小,又想保持图片宽高比,需要如下设置:

1) 设置setAdjustViewBounds为true;

2) 设置maxWidth、MaxHeight;

3) 设置设置layout_width和layout_height为wrap_content。

android:maxWidth

设置View的最大宽度。同上。

android:scaleType

设置图片的填充方式。

matrix

0

用矩阵来绘图

 

fitXY

1

拉伸图片(不按比例)以填充View的宽高

Android ImageView图片自适应

 

layout_

height

:30px

layout_

width

:120px

fitStart

2

按比例拉伸图片,拉伸后图片的高度为View的高度,且显示在View的左边

Android ImageView图片自适应

 

fitCenter

3

按比例拉伸图片,拉伸后图片的高度为View的高度,且显示在View的中间

Android ImageView图片自适应

 

fitEnd

4

按比例拉伸图片,拉伸后图片的高度为View的高度,且显示在View的右边

Android ImageView图片自适应

 

center

5

按原图大小显示图片,但图片宽高大于View的宽高时,截图图片中间部分显示Android ImageView图片自适应

 

 

layout_

height

:60px

layout_

width

:80px

padding

:10px

 

centerCrop

6

按比例放大原图直至等于某边View的宽高显示。Android ImageView图片自适应

 

centerInside

7

当原图宽高或等于View的宽高时,按原图大小居中显示;反之将原图缩放至View的宽高居中显示。Android ImageView图片自适应

 

android:src

设置View的drawable(如图片,也可以是颜色,但是需要指定View的大小)

android:tint

将图片渲染成指定的颜色。见下图:

Android ImageView图片自适应 左边为原图,右边为设置后的效果,见后面代码。

原文地址:http://blog.sina.com.cn/s/blog_618199e60100y537.html

 

android ImageView 宽度设定,高度自适应

首先,需要给你的ImageView布局加上Android:adjustViewBounds=”true”

<ImageView android:id=”@+id/test_image”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:scaleType=”fitXY”
android:adjustViewBounds=”true”
android:layout_gravity=”center”
android:contentDescription=”@string/app_name”
android:src=”@drawable/ic_launcher” />

 

然后,在代码里设置ImageView.最大宽度和最大高度,因为adjustViewBounds属性只有在设置了最大高度和最大宽度后才会起作用

int screenWidth = getScreenWidth(this);
ViewGroup.LayoutParams lp = testImage.getLayoutParams();
lp.width = screenWidth;
lp.height = LayoutParams.WRAP_CONTENT;
testImage.setLayoutParams(lp);

testImage.setMaxWidth(screenWidth);
testImage.setMaxHeight(screenWidth * 5); 这里其实可以根据需求而定,我这里测试为最大宽度的5倍

Share this: