63 lines
2.2 KiB
Java
63 lines
2.2 KiB
Java
|
|
package com.xscm.moduleutil.widget;
|
||
|
|
|
||
|
|
import android.content.Context;
|
||
|
|
import android.graphics.drawable.Drawable;
|
||
|
|
import android.util.AttributeSet;
|
||
|
|
import android.util.Log;
|
||
|
|
import androidx.appcompat.widget.AppCompatImageView;
|
||
|
|
import com.xscm.moduleutil.R;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* com.xscm.moduleutil.widget
|
||
|
|
* qx
|
||
|
|
* 2025/10/24
|
||
|
|
* 自适应图片展示
|
||
|
|
*/
|
||
|
|
public class AdaptiveImageView extends AppCompatImageView {
|
||
|
|
public AdaptiveImageView(Context context) {
|
||
|
|
super(context);
|
||
|
|
}
|
||
|
|
|
||
|
|
public AdaptiveImageView(Context context, AttributeSet attrs) {
|
||
|
|
super(context, attrs);
|
||
|
|
}
|
||
|
|
|
||
|
|
public AdaptiveImageView(Context context, AttributeSet attrs, int defStyleAttr) {
|
||
|
|
super(context, attrs, defStyleAttr);
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
||
|
|
Log.d("DebugImageView", "=== onMeasure START ===");
|
||
|
|
Log.d("DebugImageView", "widthMeasureSpec: " + MeasureSpec.toString(widthMeasureSpec));
|
||
|
|
Log.d("DebugImageView", "heightMeasureSpec: " + MeasureSpec.toString(heightMeasureSpec));
|
||
|
|
|
||
|
|
// 先调用父类测量
|
||
|
|
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
|
||
|
|
|
||
|
|
Log.d("DebugImageView", "After super - Width: " + getMeasuredWidth() + ", Height: " + getMeasuredHeight());
|
||
|
|
|
||
|
|
Drawable drawable = getDrawable();
|
||
|
|
if (drawable != null) {
|
||
|
|
Log.d("DebugImageView", "Drawable - Width: " + drawable.getIntrinsicWidth() + ", Height: " + drawable.getIntrinsicHeight());
|
||
|
|
}
|
||
|
|
|
||
|
|
// 强制设置为最小尺寸,确保不会太小
|
||
|
|
int measuredWidth = getMeasuredWidth();
|
||
|
|
int measuredHeight = getMeasuredHeight();
|
||
|
|
|
||
|
|
int minSize = (int) getContext().getResources().getDimension(R.dimen.dp_40);
|
||
|
|
|
||
|
|
if (measuredWidth < minSize) {
|
||
|
|
measuredWidth = minSize;
|
||
|
|
}
|
||
|
|
if (measuredHeight < minSize) {
|
||
|
|
measuredHeight = minSize;
|
||
|
|
}
|
||
|
|
|
||
|
|
Log.d("DebugImageView", "Final - Width: " + measuredWidth + ", Height: " + measuredHeight);
|
||
|
|
Log.d("DebugImageView", "=== onMeasure END ===");
|
||
|
|
|
||
|
|
setMeasuredDimension(measuredWidth, measuredHeight);
|
||
|
|
}
|
||
|
|
}
|