px转dp获取屏幕尺寸的DisplayUtil

本文介绍了一个实用的工具类,用于实现px与dp之间的相互转换,获取设备屏幕的宽度和高度,确保在不同分辨率下应用界面的一致性和美观性。

简单的工具类,用于px 和 dp 互转,获取屏幕尺寸(屏幕宽高)


import android.content.Context;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;

/**
 * 屏幕适配时调用的工具内,主要负责值的转化。
 */
public class DisplayUtil {


    // 获取宽高
    public static int[] getScreenSize(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        int API_LEVEL = Build.VERSION.SDK_INT;
        // 方法1
        DisplayMetrics displayMetrics = new DisplayMetrics();
        if (API_LEVEL >= 17) {
            display.getRealMetrics(displayMetrics);
        } else {
            display.getMetrics(displayMetrics);
        }
        int width = displayMetrics.widthPixels;
        int height = displayMetrics.heightPixels;

        return new int[]{width, height};
    }

    /**
     * 将px值转换为dip或dp值,保证尺寸大小不变
     * @param pxValue
     * @return
     */
    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    /**
     * 将dip或dp值转换为px值,保证尺寸大小不变
     * @param dipValue
     */
    public static int dip2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

    /**
     * 将px值转换为sp值,保证文字大小不变
     * @param pxValue
     */
    public static int px2sp(Context context, float pxValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue / fontScale + 0.5f);
    }

    /**
     * 将sp值转换为px值,保证文字大小不变
     * @param spValue
     */
    public static int sp2px(Context context, float spValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }


}

拖拽边框让宽高同时改变,右、右下边框功能一致,左、左下边框功能一致。优化下面代码 package com.maiyou.maiysdk.util.window.draggable; import android.view.MotionEvent; import android.view.View; import com.maiyou.maiysdk.util.DeviceUtil; import com.maiyou.maiysdk.util.DisplayUtil; import com.maiyou.maiysdk.util.ResourceUtil; import com.maiyou.maiysdk.util.window.EasyWindow; public class ResizeDraggable extends BaseDraggable { private static int MIN_SIZE = 0; // 最小尺寸(px) private static int MAX_SIZE = 0; // 最大尺寸(px) private int mInitialWidth; private int mInitialHeight; private int mInitialX; private int mInitialY; private int mFixedCornerX; // 固定角点X坐标 private int mFixedCornerY; // 固定角点Y坐标 private boolean isResizing = false; private int mCornerType; // 当前操作的角落类型 // 角落类型常量 private static final int CORNER_TOP_LEFT = 1; private static final int CORNER_TOP_RIGHT = 2; private static final int CORNER_BOTTOM_LEFT = 3; private static final int CORNER_BOTTOM_RIGHT = 4; @Override public boolean isTouchMoving() { return isResizing; } @Override public void start(EasyWindow<?> easyWindow) { super.start(easyWindow); // 移除宽高比约束,允许自由调整 MIN_SIZE = DisplayUtil.dip2px(easyWindow.getContext(),150); MAX_SIZE = DisplayUtil.dip2px(easyWindow.getContext(),300); } @Override public boolean onTouch(View v, MotionEvent event) { int id = v.getId(); int drag_left = ResourceUtil.getId(v.getContext(), "drag_left"); int drag_right = ResourceUtil.getId(v.getContext(), "drag_right"); int drag_bottom_left = ResourceUtil.getId(v.getContext(), "drag_bottom_left"); int drag_bottom_right = ResourceUtil.getId(v.getContext(), "drag_bottom_right"); if (id == drag_left || id == drag_right || id == drag_bottom_left || id == drag_bottom_right) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: mInitialWidth = getViewWidth(); mInitialHeight = getViewHeight(); mInitialX = getViewOnScreenX(); mInitialY = getViewOnScreenY(); isResizing = false; // 确定操作角落类型并设置固定点 if (id == drag_left || id == drag_bottom_left) { mCornerType = CORNER_BOTTOM_LEFT; mFixedCornerX = mInitialX + mInitialWidth; // 固定右上角X mFixedCornerY = mInitialY; // 固定右上角Y } else if (id == drag_right || id == drag_bottom_right) { mCornerType = CORNER_BOTTOM_RIGHT; mFixedCornerX = mInitialX; // 固定左上角X mFixedCornerY = mInitialY; // 固定左上角Y } break; case MotionEvent.ACTION_MOVE: float currentX = event.getRawX(); float currentY = event.getRawY(); int newWidth = mInitialWidth; int newHeight = mInitialHeight; int newX = mInitialX; int newY = mInitialY; // 根据角落类型计算新尺寸和位置 switch (mCornerType) { case CORNER_BOTTOM_LEFT: // 左下角 newWidth = (int) (mFixedCornerX - currentX); newHeight = (int) (currentY - mFixedCornerY); newX = (int) currentX; break; case CORNER_BOTTOM_RIGHT: // 右下角 newWidth = (int) (currentX - mFixedCornerX); newHeight = (int) (currentY - mFixedCornerY); break; } // 应用尺寸限制并保持最小尺寸 newWidth = Math.max(MIN_SIZE, Math.min(newWidth, MAX_SIZE)); newHeight = Math.max(MIN_SIZE, Math.min(newHeight, MAX_SIZE)); // 根据限制后的尺寸调整位置(保持固定点不变) switch (mCornerType) { case CORNER_TOP_LEFT: newX = mFixedCornerX - newWidth; newY = mFixedCornerY - newHeight; break; case CORNER_TOP_RIGHT: newY = mFixedCornerY - newHeight; break; case CORNER_BOTTOM_LEFT: newX = mFixedCornerX - newWidth; break; } // 边界保护:确保窗口不会超出屏幕 int screenWidth = DisplayUtil.getScreenWidth(v.getContext()); int screenHeight = DisplayUtil.getScreenHeight(v.getContext()); newX = Math.max(0, Math.min(newX, screenWidth - newWidth)); newY = Math.max(0, Math.min(newY, screenHeight - newHeight)); // 更新窗口尺寸和位置 getEasyWindow().setWidth(newWidth); getEasyWindow().setHeight(newHeight); updateLocation(newX, newY); break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: // refreshLocationCoordinate(); break; } return true; } return false; } } <?xml version="1.0" encoding="utf-8"?><!-- 使用FrameLayout作为根容器便于拖拽区域定位 --> <!-- 内容区域 --> <LinearLayout android:id="@+id/content_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="4dp"> <RelativeLayout android:layout_width="match_parent" android:layout_height="40dp" android:paddingBottom="10dp"> <LinearLayout android:id="@+id/ll_draggable" android:layout_width="30dp" android:layout_height="30dp" android:layout_centerVertical="true" android:gravity="center"> <ImageView android:layout_width="20dp" android:layout_height="20dp" android:src="@mipmap/ml_chat_draggable" /> </LinearLayout> <TextView android:layout_width="match_parent" android:layout_height="3dp" android:layout_centerInParent="true" android:layout_marginHorizontal="100dp" android:background="@drawable/botton_yuan_ef_5" /> <LinearLayout android:id="@+id/ll_close" android:layout_width="30dp" android:layout_height="30dp" android:layout_alignParentEnd="true" android:layout_centerVertical="true" android:gravity="center"> <ImageView android:layout_width="20dp" android:layout_height="20dp" android:src="@mipmap/ml_bjlb_close" /> </LinearLayout> </RelativeLayout> <androidx.recyclerview.widget.RecyclerView android:id="@+id/rlv_messagr" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:scrollbars="none" /> <TextView android:layout_width="match_parent" android:layout_height="40dp" android:background="@drawable/bg_redius_transparent_75" android:gravity="center_vertical" android:paddingHorizontal="15dp" android:text="说点什么吧" android:textColorHint="@color/milu_color_99" /> </LinearLayout> <!-- 仅占位,形成框 --> <View android:id="@+id/drag_top" android:layout_width="match_parent" android:layout_height="4dp" android:layout_gravity="top" android:background="@color/milu_orange" android:visibility="gone" /> <!-- 拖拽区域 - 左边框 --> <View android:id="@+id/drag_left" android:layout_width="4dp" android:layout_height="wrap_content" android:layout_gravity="top|start" android:background="@color/milu_orange" android:visibility="gone" /> <!-- 拖拽区域 - 右边框 --> <View android:id="@+id/drag_right" android:layout_width="4dp" android:layout_height="wrap_content" android:layout_gravity="top|end" android:background="@color/milu_orange" android:visibility="gone" /> <!-- 拖拽区域 -下 --> <LinearLayout android:id="@+id/ll_bottom" android:layout_width="wrap_content" android:layout_height="4dp" android:visibility="gone" android:layout_gravity="bottom" android:orientation="horizontal"> <!-- 拖拽区域 - 下左边框 --> <View android:id="@+id/drag_bottom_left" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@color/milu_orange" android:visibility="visible" /> <!-- 拖拽区域 - 下右边框 --> <View android:id="@+id/drag_bottom_right" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@color/milu_orange" android:visibility="visible" /> </LinearLayout> </FrameLayout>
最新发布
07-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值