ImageView继承自View类,它的功能用于显示图片, 或者显示Drawable对象
xml属性:
src和background区别 参考:http://hi.baidu.com/sunboy_2050/item/843aaed77256fc1620e250f9
区别一:图片透明度
两者都可以设置ImageView的背景
android:src:在设置ImageView的setAlpha()时有效果
android:background:在设置ImageView的setAlpha()时无效果
区别二:图片拉伸
background会根据ImageView的长宽进行拉伸,按照组件的大小来放大或者缩小图片。
src就存放的是原图的大小,不会进行拉伸,原图显示,不该变图片的大小
例如:ImageView的长宽都设置为:100px
1 background 引用图片
2 src 引用图片
其他属性参考文章:http://407827531.iteye.com/blog/1117199
属性名称 |
描述 |
|||||||||||||||||||||||||||
android:adjustViewBounds |
是否保持宽高比。需要与maxWidth、MaxHeight一起使用,否则单独使用没有效果。 |
|||||||||||||||||||||||||||
android:cropToPadding |
是否截取指定区域用空白代替。单独设置无效果,需要与scrollY一起使用,效果如下,实现代码见代码部分: |
|||||||||||||||||||||||||||
android:maxHeight |
设置View的最大高度,单独使用无效,需要与setAdjustViewBounds一起使用。如果想设置图片固定大小,又想保持图片宽高比,需要如下设置: 1) 设置setAdjustViewBounds为true; 2) 设置maxWidth、MaxHeight; 3) 设置设置layout_width和layout_height为wrap_content。 |
|||||||||||||||||||||||||||
android:maxWidth |
设置View的最大宽度。同上。 |
|||||||||||||||||||||||||||
android:scaleType |
设置图片的填充方式。
|
|||||||||||||||||||||||||||
android:src |
设置View的drawable(如图片,也可以是颜色,但是需要指定View的大小) |
|||||||||||||||||||||||||||
android:tint |
将图片渲染成指定的颜色。见下图:
|
下面给出一个实例代码: 实现图片透明度变化,以及缩放
public class ImageViewTest extends Activity { //定义一个访问图片的数组 int[] images = new int[]{ R.drawable.lijiang, R.drawable.qiao, R.drawable.shuangta, R.drawable.shui, R.drawable.xiangbi, }; //定义默认显示的图片 int currentImg = 2; //定义图片的初始透明度 private int alpha = 255; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final Button plus = (Button)findViewById(R.id.plus); final Button minus = (Button)findViewById(R.id.minus); final ImageView image1 = (ImageView)findViewById(R.id.image1); final ImageView image2 = (ImageView)findViewById(R.id.image2); final Button next = (Button)findViewById(R.id.next); //定义查看下一张图片的监听器 next.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (currentImg >= 4) { currentImg = -1; } BitmapDrawable bitmapDrawable = (BitmapDrawable) image1 .getDrawable(); //如果图片还未回收,先强制回收该图片,避免OutOfMemoryError异常 if (!bitmapDrawable.getBitmap().isRecycled()) { bitmapDrawable.getBitmap().recycle(); } //改变ImageView显示的图片 image1.setImageBitmap(BitmapFactory.decodeResource(getResources() , images[++currentImg])); } }); //定义改变图片透明度的方法 OnClickListener listener = new OnClickListener() { @Override public void onClick(View v) { if(v == plus) { alpha += 20; } if(v == minus) { alpha -= 20; } if(alpha >= 255) { alpha = 255; } if(alpha <= 0) { alpha = 0; } //改变图片的透明度 image1.setAlpha(alpha); } }; //为两个按钮添加监听器 plus.setOnClickListener(listener); minus.setOnClickListener(listener); //当点击image1图片某个部分,他就会显示在image2中 image1.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent event) { BitmapDrawable bitmapDrawable = (BitmapDrawable) image1 .getDrawable(); //获取第一个图片显示框中的位图 Bitmap bitmap = bitmapDrawable.getBitmap(); //bitmap图片实际大小与第一个ImageView的缩放比例 double scale = bitmap.getWidth() / 320.0; //获取需要显示的图片的开始点 int x = (int) (event.getX() * scale); int y = (int) (event.getY() * scale); if (x + 120 > bitmap.getWidth()) { x = bitmap.getWidth() - 120; } if (y + 120 > bitmap.getHeight()) { y = bitmap.getHeight() - 120; } //显示图片的指定区域 image2.setImageBitmap(Bitmap.createBitmap(bitmap, x, y, 120, 120)); image2.setAlpha(alpha); return false; } }); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="horizontal" > <Button android:id="@+id/plus" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="增大透明度" /> <Button android:id="@+id/minus" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="降低透明度" /> <Button android:id="@+id/next" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="下一张" /> </LinearLayout> <!-- 定义显示图片整体的ImageView --> <ImageView android:id="@+id/image1" android:layout_width="fill_parent" android:layout_height="240px" android:background="#0000ff" android:scaleType="fitCenter" android:src="@drawable/shuangta" /> <!-- 定义显示图片局部细节的ImageView --> <ImageView android:id="@+id/image2" android:layout_width="120dp" android:layout_height="120dp" android:layout_marginTop="10dp" android:background="#0000ff" /> </LinearLayout>