簡體   English   中英

android:如何旋轉位圖前景而不旋轉背景

[英]android: how to rotate bitmap foreground without rotating background

我有一個需要旋轉位圖的項目,該位圖處於“灰度/黑白”模式。 當我旋轉該位圖時,它可以工作,但是背景(位圖的白色部分)也被旋轉。 無論如何,僅旋轉前景(位圖的非白色部分)嗎? 還是如果我必須創建一個新函數來做到這一點,您能解釋一下該算法嗎?

謝謝你的幫助..

我編輯相關代碼:

int i;
        InputStream inStream = context.getResources().openRawResource(R.raw.register2);
        BufferedInputStream bis = new BufferedInputStream(inStream, 8000);

        DataInputStream dis=new DataInputStream(bis);
        byte[] music=null;
        try {
            music = new byte[inStream.available()];
            i=0;
            while(dis.available()>0)
            {
                music[i]=dis.readByte();
                i++;
            }

            dis.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Bitmap bm = Bitmap.createBitmap(260, 300, Bitmap.Config.ARGB_8888);
        ByteBuffer byteBuff=ByteBuffer.allocate(260*300);
        bm.setHasAlpha(false);
        if(false)
            byteBuff.put(music);
         int[] intbuffer = new int[260*300];
         for (int x=0; x<intbuffer.length; ++x)
            intbuffer[x] = (int) music[x];
        bm.setPixels(intbuffer, 0, 260, 0, 0, 260, 300);
        //img1.setImageBitmap(bm);
        //Canvas canvas=new Canvas(bm);
        Matrix matrix=new Matrix();
        matrix.postRotate(-90);
        Bitmap rotated=Bitmap.createBitmap(bm, 0, 0, 260, 300, matrix, true);
        //matrix.setRotate(4,bm.getWidth()/2,bm.getHeight()/2);
        //canvas.drawBitmap(bm, matrix, new Paint());
        img1.setImageBitmap(rotated);

鑒於您的前景和背景像素的顏色有所不同-您需要在兩個不同的位圖中提取/分離出前景像素和背景像素。 您創建的兩個新位圖應僅具有從原始位圖提取的像素,其余所有內容均應透明。

將前景位圖和背景位圖分開后,只需旋轉前景,然后合並回去即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM