• 2022-09-16 Android app 让图片在ScrollView里面等比例完整显示不变形,继承ImageView ,对ImageView 进行修改。


    一、我这里有三张图片,分辨率分别是214x800、1040x375、1635x747

    二、用原生的 ImageView 布局

    1、布局文件

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. android:layout_width="match_parent"
    4. android:layout_height="fill_parent">
    5. <ScrollView
    6. android:id="@+id/id_scrollView"
    7. android:layout_width="fill_parent"
    8. android:layout_height="fill_parent"
    9. android:scrollbars="vertical">
    10. <LinearLayout
    11. android:layout_width="fill_parent"
    12. android:layout_height="fill_parent"
    13. android:orientation="vertical">
    14. <ImageView
    15. android:id="@+id/img"
    16. android:layout_width="fill_parent"
    17. android:layout_height="fill_parent"
    18. android:background="@drawable/one" />
    19. <ImageView
    20. android:id="@+id/img_two"
    21. android:layout_width="fill_parent"
    22. android:layout_height="fill_parent"
    23. android:background="@drawable/two" />
    24. <ImageView
    25. android:id="@+id/img_three"
    26. android:layout_width="fill_parent"
    27. android:layout_height="fill_parent"
    28. android:background="@drawable/three" />
    29. </LinearLayout>
    30. </ScrollView>
    31. </RelativeLayout>

    2、在1080x1920分辨率的手机上运行的效果图,会变形。

     

    三、用继承 ImageView,对ImageView进行改造。

    1、布局文件

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. android:layout_width="match_parent"
    4. android:layout_height="fill_parent">
    5. <ScrollView
    6. android:id="@+id/id_scrollView"
    7. android:layout_width="fill_parent"
    8. android:layout_height="fill_parent"
    9. android:scrollbars="vertical">
    10. <LinearLayout
    11. android:layout_width="fill_parent"
    12. android:layout_height="fill_parent"
    13. android:orientation="vertical">
    14. <com.example.animation.Image.FitImageView
    15. android:id="@+id/img"
    16. android:layout_width="fill_parent"
    17. android:layout_height="fill_parent"
    18. android:background="@drawable/one" />
    19. <com.example.animation.Image.FitImageView
    20. android:id="@+id/img_two"
    21. android:layout_width="fill_parent"
    22. android:layout_height="fill_parent"
    23. android:background="@drawable/two" />
    24. <com.example.animation.Image.FitImageView
    25. android:id="@+id/img_three"
    26. android:layout_width="fill_parent"
    27. android:layout_height="fill_parent"
    28. android:background="@drawable/three" />
    29. </LinearLayout>
    30. </ScrollView>
    31. </RelativeLayout>

    1、app\src\main\java\com\example\animation\Image\FitImageView.java继承ImageView类

    1. package com.example.animation.Image;
    2. import android.annotation.SuppressLint;
    3. import android.content.Context;
    4. import android.graphics.Bitmap;
    5. import android.graphics.BitmapFactory;
    6. import android.util.AttributeSet;
    7. import android.util.DisplayMetrics;
    8. import android.util.Log;
    9. import android.view.View;
    10. import android.view.ViewGroup;
    11. import android.view.WindowManager;
    12. import android.widget.ImageView;
    13. import com.example.animation.R;
    14. @SuppressLint("AppCompatCustomView")
    15. public class FitImageView extends ImageView {
    16. private final String TAG="FitImageView";
    17. private int screenWidth;
    18. private int screenHeight;
    19. private int displayWidth = 0;
    20. private int displayHeight = 0;
    21. private int backgroundId;
    22. public FitImageView(Context context) {
    23. this(context, null);
    24. Log.d(TAG, "FitImageView (Context context)");
    25. }
    26. public FitImageView(Context context, AttributeSet attrs) {
    27. this(context, attrs, 0);
    28. Log.d(TAG, "FitImageView(Context context, AttributeSet attrs)");
    29. }
    30. public FitImageView(Context context, AttributeSet attrs, int defStyleAttr) {
    31. super(context, attrs, defStyleAttr);
    32. int textId = attrs.getAttributeResourceValue(null, "ShowText", 0);
    33. Log.d(TAG, "FitImageView textId "+ textId);
    34. backgroundId = attrs.getAttributeResourceValue("http://schemas.android.com/apk/res/android","background",0);
    35. Log.d(TAG, "FitImageView backgroundId "+ backgroundId );
    36. init();
    37. }
    38. public int getDisplayWidth(Context context) {
    39. DisplayMetrics dm = new DisplayMetrics();
    40. WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    41. wm.getDefaultDisplay().getMetrics(dm);
    42. return dm.widthPixels;
    43. }
    44. public int getDisplayHeight(Context context) {
    45. DisplayMetrics dm = new DisplayMetrics();
    46. WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    47. wm.getDefaultDisplay().getMetrics(dm);
    48. return dm.heightPixels;
    49. }
    50. private void init() {
    51. Log.d(TAG, "init");
    52. screenWidth = getDisplayWidth(getContext());
    53. screenHeight = getDisplayHeight(getContext());
    54. Log.d(TAG, "R.drawable.one id " + R.drawable.one);
    55. BitmapFactory.Options options= new BitmapFactory.Options();
    56. BitmapFactory.decodeResource(getResources(), backgroundId,options);
    57. Log.d(TAG, "options.outWidth&options.outHeight:" + options.outWidth + " "+options.outHeight );
    58. // Bitmap bitmap= BitmapFactory.decodeResource(getResources(), backgroundId);
    59. /// setImageBitmap(bitmap);
    60. //setImageResource(backgroundId);
    61. //setBackgroundResource(backgroundId);
    62. //setSize(bitmap.getWidth(),bitmap.getHeight());
    63. setSize(options.outWidth,options.outHeight);
    64. }
    65. public void setSize(int bitmapWidth, int bitmapHeight) {
    66. //计算出按比例拉伸后的宽度和高度
    67. //判断如果以图片高度拉伸到屏幕的高度,按照相应的拉伸比是否能够拉伸超过屏幕宽度或者等于屏幕宽度,否则以另一边为基准
    68. /*
    69. displayWidth = screenHeight * bitmapWidth / bitmapHeight;
    70. displayHeight = screenWidth * bitmapHeight / bitmapWidth;
    71. if(displayWidth>=screenWidth){
    72. displayHeight=screenHeight;
    73. }else{
    74. displayWidth=screenWidth;
    75. }
    76. */
    77. displayWidth = screenWidth;
    78. displayHeight = screenWidth * bitmapHeight / bitmapWidth;
    79. //TODO 拉伸后截取中间的部分
    80. /*
    81. ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) getLayoutParams();
    82. lp.leftMargin = (screenWidth - displayWidth) / 2;
    83. lp.topMargin = ((screenHeight - displayHeight) / 2);
    84. setLayoutParams(lp);
    85. */
    86. Log.d(TAG, "bitmapWidth:" + bitmapWidth);
    87. Log.d(TAG, "bitmapHeight:" + bitmapHeight);
    88. Log.d(TAG, "screenWitdth:" + screenWidth);
    89. Log.d(TAG, "screenHeight:" + screenHeight);
    90. Log.d(TAG, "fit width:" + displayWidth);
    91. Log.d(TAG, "fit height:" + displayHeight);
    92. }
    93. @Override
    94. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    95. super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    96. Log.d(TAG, "onMeasure "+displayWidth+" "+displayHeight);
    97. if(displayWidth !=0 && displayHeight !=0)
    98. setMeasuredDimension(displayWidth,displayHeight);
    99. else
    100. ;//setMeasuredDimension(displayWidth,displayHeight);
    101. }
    102. }

    3、在1080x1920分辨率的手机上运行的效果图,如下图,不会变形。

     4、看运行log,会打印分辨率等相关信息。 

    四、代码主要参考文章

    (646条消息) Android 图像的拉伸适配解决_rong_wz的博客-CSDN博客_android 图像拉伸

     五、其他的参考文章

    写自定义view该有的的流程和思路 - 知乎 (zhihu.com)

    【技巧】获取android xml文件中控件的属性的值 - cirno_9 - 博客园 (cnblogs.com)

    Android 获取屏幕宽度和高度的几种方法 - 简书 (jianshu.com)

    android 自定义控件中获取属性的三种方式(转) - 走看看 (zoukankan.com)

  • 相关阅读:
    综述、浏览器外观(Actions、右键菜单、桌面通知)
    2.26 Qt day4+5 纯净窗口移动+绘画事件+Qt实现TCP连接服务+Qt实现连接数据库
    PyTorch深度学习实战(19)——从零开始实现R-CNN目标检测
    力扣打卡之链表反转
    【Java|golang】1413. 逐步求和得到正数的最小值
    极客日报:微信回应在后台反复读取用户相册;淘特将接入微信支付;Facebook为一周两次宕机道歉
    Rust语言之多线程
    12.SqlSession 的工具类 获取sqlsession
    安装前期开发的环境
    【新版】系统架构设计师 - 案例分析 - 软件工程
  • 原文地址:https://blog.csdn.net/qq_37858386/article/details/126894415