Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c8ad6dcb authored by Karl Rosaen's avatar Karl Rosaen
Browse files

Optimize rotary selector widget by using bitmaps instead of drawables.

In profiling setBounds was expensive.  Time per onDraw from 13.3ms to 6.59ms :)
parent 82768db5
Loading
Loading
Loading
Loading
+15 −39
Original line number Diff line number Diff line
@@ -56,10 +56,10 @@ public class RotarySelector extends View {

    // UI elements
    private Bitmap mBackground;
    private Drawable mDimple;
    private Bitmap mDimple;

    private Drawable mLeftHandleIcon;
    private Drawable mRightHandleIcon;
    private Bitmap mLeftHandleIcon;
    private Bitmap mRightHandleIcon;

    private Bitmap mArrowShortLeftAndRight;
    private Bitmap mArrowLongLeft;  // Long arrow starting on the left, pointing clockwise
@@ -177,7 +177,7 @@ public class RotarySelector extends View {

        // Assets (all are BitmapDrawables).
        mBackground = getBitmapFor(R.drawable.jog_dial_bg);
        mDimple = r.getDrawable(R.drawable.jog_dial_dimple);
        mDimple = getBitmapFor(R.drawable.jog_dial_dimple);

        mArrowLongLeft = getBitmapFor(R.drawable.jog_dial_arrow_long_left_green);
        mArrowLongRight = getBitmapFor(R.drawable.jog_dial_arrow_long_right_red);
@@ -187,7 +187,7 @@ public class RotarySelector extends View {

        mEdgeTriggerThresh = (int) (mDensity * EDGE_TRIGGER_DIP);

        mDimpleWidth = mDimple.getIntrinsicWidth();
        mDimpleWidth = mDimple.getWidth();

        mBackgroundWidth = mBackground.getWidth();
        mBackgroundHeight = mBackground.getHeight();
@@ -239,20 +239,9 @@ public class RotarySelector extends View {
     * @param resId the resource ID.
     */
    public void setLeftHandleResource(int resId) {
        Drawable d = null;
        if (resId != 0) {
            d = getResources().getDrawable(resId);
            mLeftHandleIcon = getBitmapFor(resId);
        }
        setLeftHandleDrawable(d);
    }

    /**
     * Sets the left handle icon to a given Drawable.
     *
     * @param d the Drawable to use as the icon, or null to remove the icon.
     */
    public void setLeftHandleDrawable(Drawable d) {
        mLeftHandleIcon = d;
        invalidate();
    }

@@ -265,23 +254,13 @@ public class RotarySelector extends View {
     * @param resId the resource ID.
     */
    public void setRightHandleResource(int resId) {
        Drawable d = null;
        if (resId != 0) {
            d = getResources().getDrawable(resId);
        }
        setRightHandleDrawable(d);
            mRightHandleIcon = getBitmapFor(resId);
        }

    /**
     * Sets the right handle icon to a given Drawable.
     *
     * @param d the Drawable to use as the icon, or null to remove the icon.
     */
    public void setRightHandleDrawable(Drawable d) {
        mRightHandleIcon = d;
        invalidate();
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        final int length = isHoriz() ?
@@ -699,18 +678,15 @@ public class RotarySelector extends View {
    }

    /**
     * Sets the bounds of the specified Drawable so that it's centered
     * on the point (x,y), then draws it onto the specified canvas.
     * Draw the bitmap so that it's centered
     * on the point (x,y), then draws it using specified canvas.
     * TODO: is there already a utility method somewhere for this?
     */
    private static void drawCentered(Drawable d, Canvas c, int x, int y) {
        int w = d.getIntrinsicWidth();
        int h = d.getIntrinsicHeight();

        // if (DBG) log("--> drawCentered: " + x + " , " + y + "; intrinsic " + w + " x " + h);
        d.setBounds(x - (w / 2), y - (h / 2),
                    x + (w / 2), y + (h / 2));
        d.draw(c);
    private void drawCentered(Bitmap d, Canvas c, int x, int y) {
        int w = d.getWidth();
        int h = d.getHeight();

        c.drawBitmap(d, x - (w / 2), y - (h / 2), mPaint);
    }