Draw a bitmap broken on Rect android

I'm trying to draw a bitmap along a rectangle, but I'm not sure how to do this. Is there a way to split a bitmap along a Rect object using the paint property or something else? I looked, but I canโ€™t find anything that would make him do what I also need, most of the tile parameters will not break it into a specific instance, they break it all over the screen, so everything using this bitmap ends with in one large bitmap along all of them at the same time, without scrolling or anything else.

Any ideas? If you need more information, let me know, it's kind of a weird question, so I know that I probably didn't mention anything important.

William

+6
source share
2 answers

There are several ways you can attack this. I will talk about two of them here ...

One of the methods:

You can define a BitmapDrawable around a bitmap. Set TileMode to repeat. Give it some restrictions via setBounds (rect), where rect is your Rect instance.

The following is a brief example of using the onDraw view context as:

public class MyView extends View { Rect rect; Bitmap mBitmap; BitmapDrawable mDrawable; public MyView(Context context) { super(context); rect = new Rect(0, 0, 100, 100); mBitmap = loadBitmap(); mDrawable = new BitmapDrawable(context.getResources(), mBitmap); mDrawable.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT); mDrawable.setBounds(rect); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); mDrawable.draw(canvas); } public Bitmap loadBitmap() { // included only for example sake Paint paint = new Paint(); paint.setColor(Color.RED); Bitmap bm = Bitmap.createBitmap(10, 10, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bm); canvas.drawRect(0,0,10,10, paint); paint.setStyle(Style.STROKE); paint.setColor(Color.BLUE); canvas.drawRect(0, 0, 9, 9, paint); return bm; } } 

Note:

You can also define BitmapDrawable in xml instead of doing it in code.

Also, if you know what you are loading with getResources (), getDrawable (resourceId) is just a bitmap, you can drop it into BitmapDrawable and set TileMode there. A la:

 public class MyView extends View { Rect rect; BitmapDrawable mDrawable; public MyView(Context context) { super(context); rect = new Rect(0, 0, 400, 240); mDrawable = (BitmapDrawable) context.getResources().getDrawable(R.drawable.ic_launcher); mDrawable.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT); mDrawable.setBounds(rect); this.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.ic_launcher)); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); mDrawable.draw(canvas); } } 

This example shows a stretched background and a tiled version drawn on top of it.

Another way:

Loop in the x and y direction and re-draw the bitmap in the rectangle:

 public class MyView extends View { Rect rect; Bitmap mBitmap; public MyView(Context context) { super(context); rect = new Rect(0, 0, 100, 100); mBitmap = loadBitmap(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); final int bmWidth = mBitmap.getWidth(); final int bmHeight = mBitmap.getHeight(); for (int y = 0, height = rect.height(); y < height; y += bmHeight) { for (int x = 0, width = rect.width(); x < width; x += bmWidth) { canvas.drawBitmap(mBitmap, x, y, null); } } } public Bitmap loadBitmap() { // included only for example sake Paint paint = new Paint(); paint.setColor(Color.RED); Bitmap bm = Bitmap.createBitmap(10, 10, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bm); canvas.drawRect(0,0,10,10, paint); paint.setStyle(Style.STROKE); paint.setColor(Color.BLUE); canvas.drawRect(0, 0, 9, 9, paint); return bm; } } 

I personally would go for the first (BitmapDrawable) method.

+10
source

You can do this as BitmapDrawable does:

Define Paint using a raster shader, and then draw a rectangle with this paint:

 > Paint paint = new Paint(); > paint.setShader(new BitmapShader(bitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT)); > > canvas.drawRect(destRect, paint); 
+2
source

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


All Articles