Android 图像视图ImageView

清疚 2022-06-05 03:08 334阅读 0赞

ImageView是用来显示图像的控件,它和图形显示有关的属性主要有下面两个:

1.scaleType :图形的拉伸类型,默认是fitCenter。

2.色彩:图形来源,src图形按照scaleType拉伸,注意背景图不安scaleType指定的方式进行拉伸,而且背景默认的是fitXY的方式进行拉伸显示。

至于拉伸的类型和效果,我们直接通过示例来看,这比我在这里列出来要直观的多。

首先,我们看先布局页面,很简单,一个ImageView,其余是7个Button,分别对应不同的拉伸效果,点击后显示不同的效果。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical" android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. <ImageView
  6. android:id="@+id/iv_img"
  7. android:layout_width="match_parent"
  8. android:layout_height="wrap_content"
  9. android:src="@drawable/search"
  10. android:scaleType="center"
  11. android:layout_weight="1"/>
  12. <LinearLayout
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content"
  15. android:orientation="vertical">
  16. <Button
  17. android:id="@+id/fitXY"
  18. android:layout_width="match_parent"
  19. android:layout_height="wrap_content"
  20. android:text="我是 fitXY,拉伸图片刚好填满视图效果"/>
  21. <Button
  22. android:id="@+id/fitStart"
  23. android:layout_width="match_parent"
  24. android:layout_height="wrap_content"
  25. android:text="我是 fitStart,拉伸图片位于视图上部"/>
  26. <Button
  27. android:id="@+id/fitCenter"
  28. android:layout_width="match_parent"
  29. android:layout_height="wrap_content"
  30. android:text="我是 fitCenter,拉伸图片位于视图中间"/>
  31. <Button
  32. android:id="@+id/fitEnd"
  33. android:layout_width="match_parent"
  34. android:layout_height="wrap_content"
  35. android:text="我是 fitEnd,拉伸图片位于视图下部"/>
  36. <Button
  37. android:id="@+id/center"
  38. android:layout_width="match_parent"
  39. android:layout_height="wrap_content"
  40. android:text="我是 center,保持图片位于视图中间"/>
  41. <Button
  42. android:id="@+id/centerCrop"
  43. android:layout_width="match_parent"
  44. android:layout_height="wrap_content"
  45. android:text="我是 centerCrop,拉伸图片充满视图并且在中间" />
  46. <Button
  47. android:id="@+id/centerInside"
  48. android:layout_width="match_parent"
  49. android:layout_height="wrap_content"
  50. android:text="我是 centerInside,只压缩不拉伸,当图片大于视图,等同于fitCenter,小于的时候等同于center"/>
  51. </LinearLayout>
  52. </LinearLayout>

然后我们为每个按钮添加点击事件的监听,代码如下:

  1. package com.easygoing.androidtextview;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.ImageView;
  6. import android.widget.TextView;
  7. public class MainActivity extends Activity implements View.OnClickListener {
  8. private ImageView iv_img;
  9. @Override
  10. protected void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.activity_imagw_view);
  13. iv_img = findViewById(R.id.iv_img);
  14. findViewById(R.id.fitCenter).setOnClickListener(this);
  15. findViewById(R.id.fitEnd).setOnClickListener(this);
  16. findViewById(R.id.fitStart).setOnClickListener(this);
  17. findViewById(R.id.fitXY).setOnClickListener(this);
  18. findViewById(R.id.center).setOnClickListener(this);
  19. findViewById(R.id.centerCrop).setOnClickListener(this);
  20. findViewById(R.id.centerInside).setOnClickListener(this);
  21. }
  22. @Override
  23. public void onClick(View v) {
  24. switch (v.getId())
  25. {
  26. case R.id.fitCenter:
  27. iv_img.setScaleType(ImageView.ScaleType.FIT_CENTER);
  28. break;
  29. case R.id.fitEnd:
  30. iv_img.setScaleType(ImageView.ScaleType.FIT_END);
  31. break;
  32. case R.id.fitStart:
  33. iv_img.setScaleType(ImageView.ScaleType.FIT_START);
  34. break;
  35. case R.id.fitXY:
  36. iv_img.setScaleType(ImageView.ScaleType.FIT_XY);
  37. break;
  38. case R.id.center:
  39. iv_img.setScaleType(ImageView.ScaleType.CENTER);
  40. break;
  41. case R.id.centerCrop:
  42. iv_img.setScaleType(ImageView.ScaleType.CENTER_CROP);
  43. break;
  44. case R.id.centerInside:
  45. iv_img.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
  46. break;
  47. }
  48. }
  49. }

并且在点击按钮后,根据类型设置不同的图片拉伸效果。我在下面列出几种类型的显示效果,其余的大家可以自己试试。

Center Center 1

Center 2 Center 3--

另外,android中ImageView可以用来显示图片,android的所有视图都可以使用自带的屏幕截图的功能,不过要想使用这个功能,我们只能通过代码来完成。

在使用过程中可能用到的方法如下:

*setDrawingCacheEnabled :设置绘图缓存状态,true:打开 false:关闭-

*isDrawingCacheEnabled:判断绘图缓存是否可用

*setDrawingCacheQuality:设置图片缓存的质量

*getDrawingCache:获取该控件绘图缓存结果,其返回值为Bitmap

*setDrawingCacheBackgroundColor:设置绘图缓存的背景色。控件的绘图缓存默认的背景色是黑色的,大家可以想想,这样截图出来,估计只能看见一片漆黑了吧。

所以我们一般将其背景色设置为白色。

那么,该怎么实现屏幕截图呢?

1.在我们开始截图前,必须设置绘图缓存的状态是可用的,注意这个方法必须一开始就调用,这是因为先开启绘图缓存,后面更新的界面才会被记录在缓存中去。

2.获取绘图缓存数据

3.完成截图,延迟调用关闭绘图缓存的方法。如果下次还要进行截图操作,那么就必须再次开启绘图缓存。

下面我们看下代码的简单实现,这里通过对TextView进行截图,然后显示在ImageView中

Center 4

布局文件很简单,两个线性布局对半均分屏幕,一个显示文本内容,一个显示截图结果。

Center 5

Center 6

主页面代码也很简单,值得注意的是不要立即关闭绘图缓存即可,有兴趣的同学可以试试怎们判断截图后的渲染完成了呢?

运行结果:

Center 7

Center 8

Center 9

发表评论

表情:
评论列表 (有 0 条评论,334人围观)

还没有评论,来说两句吧...

相关阅读

    相关 AndroidImageView的使用

    ImageView  ImageView,图像视图,直接继承自View类,它的主要功能是用于显示图片,实际上它不仅仅可以用来显示图片,任何Drawable对象都可以使用Im

    相关 Android 图像视图ImageView

    ImageView是用来显示图像的控件,它和图形显示有关的属性主要有下面两个: 1.scaleType :图形的拉伸类型,默认是fitCenter。 2.色彩:图形来源,s