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);//缩放类型
相关文章
相关标签/搜索