Is there a similar thing in Android to WPF's Imagebrush? - android

I would like to fill each Rect with a small unique image. Is there a best way to do this? Right now they are drawn with using a Paint object.

I will answer my question for future reference then. Kotlin doesn't have the exact same solution, but you can draw bitmaps into rects similar to this way:
canvas.drawBitmap(image, Rect(0, 0, image!!.width, image!!.height), rect, null)

Related

How to remove the drawing from a bitmap of a particular region in Android

I have a bitmap object.
I have a Region object that represents a small portion of the bitmap;
I want to remove drawing from the bitmap object of that particular region and make that portion transparent..
How to do it? any help....
I am using android api-level 8..
You can simply make a pixel transparent by using mBitmap.setPixel (100,100,Color.TRANSPARENT);, so basic idea is to iterate over all the pixel to make it transparent, but if you have to iterate over too many pixels, it might be slow.
OR
You can use PorterDuffXferMode to make a portion transparent,
For an example create a paint object as mentioned below and pass it to the canvas:
Paint mPaint = new Paint();
mPaint.setXferMode(new PorterDuffXferMode(PorterDuff.Mode.CLEAR));
You can pass it to the canvas as described below:
Canvas c = new Canvas(mBitmap);
c.drawCircle(cx, cy, radius, paint);
It is for the circle but hope you will get the hint to do it for the custom region as per your need.
If still it is not working then you might have to disable Hardware Acceleration for that particular View. For more information, refer this Google DOC.
Hope this will give you some hint.

Android: What's the opposite of Bitmap.extractAlpha()

I need to do something which exactly the opposite of Bitmap.extractAlpha: Apply an alpha map (which loaded from a file) onto a RGB bitmap (which also loaded from a file).
Yes! Just like "CGImageCreateWithMask" in iOS!
The Square blog had a tutorial about this just last week: http://corner.squareup.com/2013/01/transparent-jpegs.html :).
I dont know what exactly CGImageCreateWithMask does, but if you want another picture to serve as the alpha channel for your Bitmap, you can create one as described in this question, which combines four images. I haven't tried it now, but I think for two colors it would look something like this:
Paint colorPaint = new Paint();
redPaint.setShader(new BitmapShader(redChanImg, TileMode.CLAMP, TileMode.CLAMP));
Paint alphaPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
alphaPaint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
c.setBitmap(resultImage);
c.drawRect(0, 0, width, height, colorPaint);
c.drawBitmap(alphaImg, 0, 0, alphaPaint);
// save result somewhere
You can also always just work on the raw pixel data of a bitmap using Bitmap.getPixels

Masking color android

I want to make an image transparent in android, so I did some research but came to the conclusion that I did'nt really get it. So I am drawing A couple of bitmaps on my canvas, and in 1 kind of bitmap I want to make the red transparent. i found A piece of code but I doesn't really do anything. this is my code:
if(PictureArray[a]==0){
Paint Remove = new Paint();
Remove.setARGB(255, 255, 0, 0);
int removeColor=Remove.getColor();
Remove.setAlpha(0);
Remove.setXfermode(new AvoidXfermode(removeColor,0,AvoidXfermode.Mode.TARGET));
c.drawBitmap(Stone, x, c.getHeight()/2, null);
}
Use a transparent PNG. No need to overengineer it ;)

Android Canvas.drawLine not smooth, not consistent

I am trying to use Canvas.drawLine method to draw a polygon
Here's the code that I am using
Canvas canvas = new Canvas(cache);
Paint paint = new Paint();
paint.setStrokeWidth(16);
paint.setColor(this.currentDrawing.getColor());
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
systemCanvas.drawBitmap(cache, 0, 0, paint);
paint.setStrokeCap(Cap.ROOUND);
canvas.drawLine(from.getLeft(), from.getTop(), to.getLeft(), to.getTop(), paint);
And this is the output that I am getting:
Notice the way the lines render, they break on the round shapes and don't join smoothly. I understand why is it happening but I don't know how to make it smooth and consistent.
Any help is appreciated.
You may also want to do this
mPaint.setAntiAlias(true);
Please check this answer out because it is similar to your question:
Android How to draw a smooth line following your finger
Please let me know if this helps!
You need to activate hardware acceleration. If you don't activate this, you can not use method of antialias, cap, join and etc.

why my invoke of Canvas.drawText() just doesn't work

Hi all:
I'm writing a class that inherit from TextView, and override its onDraw() method, but in the method, my invoke of canvas.drawText() doesn't seems to work, the code just like below:
protected void onDraw(Canvas canvas) {
// super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(android.graphics.Color.WHITE);
paint.setTextSize(20);
String text = "hello";
canvas.drawText(text, 0, 0, paint);
}
It isn't drawing anything because the text coordinates are bottom left. Since you're trying to draw on 0,0, it will draw above the screen.
Try changing the last line to:
canvas.drawText(text, 0, 20, paint);
Excellent suggestions all around, great job guys really. Next time though it would be nice if you ask the guy in a comment or something whether or not he's tried the completely obvious before posting it as an answer. Do you really think that the second he got to a point that wasn't working he just came straight to Stack Overflow without experimenting?
I do have an alternate suggestion, that crazily enough is based on the entire question and not just the part that could be answered without much actual knowledge.
I would recommend trying your drawText call on a Canvas that's not in a TextView subclass as that way it won't be overridden by the several hundred lines of code in TextView that manage it's drawable state.

Categories

Resources