6

Im trying to create a moving back ground. My goal is to have a huge bitmap scrolling by itself making it seem as if its moving. but first i need to figure out how show only a part of the bitmap. Ive tried this code but have been unsucessful. Is the subset what im looking for in this situation? canvas.drawBitmap(bitmap, """subset"""src, dst, paint)

This is the method explanation bitmap The bitmap to be drawn ====== src May be null. The subset of the bitmap to be drawn ======= dst The rectangle that the bitmap will be scaled/translated to fit into

Kohler Fryer
  • 764
  • 2
  • 8
  • 17

4 Answers4

10
Canvas.drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint);

It allows us to specify a portion of the Bitmap to draw via the second parameter. The Rect class holds the top-left and bottom-right corner coordinates of a rectangle. When we specify a portion of the Bitmap via the src, we do it in the Bitmap’s coordinate system. If we specify null, the complete Bitmap will be used. The third parameter defines where the portion of the the Bitmap should be drawn to, again in the form of a Rect instance. This time the corner coordinates are given in the coordinate system of the target of the Canvas, though (either a View or another Bitmap). The big surprise is that the two rectangles do not have to be the same size. If we specify the destination rectangle to be smaller in size than the source rectangle, then the Canvas will automatically scale for us. The same is true for specifying a larger destination rectangle.

Rect dst = new Rect();
dst.set(50, 50, 350, 350);
canvas.drawBitmap(bmp, null, dst, null);

here bmp is a bitmap with an original size of 160*183 pixels. It is scaled to 250*250 pixels using Rect.

Akhil
  • 13,888
  • 7
  • 35
  • 39
1

andother way to do this thing using crop image

public class CropImageManipulator
{

    public CropImageManipulator()
    {
    }

    private string _fileNameWithoutExtension;
    private string _fileExtension;
    private string _fileDirectory;

    public void Cropping(string inputImgPath, int cropWidth, int cropHeight)
    {
        this._fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(inputImgPath);
        this._fileExtension = System.IO.Path.GetExtension(inputImgPath);
        this._fileDirectory = System.IO.Path.GetDirectoryName(inputImgPath);

        //Load the image divided
         Image inputImg = Image.FromFile(inputImgPath);
        int imgWidth = inputImg.Width;
        int imgHeight = inputImg.Height;

        //Divide how many small blocks
        int widthCount = (int)Math.Ceiling((imgWidth * 1.00) / (cropWidth * 1.00));
        int heightCount = (int)Math.Ceiling((imgHeight * 1.00) / (cropHeight * 1.00));
        ArrayList areaList = new ArrayList();

        int i = 0;
        for (int iHeight = 0; iHeight < heightCount ; iHeight ++)
        {
            for (int iWidth = 0; iWidth < widthCount ; iWidth ++)
            {
                int pointX = iWidth * cropWidth;
                int pointY = iHeight * cropHeight;
                int areaWidth = ((pointX + cropWidth) > imgWidth) ? (imgWidth - pointX) : cropWidth;
                int areaHeight = ((pointY + cropHeight) > imgHeight) ? (imgHeight - pointY) : cropHeight;
                string s = string.Format("{0};{1};{2};{3}",pointX,pointY,areaWidth,areaHeight);

                Rectangle rect = new Rectangle(pointX,pointY,areaWidth,areaHeight);
                areaList.Add(rect);
                i ++;
            }
        }

        for (int iLoop = 0 ; iLoop < areaList.Count ; iLoop ++)
        {
            Rectangle rect = (Rectangle)areaList[iLoop];
            string fileName = this._fileDirectory + "\\" + this._fileNameWithoutExtension + "_" + iLoop.ToString() + this._fileExtension;
            Bitmap newBmp = new Bitmap(rect.Width,rect.Height,PixelFormat.Format24bppRgb);
            Graphics newBmpGraphics = Graphics.FromImage(newBmp);
            newBmpGraphics.DrawImage(inputImg,new Rectangle(0,0,rect.Width,rect.Height),rect,GraphicsUnit.Pixel);
            newBmpGraphics.Save();
            switch (this._fileExtension.ToLower())
            {
                case ".jpg":
                case ".jpeg":
                    newBmp.Save(fileName,ImageFormat.Jpeg);
                    break;
                case "gif":
                    newBmp.Save(fileName,ImageFormat.Gif);
                    break;
            }
        }
        inputImg.Dispose();
    }
}
Riddhish.Chaudhari
  • 833
  • 1
  • 8
  • 24
0

You could subclass an ImageView and use the getImageMatrix() to get the Matrix object associated with that ImageView, and then tell the matrix to move and/or zoom over the image using matrix.preTranslate() and matrix.preScale() thos creating a scrolling effect in an efficient way. And when your done you can call imageView.setImageMatrix(matrix) followed by imageView.invalidate() to make the imageview update its content.

Carl-Emil Kjellstrand
  • 1,233
  • 1
  • 10
  • 17
-1

The source sourceBitmap should be properly prepared, i.e. filtered and scaled with ,e.g, appropriate dencity and so on. Then you have to define a rectangle which grabs one's part. Say :

int desiredX0Lcl=50,
    desiredY0Lcl=70,
    desiredX1Lcl=400,
    desiredY1Lcl=500;   
Rect sourceRectLcl= new Rect();
sourceRectLcl.set(desiredX0Lcl,desiredY0Lcl,desiredX1Lcl,desiredY1Lcl);

Now create a destination rectangle with bounds matching desired part of the sourceBitmap:

Rect destinationRectLcl=new Rect(); int widthLcl=desiredX1Lcl-desiredX0Lcl; int heightLcl=desiredY1Lcl-desiredY0Lcl; destinationRectLcl.set(0,0,widthLcl,heightLcl);

create destinationCanvas:

Bitmap baseCanvasBitmapLcl = Bitmap.createBitmap(widthLcl,heightLcl ,Bitmap.Config.ARGB_8888); Canvas destCanvasLcl = new Canvas(baseCanvasBitmapLcl);

And draw in the desired part of sourceBitmap:

destCanvasLcl.drawBitmap(sourceBitmap,sourceRectLcl,destinationRectLcl,null); //sourceBitmap.recycle;

CodeToLife
  • 3,672
  • 2
  • 41
  • 29