16

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.

Lee
  • 3,996
  • 3
  • 33
  • 37

3 Answers3

1

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.

Andrii Kalytiiuk
  • 1,501
  • 14
  • 26
SFeng
  • 2,226
  • 1
  • 19
  • 16
0

You could try to this, though I am not sure it is hardware accelerated :

in onCreate :

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.

Snicolas
  • 37,840
  • 15
  • 114
  • 173
0

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);
yoah
  • 7,180
  • 2
  • 30
  • 30