android開發 替換bitmap中的顏色值

來源:互聯網
上載者:User

標籤:

/**     * 將bitmap中的某種顏色值替換成新的顏色     * @param bitmap     * @param oldColor     * @param newColor     * @return     */    public static Bitmap replaceBitmapColor(Bitmap oldBitmap,int oldColor,int newColor)    {        //相關說明可參考 http://xys289187120.blog.51cto.com/3361352/657590/        Bitmap mBitmap = oldBitmap.copy(Config.ARGB_8888, true);        //迴圈獲得bitmap所有像素點        int mBitmapWidth = mBitmap.getWidth();                  int mBitmapHeight = mBitmap.getHeight();                   int mArrayColorLengh = mBitmapWidth * mBitmapHeight;                  int[] mArrayColor = new int[mArrayColorLengh];                  int count = 0;                  for (int i = 0; i < mBitmapHeight; i++) {                      for (int j = 0; j < mBitmapWidth; j++) {                              //獲得Bitmap 圖片中每一個點的color顏色值                //將需要填充的顏色值如果不是                              //在這說明一下 如果color 是全透明 或者全黑 傳回值為 0                              //getPixel()不帶透明通道 getPixel32()才帶透明部分 所以全透明是0x00000000                               //而不透明黑色是0xFF000000 如果不計算透明部分就都是0了                       int color = mBitmap.getPixel(j, i);                //將顏色值存在一個數組中 方便後面修改                             if (color == oldColor) {                                      mBitmap.setPixel(j, i, newColor);  //將白色替換成透明色                                }                                                  }          }        return mBitmap;    }

 

使用方法:

iv_image.setImageBitmap(BitmapUtil.replaceBitmapColor(gifDecoder.getFrame(i), Color.WHITE, Color.TRANSPARENT));

android開發 替換bitmap中的顏色值

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.