Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show only part of a bitmap--

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

like image 896
Kohler Fryer Avatar asked Mar 15 '12 13:03

Kohler Fryer


People also ask

What is bitmap in canvas?

Canvas is the place or medium where perfroms/executes the operation of drawing, and Bitmap is responsible for storing the pixel of the picture you draw.


1 Answers

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.

like image 105
Akhil Avatar answered Sep 18 '22 23:09

Akhil