Android Java设置一个图片的颜色为透明色(不显示,不是背景色),可用于无透明通道的ImageView

http://stackoverflow.com/questions/8264181/replace-specific-color-in-bitmap-with-transparency


用透明替换特定的颜色从现有的位图透明位图

http://www.8jiaban.com/view/a00e4f0f


	public Bitmap createTransparentBitmapFromBitmap(Bitmap bitmap,
	          int replaceThisColor) {
	        if (bitmap != null) {
	          int picw = bitmap.getWidth(); 
	          int pich = bitmap.getHeight();
	          int[] pix = new int[picw * pich];
	          bitmap.getPixels(pix, 0, picw, 0, 0, picw, pich);

	          int sr = (replaceThisColor >> 16) & 0xff;
	          int sg = (replaceThisColor >> 8) & 0xff;
	          int sb = replaceThisColor & 0xff;

	          for (int y = 0; y < pich; y++) {   
	            for (int x = 0; x < picw; x++) {
	              int index = y * picw + x;
	            /*  int r = (pix[index] >> 16) & 0xff;
	              int g = (pix[index] >> 8) & 0xff;
	              int b = pix[index] & 0xff;*/

	              if (pix[index] == replaceThisColor) {

//	                if(x<topLeftHole.x) topLeftHole.x = x;  
//	                if(y<topLeftHole.y) topLeftHole.y = y;
//	                if(x>bottomRightHole.x) bottomRightHole.x = x;
//	                if(y>bottomRightHole.y)bottomRightHole.y = y;

	                pix[index] = Color.TRANSPARENT;   
	              } else {
	                //break;
	              }
	            }
	          }

	          Bitmap bm = Bitmap.createBitmap(pix, picw, pich,
	              Bitmap.Config.ARGB_8888);  

	          return bm;
	        }
	        return null;
	      }

调用代码:

				Bitmap bmp2= createTransparentBitmapFromBitmap(bmp,Color.MAGENTA);
				imgView.setBackgroundColor(Color.TRANSPARENT); 
				imgView.setImageBitmap(bmp2);  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值