首页 文章

如何在Android中用颜色填充位图对象

提问于
浏览
16

我正在研究Android游戏,并且出现了一些问题,我想在位图对象上填充颜色,但我不能尝试bitmap.setPixel但我的图像是PNG格式(如圆形或不清晰,用透明色包围)和android不能getHeight()或getWidth(),即

ImageView i = new ImageView(mContext);
Bitmap bMap = BitmapFactory.decodeResource(this.mContext.getResources(), mImageIds[position]); 

 //        for(int i1 = 0; i1 < bMap.getHeight();i1++)
//         for(int j = 0; j < bMap.getWidth(); j ++)
//          bMap.setPixel(i1, j, Color.RED);   //can not set 

    i.setImageBitmap(bMap);        
    i.setLayoutParams(new Gallery.LayoutParams(150, 100));
    i.setScaleType(ImageView.ScaleType.FIT_XY);        
    i.setBackgroundResource(mGalleryItemBackground);
    i.setBackgroundColor(Color.TRANSPARENT);

3 回答

  • 3

    如果您想填充纯色的位图,可以尝试使用

    Bitmap b = /*however you get a bmp*/
    b.eraseColor(color)
    

    它通过用颜色填充所有像素来清除位图 .

    可能是你想要的效果

    彼得

  • 43

    这个问题可能很旧,但有一个更简单的解决方案 . 您可以直接在ImageView上应用过滤器,而不是在Bitmap图像上应用过滤器:

    imageView.setColorFilter(tintColor, PorterDuff.Mode.MULTIPLY);
    

    您可以根据需要和提供给ImageView的位图尝试不同的滤镜模式(ADD,CLEAR,DARKEN,MULTIPLY ......) .

    如果它不起作用,请尝试删除模式:

    imageView.setColorFilter(tintColor);
    
  • 1

    通过copy()方法获取Bitmap的可变副本,并修改它的像素 .

相关问题