The only part of my app that is still software rendered is the rendering of one view where I need to draw a round bitmap. I'm using clipPath to clip the bitmap that I need to render to the round shape.
I understand that clipPath is not hardware accelerated, but I am looking for an algorithm that would allow me to provide the equivalent functionality using hardware acceleration.
Specifically I need to create a round clipping region of a source bitmap and have that rendered to my canvas.
If you have to use clipPath you could shut down the hardware acceleration as below.
<application
android:label="#string/application_name"
android:hardwareAccelerated="false">
And you also could control hardware acceleration among Application, Activity, Window and View layers. Detail information describes on this Hardware Acceleration article on Android Development web site.
You could try to this, though I am not sure it is hardware accelerated :
in onCreate :
create a paint (called bitmapPaint) that uses setXfermode :
http://developer.android.com/reference/android/graphics/Paint.html#setXfermode(android.graphics.Xfermode)
put an AvoidXfermode, also its deprecated, it work pretty well. Pass it the white color and target mode with a high tolerance (like 240)
in onLayout :
create a bitmap of the same size as your view
draw your circle inside on of its canvas, in white, using anti alias for a clean border
in onDraw :
draw the bitmap with the white circle in your paint canvas
now, draw your bitmap inside your paint canvas using the bitmapPaint you created in onCreate
The bitmap should be rendered inside the circle only.
If your bitmap does not change a lot, clip it once to the shape into a new bitmap, and then draw the clipped bitmap in your onDraw.
Here is an example how to clip a circle from a source bitmap
Bitmap bitmap = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
Canvas canvas = new Canvas(bitmap);
paint.setColor(Color.RED);
// Draw your shape here
canvas.drawCircle(cx, cy, radius, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(sourceBitmap, 0, 0, paint);
Related
I try to resize an image like this:
aPaint.setAntiAlias(true); // Enabling this flag will cause all draw operations that support antialiasing to use it.
aPaint.setFilterBitmap(True); // enable bilinear sampling on scaled bitmaps. If cleared, scaled bitmaps will be drawn with nearest neighbor sampling, likely resulting in artifacts.
apaint.setDither(true); // Enabling this flag applies a dither to any blit operation where the target's colour space is more constrained than the source.
aCanvas.drawBitmap(aBitmap, aJSrcRect, aJDestRectf, apaint);
But I don't have a very good antialised image (it's antialiased, but not very good). Here is an image showing my problem
What can i do to increase the quality of the antialias under android ?
For antialias effect, you can try to create a Paint like this:
Paint paint= new Paint();
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
Finally apply paint on canvas with:
canvas.drawBitmap(mBitmap, 0.f, 0.f, paint)
I am trying to create a Black screen with a transparent Hole in the middle of the screen. Here is what i have tried.
#Override
public void draw(Canvas canvas)
{
Paint myPaint = new Paint();
myPaint.setColor(0xC0000000);
canvas.drawRect(mBlackRect, myPaint);
myPaint = new Paint();
myPaint.setColor(Color.TRANSPARENT);
myPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
canvas.drawRect(mTransparentRect, myPaint);
}
The second paint, shows black color instead of transparent. How can i punch a transparent hole in MY SemiBlack Canvas?
you didn't save the canvas, try the code below
Paint myPaint = new Paint();
int sc = canvas.saveLayer(mBlackRect.left, mBlackRect.top,
mBlackRect.right, mBlackRect.bottom, myPaint,
Canvas.ALL_SAVE_FLAG);
myPaint.setColor(0xC0000000);
canvas.drawRect(mBlackRect, myPaint);
myPaint.setColor(Color.TRANSPARENT);
myPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
canvas.drawRect(mTransparentRect, myPaint);
myPaint.setXfermode(null);
canvas.restoreToCount(sc);
You can not really "punch" a hole by "removing pixels" from something already drawn, at least not with a hardware layer. And if you use a software layer, it will be bad for performance.
What you want to do is draw your shape with an alpha mask applied to your paint. A mask will prevent some parts of the shape to be drawn on the canvas, like cutting a piece of paper and stick it on a wall before spreading the painting.
To apply an alpha mask to your paint, you first need to create a bitmap containing the "hole" shape (programmatically or by loading a custom image from resources), then create a BitmapShader from this bitmap with the proper Xfermode (depending if you want the transparent part in your mask bitmap to be cut out or the non-transparent part) and finally apply this shader to your paint before drawing the semitransparent rectangle or anything you want.
Be careful with performance: only create the Paint object once (do not allocate any object in onDraw() because this method gets called up to 60 times per second on the UI thread), and recreate the alpha mask bitmap only when the bounds of your View/Drawable change (if its dimensions depend on the View dimensions of course, otherwise you just create it once).
I'm sorry if I don't have time to give you ready-to-use code but I think you should find plenty of information about the technique I just described and you can start experimenting and figuring out the solution by yourself which is more rewarding I think ;)
Imagine that I have a rectangle image. How could I create a style like the next one?
I mean, cropping the image into a circle, add the border, the shadow and the gross /shine effect. Until now, I only have tried this snippet code to crop the image: Cropping circular area from bitmap in Android but just that. I have no idea how to do the remaining components in Android.
An easy way to achieve this effect is to use Canvas.drawCircle() and a BitmapShader:
BitmapShader s = new BitmapShader(myPhoto, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
Paint p = new Paint();
p.setShader(s);
myCanvas.drawCircle(centerX, centerY, radius, p);
To do the shadow, simply call Paint.setShadowLayer() on the paint (this will only work if you draw the effect into an offscreen Bitmap or if your View uses a software layer – set by calling View.setLayerType() –).
The border can be drawn by drawing another circle on top, using the Paint.Style.STROKE style (that you can set by calling Paint.setStyle()).
Finally you can draw the gloss by drawing a circle, oval or Path on top of your very first circle. You'll need to use a LinearGradient shader on your paint and you'll also need to clip the gloss. You can do this in two ways:
If you are drawing the entire effect into a Bitmap, which is what I would recommend, simply set the paint's Xfermode to a new PorterDuffXfermode(PorterDuff.Mode.SRC_IN).
If you are drawing the effect directly on screen you can simply use Canvas.clipPath() to set a circular clip. Note that this will work with hardware acceleration only as of Android 4.3.
I have a bitmap that spans the whole screen that will function as texture for a Path object that I need to draw to my canvas. I then have a background image that this textured path needs to be drawn on top of.
I tried using the PorterDuff modes, but nothing seemed to work correctly. I was having a hard time figuring out exactly how the PorterDuff modes act, because none of them seem to act the way I always thought they were supposed to function.
I've figured out a way to texture the path with this test code:
Paint paint = new Paint();
//draw the texture
canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.texture),0,0,paint);
//construct the Path with an inverse even-odd fill
Path p = new Path();
p.setFillType(Path.FillType.INVERSE_EVEN_ODD);
p.addCircle(this.getHeight()/2, this.getWidth()/2, 200, Path.Direction.CCW);
//use CLEAR to remove inverted fill, thus showing only the originally filled section
paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.CLEAR));
//draw path
canvas.drawPath(p, paint);
But I can't figure out how to then place that on top of a background image. Am I just using the PorterDuff modes wrong?
Maybe my questions could lead you to your solution:
The paint used in your drawPath call:
What is the paint style?
What is the paint stroke width?
What is the paint stroke/fill color?
If you are not using a stoke/fill color, but a texture instead, where is the call to the paint's setShader (using a BitmapShader)?
How to get a well understanding the concept of the classes of Canvas,Drawable,Bitmap and Paint? What's the relationship among them ?
Could someone please give me an example?
Thanks a lot.
From the Canvas class documentation:
The Canvas class holds the "draw"
calls. To draw something, you need 4
basic components: A Bitmap to hold the
pixels, a Canvas to host the draw
calls (writing into the bitmap), a
drawing primitive (e.g. Rect, Path,
text, Bitmap), and a paint (to
describe the colors and styles for the
drawing).
So you need 4 components in order to draw something.
Bitmap
Canvas
Drawable
Paint
Let's say you want to draw a circle on a background image from your drawable folder.
Canvas canvas;
Bitmap mBG;
Paint mPaint = new mPaint();
mBG = BitmapFactory.decodeResource(res, R.drawable.apple); //Return a bitmap from the image from drawable folder
Canvas.drawBitmap(mBG, 0,0, null); //Draw the bitmap
mPaint.setColor(Color.BLACK); //Set paint to BLACK color
Canvas.drawCircle(100,100,30, mPaint); //Draw circle at coordinate x:100,y:100, radius 30 with the paint defined
Android's Official documentation covers all the details about the API. And best way to learn something is learn by doing, so after reading the docs, google for tutorials and experiment, all your doubts will be cleared gradually.
Canvas
Drawable
Bitmap
Paint