Show and hide object on canvas? - android

I am developing an Android Games. I have created a canvas on screen. I want to show an object for 2 second on its screen and then it should be disappear.
How can I achieve this?

Use this code to remove your canvas object
Paint paint = new Paint();
paint.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
canvas.drawPaint(paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC));
else
Canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR)

Keep 2 copies of bitmap. On 2nd dont draw the object and draw this 2 bitmaps alternatively, or look into this
will help you

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.

Drawing custom shape onto Canvas in Android

I've been stumped by this one for a little bit, so I figured I'd ask here to see if anyone has any pointers.
In a nutshell, I have an app where I want multiple complex shapes to be drawn onto a canvas, which will then be drawn onto the screen (I'll be implementing panning around the large canvas as suggested in this question's answer: Android: Drawing and rotating on a canvas)
How exactly do I go about creating a custom shape and drawing it at arbitrary locations/rotations on a Canvas in Android?
Below is an example of a simple custom shape, as I've tried to implement it so far (typically they'd be created at run-time)
public class Symbol {
public Bitmap b = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888);
public Symbol() {
Canvas canvas = new Canvas(b);;
Paint paint = new Paint();
paint.setColor(Color.GRAY);
paint.setStyle(Paint.Style.STROKE);
paint.setTextSize(25);
paint.setStrokeWidth(4);
paint.setDither(true);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setAntiAlias(true);
String str="TestStr";
canvas.drawText(str, 250,250, paint);
}
}
Wow, I derped hardcore on this one.
Android: canvas.drawBitmap performance issue
Adding this in my main class made everything happy, at least as far as this simple case went.:
Symbol s = new Symbol();
canvas.save();
canvas.rotate(5);
canvas.drawBitmap(s.b, 0,0, null);
canvas.restore();

Canvas keep ignoring Paint in drawBitmap method

I would like to draw bitmap(with specified color) on canvas.
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.RED);
// create bitmap
canvas.drawBitmap(bitmap, 0, 0, paint);
Well, the bitmap is visible on the canvas, but color of drawable didn't change.
Where is the problem?
I would like to draw bitmap(with specified color) on canvas.
A bitmap contains an image and drawing an image in single color doesn't make any sense. What do you expect it to do? Draw a red rectangle? Shapes can be drawn with color, not images...
The Color attribute of your Paint will be ignored. That Paint parameter is used to pass other settings such as anti-aliasing.
I hope this clarifies.
paint.setColor(Color.RED) is irrelevant. If your image comes with an alpha channel and you want it to be drawn in a single color, use ColorFilter instead:
paint.setColorFilter(new PorterDuffColorFilter(Color.RED, PorterDuff.Mode.SRC_IN);

Android: Draw circle of alphabets

I am working in android, I want to draw a circle of Alphabets, in this every alphabets A to Z must show. and this each alphabet must be clickable or touchable.
My view should look like following one:-
I do not have any idea related to this, how should I start ? please give me some suggestion so I can go ahead.what things should I use to do this.
you may provide me some suitable links.
Thank you in advance.
You will probably want to use the Android Canvas API.
Here's an example of drawing text on the Canvas: Android Canvas.drawText .
Bitmap b = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(b);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(20);
canvas.drawText("Some Text", 10, 25, paint);
Just write a loop to iterate where to draw the text and iterate through a-z, like so:
for(char ch='a'; ch<='z'; ch++)
{
System.out.print(ch);
}

How to code for the eraser as like paint in android canvas?

In my drawing application, I am doing painting with on canvas with this code:
currentPaint = new Paint();
currentPaint.setDither(true);
currentPaint.setColor(0x00000000);
currentPaint.setStyle(Paint.Style.STROKE);
currentPaint.setStrokeJoin(Paint.Join.ROUND);
currentPaint.setStrokeCap(Paint.Cap.ROUND);
//currentPaint.setStrokeWidth(3);
No i want to implement the eraser as same as paint.
Is it possible to do it on canvas ? If yes then Please help me for it.
Thanks.
currentPaint.setAlpha(0xFF);
currentPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
Call invalidate and the draw gets refreshed.
You could create graphical objects with the color of the background. Then it looks like the things behind the objects are deleted.

Categories

Resources