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);
可是在調用此方法的時候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>
Drawable drawable = getResources().getDrawable(R.drawable.list_item_bg); listview.setSelector(drawable);
注:列表有時候爲黑的狀況,須要加上下面的代碼使其透明:
android:cacheColorHint="@android:color/transparent"
imageView1.setScaleType(ImageView.ScaleType.CENTER);//縮放類型