美文网首页
安卓闪屏页图片适配

安卓闪屏页图片适配

作者: 李云龙_ | 来源:发表于2019-11-25 18:12 被阅读0次

参考:https://blog.csdn.net/sahadev_/article/details/48475217

1. 需求场景

要求图片宽度和屏幕宽度相同,高度居中裁剪,如果使用 scaleType="centerCrop" 则若图片比例不同则可能裁剪宽而不是裁剪高

2. 解决方案

自定义 AdverScaleImgView

3. 完整代码

1. 定义 AdverScaleImgView
class AdverScaleImgView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : ImageView(context, attrs, defStyleAttr) {

    fun setImgByScale(activity: Activity, resId: Int) {
        setImgByScale(activity, BitmapFactory.decodeResource(activity.resources, resId))
    }

    fun setImgByScale(activity: Activity, bitmap: Bitmap) {

        try {
            setImageBitmap(bitmap)

            // 获取屏幕的高宽
            val screenWidth = ScreenUtils.getScreenWidth(activity)

            // 开始对图片进行拉伸或者缩放
            // 使用图片的缩放比例计算将要放大的图片的高度
            val bitmapScaledHeight = Math.round(bitmap.height.toFloat() * screenWidth.toFloat() * 1.0f / bitmap.width)

            // 以屏幕的宽度为基准,如果图片的宽度比屏幕宽,则等比缩小,如果窄,则放大
            val scaledBitmap = Bitmap.createScaledBitmap(bitmap, screenWidth, bitmapScaledHeight, false)
            // 当UI绘制完毕,对图片进行处理
            val viewHeight = this@AdverScaleImgView.measuredHeight

            var finallyBitmap: Bitmap? = null
            if (scaledBitmap.height > viewHeight) {
                // 计算将要裁剪的图片的顶部以及底部的偏移量
                val offset = (scaledBitmap.height - viewHeight) / 2
                // 对图片以中心进行裁剪,裁剪出的图片就是非常适合做引导页的图片了
                finallyBitmap = Bitmap.createBitmap(scaledBitmap, 0, offset, scaledBitmap.width,
                        scaledBitmap.height - offset * 2)
            } else {
                finallyBitmap = scaledBitmap //如果 scaledBitmap 的 height 小于 viewHeight 就直接使用
            }

            if (scaledBitmap != null && finallyBitmap != scaledBitmap) {//如果返回的不是原图,则对原图进行回收
                scaledBitmap.recycle()
                System.gc()
            }

            // 设置图片显示
            this@AdverScaleImgView.setBackgroundDrawable(BitmapDrawable(activity.getResources(), finallyBitmap))

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}
2. 布局中使用
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.splash.view.activity.AdverActivity">

    <xxx.widgets.AdverScaleImgView
        android:id="@+id/iv_welcome"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"/>

    <TextView
        android:id="@+id/iv_jump"
        android:layout_width="@dimen/dp_75"
        android:layout_height="@dimen/dp_30"
        android:layout_gravity="end|top"
        android:layout_marginEnd="@dimen/dp_30"
        android:layout_marginTop="@dimen/dp_35"
        android:background="@drawable/jump_round"
        android:gravity="center"
        android:letterSpacing="-0.02"
        android:lineSpacingExtra="0sp"
        android:text="@string/jump"
        android:textColor="#ffffff"
        android:textSize="@dimen/font_middle_14sp"
        />

</FrameLayout>
3. 代码中使用
if (mSplash != null && !TextUtils.isEmpty(mSplash!!.savePath)) {
    LoadImageUtils.loadImageTarget(this, object : SimpleTarget<Bitmap>() {
        override fun onResourceReady(resource: Bitmap?, glideAnimation: GlideAnimation<in Bitmap>?) {
            resource?.let {
                iv_welcome.setImgByScale(this@AdverActivity, it)
            }
        }
    }, mSplash!!.savePath)
    countDown(3)
} else {
    iv_welcome.setImgByScale(this, R.drawable.adver)
    countDown(3)
}

相关文章

网友评论

      本文标题:安卓闪屏页图片适配

      本文链接:https://www.haomeiwen.com/subject/gkbjwctx.html