Android Studio入门之图像显示解析及实战(附源码 超详细必看)(包括图像视图、图像按钮、同时展示文本与图像)

爱被打了一巴掌 2023-09-30 16:27 37阅读 0赞

图像视图ImageView

显示文本用到了文本视图TextView,显示图像则用到图像视图ImageView,由于图像通常保存为单独的图片文件,因此需要把图片放到res/drawable目录,然后再去引用该图片的资源名称。并且ImageView本身默认图片居中显示,不管图片有多大抑或有多小,图像视图都会自动缩放图片,使之刚好够到ImageView的边界。通过属性android:scaleType定义,未定义即默认其值为fitCenter多种缩放类型如下

f6605ead3bf5490fba80b122c86850bd.png

效果图如下 点击图中的按钮可以将苹果置于不同的位置

9ca80de9a2e14ba398c9ab34d1e96319.png

dd3b7bc647dc4c3fbf60394c5cc3930c.png

134b91cc6f354718a969381147e57b6f.png

ImageScaleActivity类代码如下

  1. package com.example.chapter03;
  2. import android.os.Bundle;
  3. import android.view.View;
  4. import android.widget.ImageView;
  5. import androidx.appcompat.app.AppCompatActivity;
  6. // 活动类直接实现点击监听器的接口View.OnClickListener
  7. public class ImageScaleActivity extends AppCompatActivity implements View.OnClickListener {
  8. private ImageView iv_scale; // 声明一个图像视图的对象
  9. @Override
  10. protected void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.activity_image_scale);
  13. // 从布局文件中获取名叫iv_scale的图像视图
  14. iv_scale = findViewById(R.id.iv_scale);
  15. // 下面通过七个按钮,分别演示不同缩放类型的图片缩放效果
  16. findViewById(R.id.btn_center).setOnClickListener(this);
  17. findViewById(R.id.btn_fitCenter).setOnClickListener(this);
  18. findViewById(R.id.btn_centerCrop).setOnClickListener(this);
  19. findViewById(R.id.btn_centerInside).setOnClickListener(this);
  20. findViewById(R.id.btn_fitXY).setOnClickListener(this);
  21. findViewById(R.id.btn_fitStart).setOnClickListener(this);
  22. findViewById(R.id.btn_fitEnd).setOnClickListener(this);
  23. }
  24. @Override
  25. public void onClick(View v) { // 点击事件的处理方法
  26. iv_scale.setImageResource(R.drawable.apple); // 设置图像视图的图片资源
  27. if (v.getId() == R.id.btn_center) {
  28. // 将缩放类型设置为“按照原尺寸居中显示”
  29. iv_scale.setScaleType(ImageView.ScaleType.CENTER);
  30. } else if (v.getId() == R.id.btn_fitCenter) {
  31. // 将缩放类型设置为“保持宽高比例,缩放图片使其位于视图中间”
  32. iv_scale.setScaleType(ImageView.ScaleType.FIT_CENTER);
  33. } else if (v.getId() == R.id.btn_centerCrop) {
  34. // 将缩放类型设置为“缩放图片使其充满视图,并位于视图中间”
  35. iv_scale.setScaleType(ImageView.ScaleType.CENTER_CROP);
  36. } else if (v.getId() == R.id.btn_centerInside) {
  37. // 将缩放类型设置为“保持宽高比例,缩小图片使之位于视图中间(只缩小不放大)”
  38. iv_scale.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
  39. } else if (v.getId() == R.id.btn_fitXY) {
  40. // 将缩放类型设置为“缩放图片使其正好填满视图(图片可能被缩放变形)”
  41. iv_scale.setScaleType(ImageView.ScaleType.FIT_XY);
  42. } else if (v.getId() == R.id.btn_fitStart) {
  43. // 将缩放类型设置为“保持宽高比例,缩放图片使其位于视图上方或左侧”
  44. iv_scale.setScaleType(ImageView.ScaleType.FIT_START);
  45. } else if (v.getId() == R.id.btn_fitEnd) {
  46. // 将缩放类型设置为“保持宽高比例,缩放图片使其位于视图下方或右侧”
  47. iv_scale.setScaleType(ImageView.ScaleType.FIT_END);
  48. }
  49. }
  50. }

activity_image_scaleXML代码如下

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical">
  6. <ImageView
  7. android:id="@+id/iv_scale"
  8. android:layout_width="match_parent"
  9. android:layout_height="220dp"
  10. android:layout_marginTop="5dp"
  11. android:src="@drawable/apple" />
  12. <LinearLayout
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content"
  15. android:orientation="horizontal">
  16. <Button
  17. android:id="@+id/btn_fitCenter"
  18. android:layout_width="0dp"
  19. android:layout_height="wrap_content"
  20. android:layout_weight="1"
  21. android:text="fitCenter"
  22. android:textColor="#000000"
  23. android:textSize="14sp" />
  24. <Button
  25. android:id="@+id/btn_centerCrop"
  26. android:layout_width="0dp"
  27. android:layout_height="wrap_content"
  28. android:layout_weight="1"
  29. android:text="centerCrop"
  30. android:textColor="#000000"
  31. android:textSize="14sp" />
  32. <Button
  33. android:id="@+id/btn_centerInside"
  34. android:layout_width="0dp"
  35. android:layout_height="wrap_content"
  36. android:layout_weight="1"
  37. android:text="centerInside"
  38. android:textColor="#000000"
  39. android:textSize="14sp" />
  40. </LinearLayout>
  41. <LinearLayout
  42. android:layout_width="match_parent"
  43. android:layout_height="wrap_content"
  44. android:layout_marginTop="10dp"
  45. android:orientation="horizontal">
  46. <Button
  47. android:id="@+id/btn_center"
  48. android:layout_width="0dp"
  49. android:layout_height="wrap_content"
  50. android:layout_weight="1"
  51. android:text="center"
  52. android:textColor="#000000"
  53. android:textSize="14sp" />
  54. <Button
  55. android:id="@+id/btn_fitXY"
  56. android:layout_width="0dp"
  57. android:layout_height="wrap_content"
  58. android:layout_weight="1"
  59. android:text="fitXY"
  60. android:textColor="#000000"
  61. android:textSize="14sp" />
  62. <Button
  63. android:id="@+id/btn_fitStart"
  64. android:layout_width="0dp"
  65. android:layout_height="wrap_content"
  66. android:layout_weight="1"
  67. android:text="fitStart"
  68. android:textColor="#000000"
  69. android:textSize="14sp" />
  70. <Button
  71. android:id="@+id/btn_fitEnd"
  72. android:layout_width="0dp"
  73. android:layout_height="wrap_content"
  74. android:layout_weight="1"
  75. android:text="fitEnd"
  76. android:textColor="#000000"
  77. android:textSize="14sp" />
  78. </LinearLayout>
  79. </LinearLayout>

图像按钮ImageButton

常见的按钮控制是Button,其实是文本按钮,因为按钮上面只能显示文字,不能显示图片,ImageButton才是显示图片的图像按钮。ImageButton与Button的差异如下

1:Button既可显示文本也可以显示图片,而ImageButton只能显示图片不能显示文本

2:ImageButton上的图像可按比例缩放,而Button通过背景设置的图像会拉伸变形,因为背景图采取fitXY方式,无法按比例缩放

3:Button只能靠背景显示一张图片,而ImageButton可分别在前景和背景显示图片,从而实现两张图片的叠加的效果

因此,对于某些输入法打不出来的字符,以及特殊字体显示的字符串,就适合用ImageButton ,如图中的开根号就很难打出来显示出来。

b9029c7b0d9d45588aac3d1b07c67797.png

activity_image_buttonXML代码如下

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:orientation="vertical">
  5. <ImageButton
  6. android:layout_width="match_parent"
  7. android:layout_height="80dp"
  8. android:src="@drawable/sqrt"
  9. android:scaleType="fitCenter" />
  10. </LinearLayout>

ImageButtonActivity类代码如下

  1. package com.example.chapter03;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. public class ImageButtonActivity extends AppCompatActivity {
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_image_button);
  9. }
  10. }

同时展示文本与 图像

现在有了Button可在按钮上显示文字,又有ImageButton可在按钮上显示图像,通过按钮控件Button就能实现,原来Button悄悄提供了几个与图标有关的属性,通过这些属性即可指定文字旁边的图标。 有关属性如下 效果如下图

drawableTop:指定文字上方的图片

drawableBottom:指定文字下方的图片

drawableLeft 指定文字左边的图片

drawableRight 指定文字右边的图片

drawablePadding 指定图片与文字的间距

771ca30508304aea83fec82766681a15.png

activity_image_textXML文件代码如下

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:orientation="vertical">
  5. <Button
  6. android:layout_width="wrap_content"
  7. android:layout_height="wrap_content"
  8. android:layout_marginTop="10dp"
  9. android:layout_gravity="center"
  10. android:drawableLeft="@drawable/ic_about"
  11. android:drawablePadding="5dp"
  12. android:text="图标在左"
  13. android:textColor="#000000"
  14. android:textSize="17sp" />
  15. <Button
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:layout_marginTop="10dp"
  19. android:layout_gravity="center"
  20. android:drawableRight="@drawable/ic_about"
  21. android:drawablePadding="5dp"
  22. android:text="图标在右"
  23. android:textColor="#000000"
  24. android:textSize="17sp" />
  25. <Button
  26. android:layout_width="wrap_content"
  27. android:layout_height="wrap_content"
  28. android:layout_marginTop="10dp"
  29. android:layout_gravity="center"
  30. android:drawableTop="@drawable/ic_about"
  31. android:drawablePadding="5dp"
  32. android:text="图标在上"
  33. android:textColor="#000000"
  34. android:textSize="17sp" />
  35. <Button
  36. android:layout_width="wrap_content"
  37. android:layout_height="wrap_content"
  38. android:layout_marginTop="10dp"
  39. android:layout_gravity="center"
  40. android:drawableBottom="@drawable/ic_about"
  41. android:drawablePadding="5dp"
  42. android:text="图标在下"
  43. android:textColor="#000000"
  44. android:textSize="17sp" />
  45. </LinearLayout>

ImageTextActivity类代码如下

  1. package com.example.chapter03;
  2. import android.os.Bundle;
  3. import androidx.appcompat.app.AppCompatActivity;
  4. public class ImageTextActivity extends AppCompatActivity {
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_image_text);
  9. }
  10. }

发表评论

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

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

相关阅读