使用两个矩形从位图剪切

时间:2015-05-08 22:35:36

标签: android canvas bitmap draw

enter image description here

我需要使用rect对象从加载的位图中剪切出一个图像并使用以下方法:

   canvas.drawBitmap(TreadmillBeltBM,CutFromBitMap,Dest,null);

CutFromBitMap是矩形对象,其中我从加载的位图切割。 Dest是rect,在屏幕上我绘制图像的位置。问题是,正如您可以想象的那样,位图不是无限图像,而CutFromBitMap矩形对象将耗尽空间,它的底部将超过加载位图的底部。当发生这种情况时,我会制作第二个矩形,并从加载的位图的顶部开始,将两个矩形连接在一起,使其看起来像无限图像,或者像图像重新开始。这就是我执行此操作时代码的样子:

                    //first draw the first rect
                int intersectionPoint = ((int)TopofFirstRect+treadmillBelt.getHeight()) - TreadmillBeltBM.getHeight();
                Rect CutFromBitMap = new Rect(0,(int)TopofFirstRect,TreadmillBeltBM.getWidth(),TreadmillBeltBM.getHeight());
                Rect Dest = new Rect((int)((1.0f/6.0f)*((float)WindowWidth)),intersectionPoint,(int)((5.0f/6.0f)*((float)WindowWidth)), treadmillBelt.getHeight());
                canvas.drawBitmap(TreadmillBeltBM,CutFromBitMap,Dest,null);

                //draw the second rect
                CutFromBitMap = new Rect(0,(int)TopofFirstRect,treadmillBelt.getWidth(),intersectionPoint);
                Dest = new Rect((int)((1.0f/6.0f)*((float)WindowWidth)),0,(int)((5.0f/6.0f)*((float)WindowWidth)), intersectionPoint);
                canvas.drawBitmap(TreadmillBeltBM,CutFromBitMap,Dest,null);

使用的画布来自surfaceview。 intersectionPoint标记两个矩形在画布中应该相交的位置(不在位图中,位图很大,并且在循环通过位图时矩形不应该触摸)。

enter image description here

所以这不起作用,事实上第二个矩形永远不会正确渲染。这是整个方法:

        private void drawBelt(Canvas canvas) 
    {
        boolean incrementFirstTop = false;
        boolean incrementSecondTop = false;
        float Speed = SMRRR.getSpeed();
        Speed = Speed * 4;

        //increment rectangle(s) position(s)
        if(FirstRectMoving)
            TopofFirstRect = TopofFirstRect + Speed;
        if(SecondRectMoving)
            TopofSecondRect = TopofSecondRect + Speed;

        //check if the rectangle has gone out of bounds of the bitmap
        if(((int)TopofFirstRect) >= TreadmillBeltBM.getHeight())
        {
            TopofFirstRect = 0;
            FirstRectMoving = false;
        }
        if(((int)TopofSecondRect) >= TreadmillBeltBM.getHeight())
        {
            TopofSecondRect = 0;
            SecondRectMoving = false;
        }

        //check if the rectangle bottom has just passed the bottom of the bitmap
        if(((int)TopofFirstRect)+treadmillBelt.getHeight() >= TreadmillBeltBM.getHeight())
        {
            incrementSecondTop = true; // move the top appropriately after drawing
            SecondRectMoving = true;
        }
        if(((int)TopofSecondRect)+treadmillBelt.getHeight() >= TreadmillBeltBM.getHeight())
        {
            incrementFirstTop = true; // move the top appropriately after drawing
            FirstRectMoving = true;
        }

        //canvas.drawBitmap(TreadmillBeltBM,CutFromBitMap,Dest,null);
        //Log.e("Bitmap height", Integer.toString(TreadmillBeltBM.getHeight()));
        //Log.e("surface height", Integer.toString(treadmillBelt.getHeight()));
        if(FirstRectMoving && SecondRectMoving)
        {//need to render two rects
            //Log.e("two", "rectangles");
            if(TopofFirstRect>TopofSecondRect)
            {
                //first draw the first rect
                int intersectionPoint = ((int)TopofFirstRect+treadmillBelt.getHeight()) - TreadmillBeltBM.getHeight();
                Rect CutFromBitMap = new Rect(0,(int)TopofFirstRect,TreadmillBeltBM.getWidth(),TreadmillBeltBM.getHeight());
                Rect Dest = new Rect((int)((1.0f/6.0f)*((float)WindowWidth)),0,(int)((5.0f/6.0f)*((float)WindowWidth)), intersectionPoint);
                canvas.drawBitmap(TreadmillBeltBM,CutFromBitMap,Dest,null);

                //draw the second rect
                CutFromBitMap = new Rect(0,(int)TopofFirstRect,treadmillBelt.getWidth(),intersectionPoint);
                Dest = new Rect((int)((1.0f/6.0f)*((float)WindowWidth)),intersectionPoint,(int)((5.0f/6.0f)*((float)WindowWidth)), treadmillBelt.getHeight());
                canvas.drawBitmap(TreadmillBeltBM,CutFromBitMap,Dest,null);
            }
            else
            {
                //first draw the second rect
                int intersectionPoint = ((int)TopofSecondRect+treadmillBelt.getHeight()) - TreadmillBeltBM.getHeight();
                Rect CutFromBitMap = new Rect(0,(int)TopofSecondRect,TreadmillBeltBM.getWidth(),TreadmillBeltBM.getHeight());
                Rect Dest = new Rect((int)((1.0f/6.0f)*((float)WindowWidth)),0,(int)((5.0f/6.0f)*((float)WindowWidth)),intersectionPoint);
                canvas.drawBitmap(TreadmillBeltBM,CutFromBitMap,Dest,null);

                //draw the first rect
                CutFromBitMap = new Rect(0,(int)TopofSecondRect,TreadmillBeltBM.getWidth(),intersectionPoint);
                Dest = new Rect((int)((1.0f/6.0f)*((float)WindowWidth)),intersectionPoint,(int)((5.0f/6.0f)*((float)WindowWidth)), treadmillBelt.getHeight());
                canvas.drawBitmap(TreadmillBeltBM,CutFromBitMap,Dest,null);
            }
        }
        else if(FirstRectMoving && !SecondRectMoving)
        {//only render the first rectangle, second rectangle isn't necessary
            //Log.e("first", "rect");
            Rect CutFromBitMap = new Rect(0,(int)TopofFirstRect,TreadmillBeltBM.getWidth(),treadmillBelt.getHeight()+((int)TopofFirstRect));
            Rect Dest = new Rect((int)((1.0f/6.0f)*((float)WindowWidth)),0,(int)((5.0f/6.0f)*((float)WindowWidth)), treadmillBelt.getHeight());
            canvas.drawBitmap(TreadmillBeltBM,CutFromBitMap,Dest,null);
        }
        else
        {//only render the second rectangle
            Rect CutFromBitMap = new Rect(0,(int)TopofSecondRect,TreadmillBeltBM.getWidth(),treadmillBelt.getHeight()+((int)TopofSecondRect));
            Rect Dest = new Rect((int)((1.0f/6.0f)*((float)WindowWidth)),0,(int)((5.0f/6.0f)*((float)WindowWidth)), treadmillBelt.getHeight());
            canvas.drawBitmap(TreadmillBeltBM,CutFromBitMap,Dest,null);
        }

        if(incrementFirstTop)
            TopofFirstRect = TopofFirstRect + Speed;

        if(incrementSecondTop)
            TopofSecondRect = TopofSecondRect + Speed;
    }

0 个答案:

没有答案