700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Android ImageView图片显示点击背景切换

Android ImageView图片显示点击背景切换

时间:2019-08-16 20:03:17

相关推荐

Android  ImageView图片显示点击背景切换

为什么80%的码农都做不了架构师?>>>

一.介绍 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)

但是在调用此方法的时候

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 用矩阵来绘制,动态缩小放大图片来显示。

在res/drawable文件夹下创建一个xml文件

imageview_define.xml

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="/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="/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="/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);//缩放类型

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。