Android ImageView image adaptive problem record

I have been confused about the problem for a week. The pictures in the imageview are returned by the backend, including the width and height of the returned pictures. I started to use the layoutparams code to set it. I don’t know why it didn’t take effect, and then I forgot to optimize it. On the eve of the launch, the product proposed this optimization point and was confused. What I thought at the time was whether there might be a problem with the pictures returned by the backend (the product said that ios is good). . . . Speechless (I'm too skilled)
I can't bite the bullet and do it

    <ImageView
        android:id="@+id/dod_image"
        android:layout_width="@dimen/dp_73"
        android:layout_height="@dimen/dp_29"
        android:background="@drawable/drawable_dod_backgroud"
        android:paddingVertical="@dimen/dp_7"
        android:paddingHorizontal="@dimen/dp_9"
        android:scaleType="fitCenter"
        android:adjustViewBounds="true"
        app:layout_constraintTop_toTopOf="@id/iv_class_bg"
        app:layout_constraintStart_toStartOf="@id/iv_class_bg"
        />


scaleType

Most of the Internet uses this method. I also set it up, but it doesn’t work. I feel that I still have to set it from the code, so I called layoutParams again.

holder.getView<ImageView>(R.id.dod_image)?.apply {
    
    //layoutParams的单位为px 后端返回的为dp,需要借助ScreenUtils转换
                  var params=this.layoutParams
                  params.height=bean.gradeImgHeight
                  params.width= bean.gradeImgWidth
                  this.layoutParams=params
              }

This is the code written by the first reaction, I rely on it! ! ! No way, the displayed picture is just a little bit,
why is this, I can’t figure it out, so I set the width and height in xml to 0, and set the width and height in layoutparams to the width and height given by ui , Good guy, it still doesn’t work, but it’s obvious that the layoutparams didn’t take effect, but the value is wrong, so I opened the layoutparams source code again and
found the reason

The unit of layoutparamt is px! ! ! ! ! not dp

holder.getView<ImageView>(R.id.dod_image)?.apply {
    
    //layoutParams的单位为px 后端返回的为dp,需要借助ScreenUtils转换
                  var params=this.layoutParams
                  params.height=com.app.hubert.guide.util.ScreenUtils.dp2px(context,bean.gradeImgHeight+14)
                  params.width= com.app.hubert.guide.util.ScreenUtils.dp2px(context,bean.gradeImgWidth+18)
                  this.layoutParams=params
              }

Just convert it with the help of a conversion tool

package com.app.hubert.guide.util;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Point;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.KeyCharacterMap;
import android.view.KeyEvent;
import android.view.ViewConfiguration;

/**
 * Created by hubert
 * <p>
 * Created on 2017/7/27.
 */
public class ScreenUtils {
    
    

    private ScreenUtils() {
    
    
        throw new AssertionError();
    }

    /**
     * dp单位转成px
     *
     * @param context context
     * @param dp      dp值
     * @return px值
     */
    public static int dp2px(Context context, int dp) {
    
    
        return (int) (dp * context.getResources().getDisplayMetrics().density);
    }

    /**
     * 获取屏幕宽度
     */
    public static int getScreenWidth(Context context) {
    
    
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        return dm.widthPixels;
    }

    /**
     * 获取屏幕高度
     */
    public static int getScreenHeight(Context context) {
    
    
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        return dm.heightPixels;
    }

    /**
     * 获取状态栏高度
     */
    public static int getStatusBarHeight(Context context) {
    
    
        // 一般是25dp
        int height = dp2px(context, 20);
        LogUtil.i("common statusBar height:" + height);
        //获取status_bar_height资源的ID
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
    
    
            height = context.getResources().getDimensionPixelSize(resourceId);
            LogUtil.i("real statusBar height:" + height);
        }
        LogUtil.i("finally statusBar height:" + height);
        return height;
    }

    /**
     * 虚拟操作拦(home等)是否显示
     */
    public static boolean isNavigationBarShow(Activity activity) {
    
    
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
    
    
            Display display = activity.getWindowManager().getDefaultDisplay();
            Point size = new Point();
            Point realSize = new Point();
            display.getSize(size);
            display.getRealSize(realSize);
            return realSize.y != size.y;
        } else {
    
    
            boolean menu = ViewConfiguration.get(activity).hasPermanentMenuKey();
            boolean back = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
            if (menu || back) {
    
    
                return false;
            } else {
    
    
                return true;
            }
        }
    }

    /**
     * 获取虚拟操作拦(home等)高度
     */
    public static int getNavigationBarHeight(Activity activity) {
    
    
        if (!isNavigationBarShow(activity))
            return 0;
        int height = 0;
        Resources resources = activity.getResources();
        //获取NavigationBar的高度
        int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
        if (resourceId > 0)
            height = resources.getDimensionPixelSize(resourceId);
        LogUtil.i("NavigationBar的高度:" + height);
        return height;
    }
}


This is a tool class, it’s okay, goodbye after complaining

at last

If you want to become an architect or want to break through the 20-30K salary range, then don't be limited to coding and business, but you must be able to select models, expand, and improve programming thinking. In addition, a good career plan is also very important, and the habit of learning is very important, but the most important thing is to be able to persevere. Any plan that cannot be implemented consistently is empty talk.

If you have no direction, here I would like to share with you a set of "Advanced Notes on the Eight Major Modules of Android" written by the senior architect of Ali, to help you organize the messy, scattered and fragmented knowledge systematically, so that you can systematically and efficiently Master the various knowledge points of Android development.
insert image description here
Compared with the fragmented content we usually read, the knowledge points of this note are more systematic, easier to understand and remember, and are arranged strictly according to the knowledge system.

Full set of video materials:

1. Interview collection

insert image description here
2. Source code analysis collection
insert image description here

3. The collection of open source frameworks
insert image description here
welcomes everyone to support with one click and three links. If you need the information in the article, directly scan the CSDN official certification WeChat card at the end of the article to get it for free↓↓↓

Guess you like

Origin blog.csdn.net/datian1234/article/details/130175397