99re热这里只有精品视频,7777色鬼xxxx欧美色妇,国产成人精品一区二三区在线观看,内射爽无广熟女亚洲,精品人妻av一区二区三区

Android ImageView(圖像視圖)

2023-03-31 13:38 更新

本節(jié)引言:

本節(jié)介紹的UI基礎(chǔ)控件是:ImageView(圖像視圖),見名知意,就是用來顯示圖像的一個View或者說控件! 官方API:ImageView;本節(jié)講解的內(nèi)容如下:

  1. ImageView的src屬性和blackground的區(qū)別;
  2. adjustViewBounds設(shè)置圖像縮放時是否按長寬比
  3. scaleType設(shè)置縮放類型
  4. 最簡單的繪制圓形的ImageView

1.src屬性和background屬性的區(qū)別:

在API文檔中我們發(fā)現(xiàn)ImageView有兩個可以設(shè)置圖片的屬性,分別是:src和background

常識:

①background通常指的都是背景,而src指的是內(nèi)容!!

②當(dāng)使用src填入圖片時,是按照圖片大小直接填充,并不會進(jìn)行拉伸

而使用background填入圖片,則是會根據(jù)ImageView給定的寬度來進(jìn)行拉伸

1)寫代碼驗(yàn)證區(qū)別:

寫個簡單的布局測試下:

<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> 

效果圖如下:

結(jié)果分析:

寬高都是wrap_content那就一樣,是原圖大小,但是,當(dāng)我們固定了寬或者高的話, 差別就顯而易見了,blackground完全填充了整個ImageView,而src依舊是那么大, 而且他居中了哦,這就涉及到了ImageView的另一個屬性scaleType了! 另外還有一點(diǎn),這里我們說了只設(shè)置width或者h(yuǎn)eight哦!加入我們同時設(shè)置了 width和height的話,blackground依舊填充,但是,src的大小可能發(fā)生改變哦! 比如,我們測試下下面這段代碼:

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

運(yùn)行效果圖:

PS:scaleType下面會講~


2)解決blackground拉伸導(dǎo)致圖片變形的方法

在前面的效果圖中的第二個Imageview中我們可以看到圖片已經(jīng)被拉伸變形了, 正方形變成了長方形,對于和我一樣有輕微強(qiáng)迫癥的人來說,顯然是不可接受的, 有沒有辦法去設(shè)置呢?答案肯定是有的,筆者暫時知道的有以下兩種方式:

  • 這個適用于動態(tài)加載ImageView的,代碼也漸漸,只要在添加View的時候,把大小寫死就可以了

    LinearLayout.LayoutParams layoutParam = new LinearLayout.LayoutParams(48, 48);    
            layout.addView(ibtnPen, layoutParam); 
  • 除了動態(tài)加載view,更多的時候,我們還是會通過xml布局的方式引入ImageView的 解決方法也不難,就是通過drawable的Bitmap資源文件來完成,然后blackground屬性設(shè)置為該文件即可! 這個xml文件在drawable文件夾下創(chuàng)建,這個文件夾是要自己創(chuàng)建的哦!!

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設(shè)置 背景時候的平鋪,多個小圖標(biāo)鋪滿整個屏幕捏!記得了吧!不記得自己可以試試!disabled就是把他給禁止了!

就是上面這串簡單的代碼,至于調(diào)用方法如下:

動態(tài): ibtnPen.setBacklgroundResource(R.drawable.penbg);

靜態(tài): android:background = "@drawable/penbg"


3)設(shè)置透明度的問題

說完前面兩個區(qū)別,接著再說下setAlpha屬性咯!這個很簡單,這個屬性,只有src時才是有效果的!!


4)兩者結(jié)合妙用:

網(wǎng)上的一張圖:

一看去是一個簡單的GridView,每個item都是一個ImageView,但是細(xì)心的你可能發(fā)現(xiàn)了, 上面的ICON都不是規(guī)則的,而是圓形,圓角矩形等等,于是乎這里用到了src + background了! 要實(shí)現(xiàn)上述的效果,你只需要兩個操作: 找一張透明的png圖片 + 設(shè)置一個黑色的背景 (當(dāng)然你也可以設(shè)置png的透明度來實(shí)現(xiàn),不過結(jié)果可能和預(yù)想的有出入哦!) 我們寫個簡單例子:

如圖,呆萌呆萌的小豬就這樣顯示到ImageView上了,哈哈,blackground設(shè)置了藍(lán)色背景!

實(shí)現(xiàn)代碼:

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

PS: 當(dāng)然你也可以用selctor實(shí)現(xiàn)點(diǎn)擊效果,設(shè)置不同的情況設(shè)置不同的圖片,以實(shí)現(xiàn)點(diǎn)擊或者觸摸效果!


5)Java代碼中設(shè)置blackground和src屬性:

前景(對應(yīng)src屬性):setImageDrawable( );
背景(對應(yīng)background屬性):setBackgroundDrawable( );


2.adjustViewBounds設(shè)置縮放是否保存原圖長寬比

ImageView為我們提供了adjustViewBounds屬性,用于設(shè)置縮放時是否保持原圖長寬比! 單獨(dú)設(shè)置不起作用,需要配合maxWidthmaxHeight屬性一起使用!而后面這兩個屬性 也是需要adjustViewBounds為true才會生效的~

  • android:maxHeight:設(shè)置ImageView的最大高度
  • android:maxWidth:設(shè)置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" />
    <!-- 限制了最大寬度與高度,并且設(shè)置了調(diào)整邊界來保持所顯示圖像的長寬比-->
    <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>

運(yùn)行效果圖:

結(jié)果分析: 大的那個圖片是沒有任何處理的圖片,尺寸是:541374;而下面的那個的話我們通過maxWidth和maxHeight 限制ImageView最大寬度與高度為200px,就是最多只能顯示200200的圖片,我們又設(shè)置了一個 adjustViewBounds = "true"調(diào)整我們的邊界來保持圖片的長寬比,此時的ImageView寬高為是128*200~


3.scaleType設(shè)置縮放類型

android:scaleType用于設(shè)置顯示的圖片如何縮放或者移動以適應(yīng)ImageView的大小 Java代碼中可以通過imageView.setScaleType(ImageView.ScaleType.CENTER);來設(shè)置~ 可選值如下:

  • fitXY:對圖像的橫向與縱向進(jìn)行獨(dú)立縮放,使得該圖片完全適應(yīng)ImageView,但是圖片的橫縱比可能會發(fā)生改變
  • fitStart:保持縱橫比縮放圖片,知道較長的邊與Image的編程相等,縮放完成后將圖片放在ImageView的左上角
  • fitCenter:同上,縮放后放于中間;
  • fitEnd:同上,縮放后放于右下角;
  • center:保持原圖的大小,顯示在ImageView的中心。當(dāng)原圖的size大于ImageView的size,超過部分裁剪處理。
  • centerCrop:保持橫縱比縮放圖片,知道完全覆蓋ImageView,可能會出現(xiàn)圖片的顯示不完全
  • centerInside:保持橫縱比縮放圖片,直到ImageView能夠完全地顯示圖片
  • matrix:默認(rèn)值,不改變原圖的大小,從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" />

運(yùn)行效果圖:


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" />

運(yùn)行效果圖:


3)fitXY

不按比例縮放圖片,目標(biāo)是把圖片塞滿整個View

示例代碼:

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

運(yùn)行效果圖:

好吧,明顯扁了=-=~


4)matrix

從ImageView的左上角開始繪制原圖,原圖超過ImageView的部分作裁剪處理

示例代碼:

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

運(yùn)行效果圖:


5)center

保持原圖的大小,顯示在ImageView的中心。當(dāng)原圖的size大于ImageView的size,超過部分裁剪處理。

示例代碼:

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

運(yùn)行效果圖:


4.最簡單的繪制圓形的ImageView

相信大家對圓形或者圓角的ImageView不陌生吧,現(xiàn)在很多的APP都很喜歡圓形的頭像是吧~

這里就簡單的寫個圓形的ImageView吧,當(dāng)然這只是一個示例,再不考慮性能與抗鋸齒的情況下?。?!

可以說是寫來玩玩,實(shí)際項目的話可以考慮用Github上牛人寫的控件,比如下面這兩個:

git怎么玩前面已經(jīng)教過大家了~把項目clone下來把相關(guān)文件復(fù)制到自己的項目即可~

RoundedImageView

CircleImageView

代碼示例:

運(yùn)行效果圖:

實(shí)現(xiàn)代碼:

自定義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);
    }
}

本節(jié)小結(jié):

本節(jié)講解了ImageView(圖像視圖),內(nèi)容看上很多,不過都是一些詳述性的東西,知道即可~ 最后的自定義圓形ImageView也是,只是寫來玩玩的,實(shí)際項目中還是建議使用那兩個第三方的自定義控件吧~

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號