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中
 

一、public voidsetVisibility (int visibility)

visibility

One of VISIBLEINVISIBLE, or GONEhtml

可是在調用此方法的時候java

image.setVisibility(visibility)android

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

即:windows

image.setVisibility(0x00000000) / image.setVisibility(View.VISIBLE);// 表示顯示; image.setVisibility(0x00000004) / image.setVisibility(View.INVISIBLE);//表示隱藏; image.setVisibility(0x00000008) / image.setVisibility(View.GONE);//表示view不存在。

二、設置顏色的不一樣方法eclipse

color.rgb(255,255,255);ide

color.RED;post

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

三、設置圖片指定大小code

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 用矩陣來繪製,動態縮小放大圖片來顯示。


在res/drawable文件夾下建立一個xml文件

imageview_define.xml

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@*android:drawable/pressed_true" /> <item android:state_pressed="false" android:drawable="@*android:drawable/pressed_false" /> </selector>

而後,在定義imageView的xml文件裏面設置

 

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageView android:id="@+id/ImageView" android:src="@drawable/youPicture" android:background="@drawable/imageview_define" /> </LinearLayout>

 把下面的XML保存成.xml文件(好比list_item_bg.xml),運行時系統會根據ListView中列表項的狀態來使用相應的背景圖片

 

<?xml version="1.0" encoding="utf-8" ?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 默認時的背景圖片 --> <item android:drawable="@drawable/pic1" /> <!-- 沒有焦點時的背景圖片 --> <item android:state_window_focused="false" android:drawable="@drawable/pic1" /> <!-- 非觸摸模式下得到焦點並單擊時的背景圖片 --> <item android:state_focused="true" android:state_pressed="true" android:drawable= "@drawable/pic2" /> <!-- 觸摸模式下單擊時的背景圖片 --> <item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/pic3" /> <!--選中時的圖片背景 --> <item android:state_selected="true" android:drawable="@drawable/pic4" /> <!--得到焦點時的圖片背景 --> <item android:state_focused="true" android:drawable="@drawable/pic5" /> </selector>

 

使用方法

  • 第一種是在listview中配置android:listSelector=」@drawable/list_item_bg」
  • 第二種是在listview的item中添加屬性android:background=」@drawable/list_item_bg」
  • 第三種是java代碼中使用:
    Drawable drawable = getResources().getDrawable(R.drawable.list_item_bg); listview.setSelector(drawable);

注:列表有時候爲黑的狀況,須要加上下面的代碼使其透明:

android:cacheColorHint="@android:color/transparent"
  imageView1.setScaleType(ImageView.ScaleType.CENTER);//縮放類型
相關文章
相關標籤/搜索