android基本控件学习-----ImageView

ImageView的讲解

一、src和background的区别

background我们通常理解是背景,而src是内容,当使用src填入图片的时候是以图片的大小直接填充,并不会进行拉伸,而background填入图片会根据指定的大小对图片进行缩放拉伸。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#ffffff">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@mipmap/ic_launcher"/>
    <ImageView
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:background="@mipmap/ic_launcher"/>
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"/>
    <ImageView
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"/>

</LinearLayout>

当我们设置宽和高都是wrap_content的时候background和src都是原图片的大小,但是当我们将layout_height设置了高度的时候差别就显然可见,我们会发现src属性的时候图片的宽度没有那么宽,这时图片就会自动居中,同时如果宽和高设置都不能满足图片的要求的时候这时就是涉及到了图片缩放了ScaleType属性。同时如果两者结合使用还有其他的一些效果。

二.设置图片的透明度

  前面所过两个属性的区别,现在我们聊一下如何设置一个图片的透明度,很简单,我们只需要在Java代码调用setAlpha方法就可以了

但是我们需要注意的是,这个方法只能对src属性的控件有作用,参数是float类型的。

三、adjustViewBounds设置缩放是否保持原图的长宽比例

  这个属性单独的使用没有什么作用,我们需要同过maxHeight和maxWidth结合使用才能起作用

  maxHeight:是imageView的最大高度

  maxeWidth:是imageView的最大宽度

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#ffffff">
    <ImageView
        android:id="@+id/image1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/women"
        />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:src="@mipmap/women"
        android:adjustViewBounds="true"
        android:maxHeight="200dp"
        android:maxWidth="200dp"/>
</LinearLayout>

四、图片的缩放ScaleType

android:ScaleType通过设置缩放或者移动的类型来满足ImageView的大小,同样我们也可以在Java代码中调用imageView

.setScaleType(ImageView.ScaleType.CENTER)进行缩放或者移动,可选参数:

fitXY:对图片进行纵横比例进行缩放,是图片能够完全适应ImageView,但是纵横比例可能会发生变化。

fitStart:对图片进行纵横比进行缩放,以致可以是ImageView(适应imageView中高度或者宽度较小的一个),在缩放变化的过程中纵横比不会发生变化,并且将缩放好的图片放在ImageView的左上角。

fitCenter:同fitStart基本一样,只是最后将缩放好的图片放在ImageView的中间。

fitEnd:同fitStart也基本一样,只是最后将缩放好的图片放在ImageView的右下角。

center:保持原图的大小,显示在ImageView的中心,原图的大小超过ImageView设定的大小的时候需要图片进行裁剪。

centerCrop:保持原图纵横缩放比例,直到完全覆盖ImageView,可能会出现图片不完整的现象。

centerInside:保持原图纵横缩放比例,直到ImageView可以完全的显示出来。

matrix:默认值,不改变图片的大小,从ImageView的左上角开始绘制图片,原图超过ImageView的时候回对图片进行裁剪。

上面的解释应该已经很清楚,如果有兴趣就可以自己动手去试试。

五、自定义圆形的ImageView

package com.example.test3;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
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-tu on 2016/1/7.
 */
public class RoundImageView extends ImageView{
    private Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.women);
    private Rect rect = new Rect();
    private PaintFlagsDrawFilter filter = new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG);
    private Paint paint = new Paint();
    private Path path = new Path();

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

    public RoundImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
    private void init() {
        paint.setStyle(Paint.Style.STROKE);
        paint.setFlags(Paint.ANTI_ALIAS_FLAG);
//       抗锯齿
        paint.setAntiAlias(true);
    }
//    传入一个bitmap对象
    public void setBitmap(Bitmap bitmap) {
        this.bitmap = bitmap;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (bitmap == null){
            return;
        }
        rect.set(0,0,getWidth(),getHeight());
        canvas.save();
        canvas.setDrawFilter(filter);
        path.addCircle(getWidth() / 2, getWidth() / 2, getHeight() / 2, Path.Direction.CCW);
        canvas.clipPath(path, Region.Op.REPLACE);
        canvas.drawBitmap(bitmap, null, rect, paint);
        canvas.restore();
    }
}

在布局文件中使用

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#ffffff">
    <com.example.test3.RoundImageView
        android:id="@+id/image1"
        android:layout_width="200dp"
        android:layout_height="200dp"/>
</LinearLayout>

效果图

 

posted @ 2016-01-07 19:44  会飞的一只狼  阅读(537)  评论(0编辑  收藏  举报