How to show only part of the bitmap -

I am trying to create a moving earth. My goal is to have a huge scroll scroll, giving the impression that it is moving. but first I need to figure out how to show only part of the bitmap. Ive tried this code but was unacceptable. Is the subset what he is looking for in this situation? canvas.drawBitmap(bitmap, """subset"""src, dst, paint)

This is an explanation of the method. Bitmap Bitmap to be drawn ====== src May be null. The subset of the bitmap to be drawn ======= dst The rectangle that the bitmap will scale / translate to fit in

+6
source share
4 answers
 Canvas.drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint); 

This allows us to specify the part of the bitmap for drawing through the second parameter. The Rect class contains the upper left and lower right corner of the rectangle coordinate. When we specify the bitmap part via src, we do this in the coordinate system of the bitmaps. If we specify null, the full bitmap will be used. The third parameter determines which part of the bitmap to draw, again in the form of an instance of Rect. This time, the angular coordinates are set in the coordinate system of the canvas target, although (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 should be smaller than the original 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 the original size of 160 * 183 pixels. It scales to 250 * 250 pixels with Rect.

+9
source

and another way to do this is using the 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(); } } 
+1
source

You can subclass ImageView and use getImageMatrix () to get the Matrix object associated with that ImageView, and then tell the matrix about the movement and / or scaling of the image using matrix.preTranslate () and matrix.preScale () effectively creating a scroll effect. And when you do this, you can call imageView.setImageMatrix (matrix) and then imageView.invalidate () to update its contents.

0
source

The source Bitmap must be properly prepared, i.e. filtered and scaled with, for example, appropriate density, etc. Then you must define a rectangle that captures one piece. 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 borders corresponding to the desired part of 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 the desired part of sourceBitmap:

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

-1
source

Source: https://habr.com/ru/post/910799/


All Articles