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

二、用原生的 ImageView 布局
1、布局文件
- <?xml version="1.0" encoding="utf-8"?>
-
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="fill_parent">
-
- <ScrollView
- android:id="@+id/id_scrollView"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:scrollbars="vertical">
-
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical">
-
- <ImageView
- android:id="@+id/img"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="@drawable/one" />
-
- <ImageView
- android:id="@+id/img_two"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="@drawable/two" />
- <ImageView
- android:id="@+id/img_three"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="@drawable/three" />
- </LinearLayout>
-
- </ScrollView>
-
- </RelativeLayout>
2、在1080x1920分辨率的手机上运行的效果图,会变形。

三、用继承 ImageView,对ImageView进行改造。
1、布局文件
- <?xml version="1.0" encoding="utf-8"?>
-
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="fill_parent">
-
- <ScrollView
- android:id="@+id/id_scrollView"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:scrollbars="vertical">
-
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical">
-
- <com.example.animation.Image.FitImageView
- android:id="@+id/img"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="@drawable/one" />
-
- <com.example.animation.Image.FitImageView
- android:id="@+id/img_two"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="@drawable/two" />
- <com.example.animation.Image.FitImageView
- android:id="@+id/img_three"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="@drawable/three" />
- </LinearLayout>
-
- </ScrollView>
-
- </RelativeLayout>
1、app\src\main\java\com\example\animation\Image\FitImageView.java继承ImageView类
- package com.example.animation.Image;
-
- import android.annotation.SuppressLint;
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.util.AttributeSet;
- import android.util.DisplayMetrics;
- import android.util.Log;
- import android.view.View;
- import android.view.ViewGroup;
- import android.view.WindowManager;
- import android.widget.ImageView;
-
- import com.example.animation.R;
-
-
- @SuppressLint("AppCompatCustomView")
- public class FitImageView extends ImageView {
- private final String TAG="FitImageView";
-
- private int screenWidth;
- private int screenHeight;
- private int displayWidth = 0;
- private int displayHeight = 0;
- private int backgroundId;
-
- public FitImageView(Context context) {
- this(context, null);
- Log.d(TAG, "FitImageView (Context context)");
- }
-
- public FitImageView(Context context, AttributeSet attrs) {
- this(context, attrs, 0);
- Log.d(TAG, "FitImageView(Context context, AttributeSet attrs)");
- }
-
- public FitImageView(Context context, AttributeSet attrs, int defStyleAttr) {
- super(context, attrs, defStyleAttr);
- int textId = attrs.getAttributeResourceValue(null, "ShowText", 0);
- Log.d(TAG, "FitImageView textId "+ textId);
- backgroundId = attrs.getAttributeResourceValue("http://schemas.android.com/apk/res/android","background",0);
- Log.d(TAG, "FitImageView backgroundId "+ backgroundId );
- init();
- }
-
- public int getDisplayWidth(Context context) {
- DisplayMetrics dm = new DisplayMetrics();
- WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
- wm.getDefaultDisplay().getMetrics(dm);
- return dm.widthPixels;
- }
-
- public int getDisplayHeight(Context context) {
-
- DisplayMetrics dm = new DisplayMetrics();
- WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
- wm.getDefaultDisplay().getMetrics(dm);
- return dm.heightPixels;
- }
- private void init() {
- Log.d(TAG, "init");
- screenWidth = getDisplayWidth(getContext());
- screenHeight = getDisplayHeight(getContext());
-
- Log.d(TAG, "R.drawable.one id " + R.drawable.one);
- BitmapFactory.Options options= new BitmapFactory.Options();
- BitmapFactory.decodeResource(getResources(), backgroundId,options);
- Log.d(TAG, "options.outWidth&options.outHeight:" + options.outWidth + " "+options.outHeight );
-
- // Bitmap bitmap= BitmapFactory.decodeResource(getResources(), backgroundId);
- /// setImageBitmap(bitmap);
- //setImageResource(backgroundId);
- //setBackgroundResource(backgroundId);
- //setSize(bitmap.getWidth(),bitmap.getHeight());
- setSize(options.outWidth,options.outHeight);
- }
-
- public void setSize(int bitmapWidth, int bitmapHeight) {
-
- //计算出按比例拉伸后的宽度和高度
- //判断如果以图片高度拉伸到屏幕的高度,按照相应的拉伸比是否能够拉伸超过屏幕宽度或者等于屏幕宽度,否则以另一边为基准
- /*
- displayWidth = screenHeight * bitmapWidth / bitmapHeight;
- displayHeight = screenWidth * bitmapHeight / bitmapWidth;
-
- if(displayWidth>=screenWidth){
- displayHeight=screenHeight;
- }else{
- displayWidth=screenWidth;
- }
- */
- displayWidth = screenWidth;
- displayHeight = screenWidth * bitmapHeight / bitmapWidth;
- //TODO 拉伸后截取中间的部分
- /*
- ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) getLayoutParams();
- lp.leftMargin = (screenWidth - displayWidth) / 2;
- lp.topMargin = ((screenHeight - displayHeight) / 2);
- setLayoutParams(lp);
- */
- Log.d(TAG, "bitmapWidth:" + bitmapWidth);
- Log.d(TAG, "bitmapHeight:" + bitmapHeight);
- Log.d(TAG, "screenWitdth:" + screenWidth);
- Log.d(TAG, "screenHeight:" + screenHeight);
- Log.d(TAG, "fit width:" + displayWidth);
- Log.d(TAG, "fit height:" + displayHeight);
- }
-
- @Override
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
- super.onMeasure(widthMeasureSpec, heightMeasureSpec);
- Log.d(TAG, "onMeasure "+displayWidth+" "+displayHeight);
- if(displayWidth !=0 && displayHeight !=0)
- setMeasuredDimension(displayWidth,displayHeight);
- else
- ;//setMeasuredDimension(displayWidth,displayHeight);
- }
- }
3、在1080x1920分辨率的手机上运行的效果图,如下图,不会变形。

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

四、代码主要参考文章
(646条消息) Android 图像的拉伸适配解决_rong_wz的博客-CSDN博客_android 图像拉伸
五、其他的参考文章
写自定义view该有的的流程和思路 - 知乎 (zhihu.com)
【技巧】获取android xml文件中控件的属性的值 - cirno_9 - 博客园 (cnblogs.com)