【问题标题】:How to make bitmap transparent?如何使位图透明?
【发布时间】:2013-02-11 14:37:41
【问题描述】:
/**
 * @param bitmap
 *            The source bitmap.
 * @param opacity
 *            a value between 0 (completely transparent) and 255 (completely
 *            opaque).
 * @return The opacity-adjusted bitmap. If the source bitmap is mutable it
 *         will be adjusted and returned, otherwise a new bitmap is created.
 *         Source : http://stackoverflow.com/questions/7392062/android-
 *         semitransparent-bitmap-background-is-black/14858913#14858913
 */
private Bitmap adjustOpacity(Bitmap bitmap, int opacity) {
    Bitmap mutableBitmap = bitmap.isMutable() ? bitmap : bitmap.copy(Bitmap.Config.ARGB_8888, true);
    Canvas canvas = new Canvas(mutableBitmap);
    int colour = (opacity & 0xFF) << 24;
    canvas.drawColor(colour, PorterDuff.Mode.DST_IN);
    return mutableBitmap;
}

使用adjustOpacity,我将ImageViewBitmap 设为半透明。

Bitmap newBitmap = adjustOpacity(orignalBitmap, 10);
view.setImageBitmap(newBitmap);
view.setBackgroundColor(Color.WHITE);

但是,Imageview 在不是白色之前显示更暗。如何使用Bitmap 制作半透明(白色背景)Imageview?

【问题讨论】:

    标签: android bitmap imageview opacity transparent


    【解决方案1】:

    使现有位图透明太简单了。

    如果您想在进一步绘制之前使您的位图透明,请按照此操作。

    如果您的位图是 mBitmap,那么只需使用:-

    mBitmap.eraseColor(Color.TRANSPARENT) 
    

    就是这样。祝你好运!!

    【讨论】:

      【解决方案2】:

      添加方法

      Bitmap bmp; DrawView DrawView;
      
      private static Bitmap makeTransparentBitmap(Bitmap bmp, int alpha) {
              Bitmap transBmp = Bitmap.createBitmap(bmp.getWidth(),
                      bmp.getHeight(), Bitmap.Config.ARGB_8888);
              Canvas canvas = new Canvas(transBmp);
              final Paint paint = new Paint();
              paint.setAlpha(alpha);
              canvas.drawBitmap(bmp, 0, 0, paint);
              return transBmp;
          }
      
      
      @Override
      protected void onDraw(Canvas canvas) {
          canvas.drawColor(Color.rgb(43,44,45));
      
          bmp = BitmapFactory.decodeResource(getResources(), R.drawable.mouse);
          canvas.drawBitmap(bmp, pos_x, pos_y, null); 
      
      }
      

      你的事件:

      bmp = makeTransparentBitmap( bmp, 122 );
      DrawView.invalidate();
      

      DrawView

      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
      
          DrawView = new DrawView(this);
          setContentView( DrawView );
      }
      

      我找到了这个答案here

      【讨论】:

        【解决方案3】:

        @akaya 的答案是正确的方法,只是构造函数已被弃用。自 API 4 以来的新方法是使用:

        BitmapDrawable drawable = new BitmapDrawable(getResources(), bitmap);
        drawable.setAlpha(100);
        

        【讨论】:

          【解决方案4】:
          // Convert transparentColor to be transparent in a Bitmap.
          public static Bitmap makeTransparent(Bitmap bit, Color transparentColor) {
              int width =  bit.getWidth();
              int height = bit.getHeight();
              Bitmap myBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
              int [] allpixels = new int [ myBitmap.getHeight()*myBitmap.getWidth()];
              bit.getPixels(allpixels, 0, myBitmap.getWidth(), 0, 0, myBitmap.getWidth(),myBitmap.getHeight());
              myBitmap.setPixels(allpixels, 0, width, 0, 0, width, height);
          
              for(int i =0; i<myBitmap.getHeight()*myBitmap.getWidth();i++){
               if( allpixels[i] == transparentColor)
          
                           allpixels[i] = Color.alpha(Color.TRANSPARENT);
               }
          
                myBitmap.setPixels(allpixels, 0, myBitmap.getWidth(), 0, 0, myBitmap.getWidth(), myBitmap.getHeight());
                return myBitmap;
          }
          

          上面的代码将获取一个位图,并返回一个位图,其中颜色为透明颜色的每个像素都是透明的。这适用于低至 8 级的 API,我还没有在任何更低的级别进行测试。

          我通常使用 Color.RED 之类的东西来制作透明像素,因为我的资源中不使用大量 RED,但如果我这样做的话,它就是自定义的红色。

          【讨论】:

          • allpixels[i] == transparentColor mmm.. 比较 ColorInt... 我没有尝试过 tris,但我可以看到这可能会给操作员带来问题。
          【解决方案5】:

          试试这个。另请注意,setAlpha 在 0-255 范围内

          //bmp is your Bitmap object
          
          BitmapDrawable bd = new BitmapDrawable(bmp);
          bd.setAlpha(50);
          
          ImageView v = (ImageView) findViewById(R.id.image);
          v.setImageDrawable(bd);
          

          【讨论】:

          • 谢谢,我的意思是,我想改变位图本身!
          • @vingorius 你应该改变问题标题然后
          【解决方案6】:

          试试这个,

          newBitmap.setImageResource(android.R.color.transparent);
          

          【讨论】:

          • 你的意思是view.setImageResource(android.R.color.transparent);
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-10-06
          • 1970-01-01
          • 2013-08-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多