Android 使用两个矩形从位图中剪切

Android 使用两个矩形从位图中剪切,android,canvas,bitmap,draw,Android,Canvas,Bitmap,Draw,我需要使用rect对象并使用以下方法从加载的位图中剪切图像: canvas.drawBitmap(TreadmillBeltBM,CutFromBitMap,Dest,null); 其中CutFromBitMap是从加载的位图中剪切的矩形对象。Dest是rect,它是屏幕上绘制图像的位置。问题是,正如您所想象的,位图不是无限图像,CutFromBitMap矩形对象将耗尽空间,其底部将超过加载位图的底部。当这种情况发生时,我制作第二个矩形,从加载的位图的顶部开始,将两个矩形连接在一起,使其

我需要使用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。相交点标记两个矩形应在画布中相交的位置(不在位图中,位图较大,在位图中循环时,矩形不应接触)

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

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

你到底想要实现什么?这些矩形实际上是用来做什么的?这些矩形从位图中剪切出来,粘贴到surfaceview中画布的一部分。所以我需要两个矩形的原因是,当其中一个矩形开始离开位图的视图时,它需要第二个矩形从位图的顶部开始,有点像连续不断的图像流被粘贴到画布中,所以为什么不调用
Canvas#drawBitmap()
两次?甚至更好:使用
BitmapShader