Android ImageView 總結【轉載】

來源:互聯網
上載者:User
Android ImageView 總結【轉載】   一.介紹 ImageView用來顯示任意映像圖片,可以自己定義顯示尺寸,顯示顏色等等. 二.XML屬性 android:adjustViewBounds 是否保持寬高比。需要與maxWidth、MaxHeight一起使用,單獨使用沒有效果。 android:cropToPadding 是否截取指定地區用空白代替。單獨設定無效果,需要與scrollY一起使用  android:maxHeight 定義View的最大高度,需要與AdjustViewBounds一起使用,單獨使用沒有效果。如果想設定圖片固定大小,又想保持圖片寬高比,需要如下設定: 1) 設定AdjustViewBounds為true; 2) 設定maxWidth、MaxHeight; 3) 設定設定layout_width和layout_height為wrap_content。 android:maxWidth 設定View的最大寬度。 android:scaleType 設定圖片的填充方式。 android:src 設定View的圖片或顏色 android:tint 將圖片渲染成指定的顏色。
--------------------------------------------------------------------------------------------- 使用Martix(android.graphics.Matrix)類中的postScale()方法結合Bitmap來實現縮放圖片的功能

Bitmap bmp = BitmapFactory.decodeResource(getResource(),R.drawalbe.icon1)

int bmpwidth = bmp.getWidth();

int bmpheight = bmp.getHeight();

Matrix matrix = new Matrix();

matrix.postScale(width,height);

Bitmap bm = Bitmap.createBitmap(bmp,0,0,bmpwidth,bmpheight ,matrix,true);

imageView.setImageBitmap(bm);


在Android中不允許ImageView在產生後,動態修改其長度和寬度,

所以要實現圖片發大縮小的功能,必須將原來的ImageView移除,

重新產生一個新的ImageView,並且指定圖片來源給它,再放入Layout中

--------------------------------------------------------------------------------------------------

1、public voidsetVisibility (int visibility)
visibility

One of VISIBLEINVISIBLE,
or GONE

但是在調用此方法的時候

image.setVisibility(visibility)

其中visibility是int型的參數。對應上面:VISIBLE=0x00000000;INVISIBLE=0x00000004;GONE=0x00000008。

即:

image.setVisibility(0x00000000)   /  image.setVisibility(View.VISIBLE); 表示顯示;

image.setVisibility(0x00000004)  /  image.setVisibility(View.INVISIBLE);表示隱藏;

image.setVisibility(0x00000008)  /  image.setVisibility(View.GONE);表示view不存在。

2、設定顏色的不同方法

color.rgb(255,255,255);

color.RED;

color.parseColor(colorString); 其中colorString可以是:#RRGGBB #AARRGGBB 'red', 'blue', 'green', 'black', 'white', 'gray', 'cyan', 'magenta', 'yellow', 'lightgray', 'darkgray' 等

3、設定圖片指定大小

protected Bitmap scaleImg(Bitmap bm, int newWidth, int newHeight) {
    // 圖片源
    // Bitmap bm = BitmapFactory.decodeStream(getResources()
    // .openRawResource(id));
    // 獲得圖片的寬高
    int width = bm.getWidth();
    int height = bm.getHeight();
    // 設定想要的大小
    int newWidth1 = newWidth;
    int newHeight1 = newHeight;
    // 計算縮放比例
    float scaleWidth = ((float) newWidth1) / width;
    float scaleHeight = ((float) newHeight1) / height;
    // 取得想要縮放的matrix參數
    Matrix matrix = new Matrix();
    matrix.postScale(scaleWidth, scaleHeight);
    // 得到新的圖片
    Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix,
      true);
    return newbm;

   }

調用:

獲得18×18的圖片

Bitmap bm = BitmapFactory.decodeStream(getResources().openRawResource(R.drawable.icon));

Bitmap newBm = scaleImg(bmImg , 18, 18);

imageView.setImageBitmap(newBm);

android:scaleType:

  android:scaleType是控製圖片如何resized/moved來匹對ImageView的size。ImageView.ScaleType / android:scaleType值的意義區別:

  CENTER /center 按圖片的原來size置中顯示,當圖片長/寬超過View的長/寬,則截取圖片的置中部分顯示

  CENTER_CROP / centerCrop 按比例擴大圖片的size置中顯示,使得圖片長(寬)等於或大於View的長(寬)

  CENTER_INSIDE / centerInside 將圖片的內容完整置中顯示,通過按比例縮小或原來的size使得圖片長/寬等於或小於View的長/寬

  FIT_CENTER / fitCenter 把圖片按比例擴大/縮小到View的寬度,置中顯示

  FIT_END / fitEnd 把圖片按比例擴大/縮小到View的寬度,顯示在View的下部分位置

  FIT_START / fitStart 把圖片按比例擴大/縮小到View的寬度,顯示在View的上部分位置

  FIT_XY / fitXY 把圖片 不按比例 擴大/縮小到View的大小顯示

  MATRIX / matrix 用矩陣來繪製,動態縮小放大圖片來顯示。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.