Android 全屏ImageView, 无拉伸, 自适应

public class FullScreenImageView extends android.support.v7.widget.AppCompatImageView {

    public static final String TAG = FullScreenImageView.class.getSimpleName();

    private Bitmap mBitmap;
    private boolean mLock;

    public FullScreenImageView(Context context) {
        this(context, null);
    }

    public FullScreenImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FullScreenImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);

        if (mBitmap != null && mLock) {
            mLock = false;
            float viewW = getWidth();
            float viewH = getHeight();
            float bw = mBitmap.getWidth();
            float bh = mBitmap.getHeight();
            float radioW = viewW / bh;
            float radioH = viewH / bw;

            Matrix matrixFollScreen = new Matrix();
            //matrixFollScreen.postRotate(90);
            matrixFollScreen.postScale(radioW, radioH);
            Bitmap bitmap = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), matrixFollScreen, true);
//            Log.e(TAG, "zwg----[onLayout]:  " + viewW + "*" + viewH);
//            Log.e(TAG, "zwg----[onLayout]:  " + bw + "*" + bh);
//            Log.e(TAG, "zwg----[onLayout]:  " + radioW + "-" + radioH);
            super.setImageBitmap(bitmap);
        }
    }

    @Override
    public void setImageBitmap(Bitmap bm) {
        mBitmap = bm;
        mLock = true;
    }
}
 <*******.FullScreenImageView
        android:id="@+id/trapezoid_bg"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Studio中,要让图片自适应ImageView,你可以使用`ImageView`的`scaleType`属性来设置图片缩放的方式,这样图片可以根据容器(ImageView)的大小进行调整。以下是几种常见的自适应图片布局方式: 1. `FitCenter`:保持图片的中心,拉伸或压缩图片以完全填充ImageView。 ```xml <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:scaleType="fitCenter" /> ``` 2. `FitStart` 或 `Start`:图片从ImageView的左上角开始,保持原始比例,填充剩余空间。 ```xml <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:scaleType="fitStart" /> ``` 3. `FitEnd` 或 `End`:图片从ImageView的右下角开始,保持原始比例,填充剩余空间。 ```xml <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:scaleType="fitEnd" /> ``` 4. `CenterCrop`:保持图片的宽高比,将图片居中裁剪,使其完全填充ImageView。 ```xml <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:scaleType="centerCrop" /> ``` 5. `CenterInside`:保持图片的宽高比,将图片居中,不改变原始尺寸,只填充ImageView内部空间。 ```xml <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:scaleType="centerInside" /> ``` 6. `Matrix`:允许对图片进行更复杂的变换,如旋转、缩放和倾斜。 每种缩放类型都有其适用场景,确保根据实际需求选择合适的`scaleType`。如果你想要在代码中动态设置,可以在Java或Kotlin中这样做: ```java ImageView imageView = findViewById(R.id.imageView); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); // 设置为自适应类型 ``` 如果你需要更灵活的自适应策略,可以考虑使用`ConstraintLayout`或者`AspectRatioFrameLayout`等高级布局。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值