You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将ImageView的一半设置为透明且不改变其尺寸?

实现ImageView Bitmap半透明效果(不改变尺寸)

没问题,我来帮你搞定这个需求!要让ImageView里的Bitmap一半变成透明,同时保持原有的宽高不变,咱们可以通过直接操作Bitmap的像素数据或者用Canvas绘制的方式实现,下面是具体的步骤和代码示例:

核心思路

  • 先获取ImageView当前显示的原Bitmap,如果原Bitmap是不可编辑的(比如从资源/网络加载的),需要先创建可修改的副本
  • 要么遍历指定区域的像素将Alpha通道设为0,要么用Canvas绘制透明矩形覆盖目标区域
  • 将修改后的Bitmap重新设置给ImageView,同时回收不再使用的Bitmap避免内存泄漏

推荐实现:Canvas高效版(适合大图)

这种方式比逐像素遍历效率更高,推荐优先使用:

Kotlin 代码

fun makeHalfTransparent(imageView: ImageView) {
    // 获取原Bitmap
    val originalBitmap = (imageView.drawable as? BitmapDrawable)?.bitmap ?: return
    
    // 创建和原图尺寸一致的可编辑Bitmap
    val resultBitmap = Bitmap.createBitmap(originalBitmap.width, originalBitmap.height, Bitmap.Config.ARGB_8888)
    val canvas = Canvas(resultBitmap)
    
    // 先绘制完整的原Bitmap
    canvas.drawBitmap(originalBitmap, 0f, 0f, null)
    
    // 创建用于绘制透明区域的Paint
    val transparentPaint = Paint().apply {
        color = Color.TRANSPARENT
        // 使用SRC_IN模式,只在原图像区域内绘制透明效果
        xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)
    }
    
    // 绘制左半部分的透明矩形(要右半透明就改width/2到width的范围)
    canvas.drawRect(
        0f,
        0f,
        (originalBitmap.width / 2).toFloat(),
        originalBitmap.height.toFloat(),
        transparentPaint
    )
    
    // 将修改后的Bitmap设置回ImageView
    imageView.setImageBitmap(resultBitmap)
    
    // 回收原Bitmap释放内存
    originalBitmap.recycle()
}

Java 代码

public static void makeHalfTransparent(ImageView imageView) {
    // 获取原Bitmap
    Drawable drawable = imageView.getDrawable();
    if (!(drawable instanceof BitmapDrawable)) return;
    Bitmap originalBitmap = ((BitmapDrawable) drawable).getBitmap();
    if (originalBitmap == null) return;
    
    // 创建和原图尺寸一致的可编辑Bitmap
    Bitmap resultBitmap = Bitmap.createBitmap(originalBitmap.getWidth(), originalBitmap.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(resultBitmap);
    
    // 先绘制完整的原Bitmap
    canvas.drawBitmap(originalBitmap, 0f, 0f, null);
    
    // 创建用于绘制透明区域的Paint
    Paint transparentPaint = new Paint();
    transparentPaint.setColor(Color.TRANSPARENT);
    // 使用SRC_IN模式,只在原图像区域内绘制透明效果
    transparentPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    
    // 绘制左半部分的透明矩形(要右半透明就改width/2到width的范围)
    canvas.drawRect(
        0f,
        0f,
        (float) originalBitmap.getWidth() / 2,
        (float) originalBitmap.getHeight(),
        transparentPaint
    );
    
    // 将修改后的Bitmap设置回ImageView
    imageView.setImageBitmap(resultBitmap);
    
    // 回收原Bitmap释放内存
    originalBitmap.recycle();
}

逐像素遍历版(适合小图)

如果需要更精细的像素控制,可以用这种方式:

Kotlin 代码片段

fun makeHalfTransparentByPixel(imageView: ImageView) {
    val originalBitmap = (imageView.drawable as? BitmapDrawable)?.bitmap ?: return
    val mutableBitmap = originalBitmap.copy(Bitmap.Config.ARGB_8888, true)
    
    val width = mutableBitmap.width
    val height = mutableBitmap.height
    
    // 遍历左半部分像素,设置Alpha为0
    for (y in 0 until height) {
        for (x in 0 until width / 2) {
            val pixel = mutableBitmap.getPixel(x, y)
            // 保留RGB值,将Alpha设为0(完全透明)
            val transparentPixel = Color.argb(0, Color.red(pixel), Color.green(pixel), Color.blue(pixel))
            mutableBitmap.setPixel(x, y, transparentPixel)
        }
    }
    
    imageView.setImageBitmap(mutableBitmap)
    if (originalBitmap != mutableBitmap) originalBitmap.recycle()
}

使用说明

  • 调用方法时直接传入你的ImageView即可:makeHalfTransparent(yourImageView)
  • 如果需要右半部分透明,只需要修改矩形绘制的起始x坐标为width/2,结束x坐标为width即可

内容的提问来源于stack exchange,提问作者Mehdi bahmanpour

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:47:02