简体   繁体   English

android:如何旋转位图前景而不旋转背景

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

I have a project that needs to rotate a bitmap, the bitmap is in GRAYSCALE/Black and White mode. 我有一个需要旋转位图的项目,该位图处于“灰度/黑白”模式。 When I rotate that bitmap, it works but the background (the white part of the bitmap) is rotated too. 当我旋转该位图时,它可以工作,但是背景(位图的白色部分)也被旋转。 Is there anyway to rotate just the foreground (the non white part of the bitmap) only? 无论如何,仅旋转前景(位图的非白色部分)吗? or if I must create a new function to do that, can you explain the algorithm? 还是如果我必须创建一个新函数来做到这一点,您能解释一下该算法吗?

Thanks for your help.. 谢谢你的帮助..

I edit for the relevant code : 我编辑相关代码:

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);

Given that your foreground and background pixels are diff in color- You need to extract/separate out your foreground pixels and background pixels in two different bitmaps. 鉴于您的前景和背景像素的颜色有所不同-您需要在两个不同的位图中提取/分离出前景像素和背景像素。 The two new bitmaps that you create should only have the pixels extracted from your original bitmap rest everything should be transparent. 您创建的两个新位图应仅具有从原始位图提取的像素,其余所有内容均应透明。

Once you have your foreground and background bitmap separated , just rotate your foreground and then merge back. 将前景位图和背景位图分开后,只需旋转前景,然后合并回去即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM