Code example for SurfaceHolder

Methods: getSurfaceFrame, lockCanvas, unlockCanvasAndPost

0
                Log.w("PolarClockWallpaper", "no palette?!");
                return; 
            } 
 
            final SurfaceHolder holder = getSurfaceHolder();
            final Rect frame = holder.getSurfaceFrame();
            final int width = frame.width();
            final int height = frame.height();
 
            Canvas c = null;
            try { 
                c = holder.lockCanvas();
                if (c != null) {
                    final Time calendar = mCalendar;
                    final Paint paint = mPaint;
 
                    final long millis = System.currentTimeMillis();
                    calendar.set(millis);
                    calendar.normalize(false);
 
                    int s = width / 2;
                    int t = height / 2;
 
                    c.drawColor(mPalette.getBackgroundColor());
 
                    c.translate(s + MathUtils.lerp(s, -s, mOffsetX), t);
                    c.rotate(-90.0f);
                    if (height < width) {
                        c.scale(0.9f, 0.9f);
                    } 
 
                    float size = Math.min(width, height) * 0.5f - DEFAULT_RING_THICKNESS;
                    final RectF rect = mRect;
                    rect.set(-size, -size, size, size);
                    float angle;
 
                    float lastRingThickness = DEFAULT_RING_THICKNESS;
 
                    if (mShowSeconds) {
                        // Draw seconds   
                        angle = (float) (millis % 60000) / 60000.0f;
                        //Log.d("PolarClock", "millis=" + millis + ", angle=" + angle); 
                        paint.setColor(mPalette.getSecondColor(angle));
 
                        if (mVariableLineWidth) {
                            lastRingThickness = SMALL_RING_THICKNESS;
                            paint.setStrokeWidth(lastRingThickness);
                        } 
                        c.drawArc(rect, 0.0f, angle * 360.0f, false, paint);
                    } 
 
                    // Draw minutes 
                    size -= (SMALL_GAP + lastRingThickness);
                    rect.set(-size, -size, size, size);
 
                    angle = ((calendar.minute * 60.0f + calendar.second) % 3600) / 3600.0f;
                    paint.setColor(mPalette.getMinuteColor(angle));
 
                    if (mVariableLineWidth) {
                        lastRingThickness = MEDIUM_RING_THICKNESS;
                        paint.setStrokeWidth(lastRingThickness);
                    } 
                    c.drawArc(rect, 0.0f, angle * 360.0f, false, paint);
 
                    // Draw hours 
                    size -= (SMALL_GAP + lastRingThickness);
                    rect.set(-size, -size, size, size);
 
                    angle = ((calendar.hour * 60.0f + calendar.minute) % 1440) / 1440.0f;
                    paint.setColor(mPalette.getHourColor(angle));
 
                    if (mVariableLineWidth) {
                        lastRingThickness = LARGE_RING_THICKNESS;
                        paint.setStrokeWidth(lastRingThickness);
                    } 
                    c.drawArc(rect, 0.0f, angle * 360.0f, false, paint);
 
                    // Draw day 
                    size -= (LARGE_GAP + lastRingThickness);
                    rect.set(-size, -size, size, size);
 
                    angle = (calendar.monthDay - 1) /
                            (float) (calendar.getActualMaximum(Time.MONTH_DAY) - 1);
                    paint.setColor(mPalette.getDayColor(angle));
 
                    if (mVariableLineWidth) {
                        lastRingThickness = MEDIUM_RING_THICKNESS;
                        paint.setStrokeWidth(lastRingThickness);
                    } 
                    c.drawArc(rect, 0.0f, angle * 360.0f, false, paint);
 
                    // Draw month 
                    size -= (SMALL_GAP + lastRingThickness);
                    rect.set(-size, -size, size, size);
 
                    angle = (calendar.month) / 11.0f; // NB: month is already on [0..11]
 
                    paint.setColor(mPalette.getMonthColor(angle));
 
                    if (mVariableLineWidth) {
                        lastRingThickness = LARGE_RING_THICKNESS;
                        paint.setStrokeWidth(lastRingThickness);
                    } 
                    c.drawArc(rect, 0.0f, angle * 360.0f, false, paint);
                } 
            } finally { 
                if (c != null) holder.unlockCanvasAndPost(c);
            } 
 
            mHandler.removeCallbacks(mDrawClock);
            if (mVisible) {
                if (mShowSeconds) {