android紋理圖片的載入與修改,

來源:互聯網
上載者:User

android紋理圖片的載入與修改,

環境:eclipse,android,opengl es

           最近需要對紋理映像進行一些修改,首先需要載入紋理圖片,並對紋理圖片進行一些修改,最後再進行紋理對象的產生。

一、紋理圖片的載入

      紋理圖片的載入可以通過getResources().openRawResource(int)函數和bitmapFactory類進行載入。

 InputStream is = mContext.getResources().openRawResource(        R.drawable.map_lr_new_n);        Bitmap sbitmap = null;        try {    sbitmap = BitmapFactory.decodeStream(is);    // dst = revertBitmapColor(bitmap);        } finally {    try {        is.close();    } catch (IOException e) {        // Ignore.    }        }        @SuppressWarnings("deprecation")        Drawable drawable = new BitmapDrawable(sbitmap);        Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();                
二、紋理圖片的修改

1、copy bitmap對象

從檔案加入的圖片不能修改,所以需要先將映像copy到另外一個bitmap對象。

 Bitmap bitmap2=bitmap.copy(bitmap.getConfig(), true);                ModifyBitmap(bitmap2);

2、修改bitmap對象

通過bitmap中的setPixel函數進行bitmap對象的修改,其中還涉及到int類型的rgb賦值。

 public void ModifyBitmap(Bitmap bitmap)    {    int nw=bitmap.getWidth();        int nh=bitmap.getHeight();        //int alpha=        int col1=0;    int col2=0;        for(int i=0;i<nh;i++)    {    for(int j=0;j<nw-1;j++)    {                           int p=bitmap.getPixel(j+1, i);              int r=(p&  0x00FF0000)>>16;       int g=(p&  0x0000FF00)>>8;       int b= p&  0x000000FF;              int a=255-k*50;              p=(a<<24)|(r<<16)|(g<<8)|(b);                   int tempA=p;       bitmap.setPixel(j+1-k, i,tempA) ;                            }          }    }        }

三、紋理的綁定

使用opengl es中的函數對紋理進行綁定。

    if (bitmap != null) {    glBindTexture(GLES11.GL_TEXTURE_2D, drawTestTxId[0]);        glTexParameteri(GLES11.GL_TEXTURE_2D,    GLES11.GL_TEXTURE_MAG_FILTER, GLES11.GL_LINEAR);    glTexParameteri(GLES11.GL_TEXTURE_2D,    GLES11.GL_TEXTURE_MIN_FILTER, GLES11.GL_LINEAR);    glTexParameteri(GLES11.GL_TEXTURE_2D, GLES11.GL_TEXTURE_WRAP_S,    GLES11.GL_REPEAT);    glTexParameteri(GLES11.GL_TEXTURE_2D, GLES11.GL_TEXTURE_WRAP_T,    GLES11.GL_REPEAT);                GLUtils.texImage2D(GLES10.GL_TEXTURE_2D, 0, (bitmap 0);        bitmap.recycle();        }



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.