Back to project page SpunkyCharts.
The source code is released under:
GNU General Public License
If you think the Android project SpunkyCharts listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.jogden.spunkycharts; //w w w.j a va 2 s . com /* Copyright (C) 2014 Jonathon Ogden < jeog.dev@gmail.com > This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses. */ import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.PixelFormat; import android.util.AttributeSet; import android.view.SurfaceHolder; import android.view.SurfaceView; import com.jogden.spunkycharts.R; public class ChartPanelSurfaceView extends SurfaceView implements SurfaceHolder.Callback { private boolean _active; private boolean _surfaceReady = false; private Thread _drawThread = null; private SurfaceHolder _holder = null; private DrawingThreads _parent = null; public interface Flags{ public void setRunningFlag(boolean flag); } public interface DrawingThreads{ public Thread newDrawingThreadInstance( SurfaceHolder holder, ChartPanelSurfaceView parent, boolean active ); } public ChartPanelSurfaceView(Context context, boolean _active) { super(context); this._active = _active; } public ChartPanelSurfaceView(Context context, AttributeSet attrs) { super(context,attrs); TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomSpunky); this._active = ta.getBoolean( R.styleable.CustomSpunky_active_surface, false); ta.recycle(); } public ChartPanelSurfaceView( Context context, AttributeSet attrs, int style ){ super(context,attrs, style); TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomSpunky); this._active = ta.getBoolean( R.styleable.CustomSpunky_active_surface, false); ta.recycle(); } { this.setZOrderOnTop(true); _holder = getHolder(); _holder.setFormat(PixelFormat.TRANSLUCENT); _holder.addCallback(this); } @Override public void surfaceCreated(SurfaceHolder holder) { postStart(); } @Override public void surfaceDestroyed(SurfaceHolder holder) { /* don't post stop, just kill threads */ _surfaceReady = false; _stopThreads(); } @Override public void surfaceChanged( SurfaceHolder holder, int format, int width, int height ){ postDraw(); } public void postStart(){ if(_drawThread != null) _stopThreads(); _drawBackground(); _initThreads(); _surfaceReady = true; } public void postStop(){ _surfaceReady = false; _stopThreads(); _drawBackground(); } public void postUpdate(){ if(_surfaceReady && _active && _drawThread != null) synchronized(_drawThread){ _drawThread.notify(); } } public void postDraw(){ if(_surfaceReady){ if(_drawThread != null) _stopThreads(); _initThreads(); } } public void setParent(DrawingThreads _parent) { this._parent = _parent; } private synchronized void _initThreads() { synchronized(this){ try{ if(_parent != null) _drawThread = _parent.newDrawingThreadInstance( _holder, this, _active ); _drawThread.start(); if(_active && _drawThread != null) ((Flags) _drawThread).setRunningFlag(true); }catch(Exception e){ e.printStackTrace(); } } } public synchronized void _stopThreads() { /* this blocks until the interrupted thread is joined*/ try{ if(_drawThread != null){ if(_active) ((Flags) _drawThread).setRunningFlag(false); _drawThread.interrupt(); _drawThread.join(); _drawThread = null; } } catch (InterruptedException e){ } } private void _drawBackground() { Canvas canvas = _holder.lockCanvas(); canvas.drawColor( Color.TRANSPARENT, android.graphics.PorterDuff.Mode.CLEAR ); _holder.unlockCanvasAndPost(canvas); } };