本节引言:

本节介�的UI基础控件是:ImageView(图�视图),��知�,就是用�显示图�的一个View或者说控件� 官方API:ImageView;本节讲解的内容如下:

  1. ImageView的src属性和blackground的区别;
  2. adjustViewBounds设置图�缩放时是�按长宽比
  3. scaleType设置缩放类型
  4. 最简�的绘制圆形的ImageView

1.src属性和background属性的区别:

在API文档中我们�现ImageView有两个�以设置图片的属性,分别是:src和background

常识:

①background通常指的都是背景,而src指的是内容!!

②当使用src填入图片时,是按照图片大�直接填充,并�会进行拉伸

而使用background填入图片,则是会根�ImageView给定的宽度�进行拉伸

1)写代�验�区别:

写个简�的布局测试下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:id="@+id/LinearLayout1"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:orientation="vertical"  
    tools:context="com.jay.example.imageviewdemo.MainActivity" >  
  
    <ImageView  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:background="@drawable/pen" />  
  
    <ImageView  
        android:layout_width="200dp"  
        android:layout_height="wrap_content"  
        android:background="@drawable/pen" />  
  
    <ImageView  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:src="@drawable/pen" />  
  
    <ImageView  
        android:layout_width="200dp"  
        android:layout_height="wrap_content"  
        android:src="@drawable/pen" />  
  
</LinearLayout> 

效果图如下:

结果分�:

宽高都是wrap_content那就一样,是原图大�,但是,当我们固定了宽或者高的�, 差别就显而易�了,blackground完全填充了整个ImageView,而src�旧是那么大, 而且他居中了哦,这就涉�到了ImageView的�一个属性scaleType了! �外还有一点,这里我们说了�设置width或者height哦�加入我们�时设置了 width和height的�,blackground�旧填充,但是,src的大��能�生改�哦! 比如,我们测试下下�这段代�:

<ImageView  
        android:layout_width="100dp"  
        android:layout_height="50dp"  
        android:src="@drawable/pen" />

�行效果图:

PS:scaleType下�会讲~


2)解决blackground拉伸导致图片�形的方法

在��的效果图中的第二个Imageview中我们�以看到图片已�被拉伸�形了, 正方形��了长方形,对于和我一样有轻微强迫症的人�说,显然是��接�的, 有没有办法去设置呢?答案肯定是有的,笔者暂时知�的有以下两�方�:

  • 这个适用于动æ€�加载ImageViewçš„,代ç �也简å�•,å�ªè¦�在添加View的时候,把大å°�写写死就å�¯ä»¥äº†

    LinearLayout.LayoutParams layoutParam = new LinearLayout.LayoutParams(48, 48);    
            layout.addView(ibtnPen, layoutParam); 
    
  • 除了动æ€�加载view,更多的时候,我们还是会通过xml布局的方å¼�引入ImageViewçš„ 解决方法也ä¸�éš¾,就是通过drawableçš„Bitmap资æº�文件æ�¥å®Œæˆ�,ç„¶å�Žblackground属性设置为该文件å�³å�¯! 这个xml文件在drawable文件夹下创建,这个文件夹是è¦�自己创建的哦!ï¼�

pen_bg.xml:

<bitmap  
    xmlns:android="http://schemas.android.com/apk/res/android"  
    android:id="@id/pen_bg"  
    android:gravity="top"  
    android:src="@drawable/pen"  
    android:tileMode="disabled" >  
</bitmap>

上述代�并�难�解,估计大家最迷惑的是titleMode属性�,这个属性是平铺,就是我们windows设置 背景时候的平铺,多个�图标铺满整个�幕��记得了���记得自己�以试试!disabled就是把他给�止了!

就是上�这串简�的代�,至于调用方法如下:

动�: ibtnPen.setBacklgroundResource(R.drawable.penbg);

��: android:background = "@drawable/penbg"


3)设置�明度的问题

说完��两个区别,接��说下setAlpha属性咯!这个很简�,这个属性,�有src时�是有效果的!!


4)两者结�妙用:

网上的一张图:

一看去是一个简�的GridView,�个item都是一个ImageView,但是细心的你�能�现了, 上�的ICON都�是规则的,而是圆形,圆角矩形等等,于是乎这里用到了src + background了� �实现上述的效果,你�需�两个�作: 找一张�明的png图片 + 设置一个黑色的背景 (当然你也�以设置png的�明度�实现,�过结果�能和预想的有出入哦!) 我们写个简�例�:

如图,呆�呆�的�猪就这样显示到ImageView上了,哈哈,blackground设置了�色背景!

实现代�:

<ImageView  
    android:layout_width="150dp"  
    android:layout_height="wrap_content"  
    android:src="@drawable/pig"  
    android:background="#6699FF" /> 

PS: 当然你也�以用selctor实现点击效果,设置��的情况设置��的图片,以实现点击或者触摸效果!


5)Java代�中设置blackground和src属性:

�景(对应src属性):setImageDrawable( );
背景(对应background属性):setBackgroundDrawable( );


2.adjustViewBounds设置缩放是��存原图长宽比

ImageView为我们�供了adjustViewBounds属性,用于设置缩放时是���原图长宽比� �独设置�起作用,需���maxWidth和maxHeight属性一起使用�而��这两个属性 也是需�adjustViewBounds为true�会生效的~

  • android:maxHeight:设置ImageView的最大高度
  • android:maxWidth:设置ImageView的最大宽度

代�示例:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <!-- 正常的图片 -->
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5px"
        android:src="@mipmap/meinv" />
    <!-- �制了最大宽度与高度,并且设置了调整边界���所显示图�的长宽比-->
    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5px"
        android:adjustViewBounds="true"
        android:maxHeight="200px"
        android:maxWidth="200px"
        android:src="@mipmap/meinv" />

</LinearLayout>

�行效果图:

结果分�: 大的那个图片是没有任何处�的图片,尺寸是:541374;而下�的那个的�我们通过maxWidth和maxHeight �制ImageView最大宽度与高度为200px,就是最多�能显示200200的图片,我们�设置了一个 adjustViewBounds = "true"调整我们的边界���图片的长宽比,此时的ImageView宽高为是128*200~


3.scaleType设置缩放类型

android:scaleType用于设置显示的图片如何缩放或者移动以适应ImageView的大� Java代�中�以通过imageView.setScaleType(ImageView.ScaleType.CENTER);�设置~ �选值如下:

  • fitXY:对图åƒ�的横å�‘与纵å�‘进行独立缩放,使得该图片完全适应ImageView,但是图片的横纵比å�¯èƒ½ä¼šå�‘生改å�˜
  • fitStart:ä¿�æŒ�纵横比缩放图片,知é�“较长的边与Image的编程相等,缩放完æˆ�å�Žå°†å›¾ç‰‡æ”¾åœ¨ImageView的左上角
  • fitCenter:å�Œä¸Š,缩放å�Žæ”¾äºŽä¸­é—´;
  • fitEnd:å�Œä¸Š,缩放å�Žæ”¾äºŽå�³ä¸‹è§’;
  • center:ä¿�æŒ�原图的大å°�,显示在ImageView的中心。当原图的size大于ImageViewçš„size,超过部分è£�剪处ç�†ã€‚
  • centerCrop:ä¿�æŒ�横纵比缩放图片,知é�“完全覆盖ImageView,å�¯èƒ½ä¼šå‡ºçŽ°å›¾ç‰‡çš„æ˜¾ç¤ºä¸�完全
  • centerInside:ä¿�æŒ�横纵比缩放图片,直到ImageView能够完全地显示图片
  • matrix:默认值,ä¸�改å�˜åŽŸå›¾çš„å¤§å°�,从ImageView的左上角开始绘制原图, 原图超过ImageView的部分作è£�剪处ç�†

接下�我们一组组的�对比:


1)1.fitEnd,fitStart,fitCenter

这里以fitEnd为例,其他两个类似:

示例代�:

<!-- ��图片的横纵比缩放,知�该图片能够显示在ImageView组件上,并将缩放好的图片显示在imageView的�下角 -->
    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="300px"
        android:layout_height="300px"
        android:layout_margin="5px"
        android:scaleType="fitEnd"
        android:src="@mipmap/meinv" />

�行效果图:


2)centerCrop与centerInside

  • centerCrop:按横纵比缩放,直接完全覆盖整个ImageView
  • centerInside:按横纵比缩放,使得ImageView能够完全显示这个图片

示例代�:

<ImageView
        android:layout_width="300px"
        android:layout_height="300px"
        android:layout_margin="5px"
        android:scaleType="centerCrop"
        android:src="@mipmap/meinv" />

    <ImageView
        android:layout_width="300px"
        android:layout_height="300px"
        android:layout_margin="5px"
        android:scaleType="centerInside"
        android:src="@mipmap/meinv" />

�行效果图:


3)fitXY

�按比例缩放图片,目标是把图片塞满整个View

示例代�:

<ImageView
        android:layout_width="300px"
        android:layout_height="300px"
        android:layout_margin="5px"
        android:scaleType="fixXY"
        android:src="@mipmap/meinv" />

�行效果图:

好�,明显�了=-=~


4)matrix

从ImageView的左上角开始绘制原图,原图超过ImageView的部分作�剪处�

示例代�:

<ImageView
        android:layout_width="300px"
        android:layout_height="300px"
        android:layout_margin="5px"
        android:scaleType="matrix"
        android:src="@mipmap/meinv" />

�行效果图:


5)center

��原图的大�,显示在ImageView的中心。当原图的size大于ImageView的size,超过部分�剪处�。

示例代�:

<ImageView
        android:layout_width="300px"
        android:layout_height="300px"
        android:layout_margin="5px"
        android:scaleType="center"
        android:src="@mipmap/meinv" />

�行效果图:


4.最简�的绘制圆形的ImageView

相信大家对圆形或者圆角的ImageView�陌生�,现在很多的APP都很喜欢圆形的头�是�~

这里就简�的写个圆形的ImageView�,当然这�是一个示例,��考虑性能与抗锯齿的情况下���

�以说是写�玩玩,实际项目的��以考虑用Github上牛人写的控件,比如下�这两个:

git怎么玩��已�教过大家了~把项目clone下�把相关文件�制到自己的项目��~

RoundedImageView

CircleImageView

代�示例:

�行效果图:

实现代�:

自定义ImageView:**RoundImageView.java

package com.jay.demo.imageviewdemo;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.Region;
import android.util.AttributeSet;
import android.widget.ImageView;

/**
 * Created by coder-pig on 2015/7/18 0018.
 */
public class RoundImageView extends ImageView {

    private Bitmap mBitmap;
    private Rect mRect = new Rect();
    private PaintFlagsDrawFilter pdf = new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG);
    private Paint mPaint = new Paint();
    private Path mPath=new Path();
    public RoundImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }


    //传入一个Bitmap对象
    public void setBitmap(Bitmap bitmap) {
        this.mBitmap = bitmap;
    }


    private void init() {
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
        mPaint.setAntiAlias(true);// 抗锯尺
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(mBitmap == null)
        {
            return;
        }
        mRect.set(0,0,getWidth(),getHeight());
        canvas.save();
        canvas.setDrawFilter(pdf);
        mPath.addCircle(getWidth() / 2, getWidth() / 2, getHeight() / 2, Path.Direction.CCW);
        canvas.clipPath(mPath, Region.Op.REPLACE);
        canvas.drawBitmap(mBitmap, null, mRect, mPaint);
        canvas.restore();
    }
}

布局代�:activity_main.xml:

<com.jay.demo.imageviewdemo.RoundImageView
        android:id="@+id/img_round"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_margin="5px"/>

MainActivity.java:

package com.jay.demo.imageviewdemo;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private RoundImageView img_round;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        img_round = (RoundImageView) findViewById(R.id.img_round);
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.meinv);
        img_round.setBitmap(bitmap);
    }
}

本节�结:

本节讲解了ImageView(图�视图),内容看上很多,�过都是一些详述性的东西,知���~ 最�的自定义圆形ImageView也是,�是写�玩玩的,实际项目中还是建议使用那两个第三方的自定义控件�~