標籤:
:
實現源碼:
/** * FunctionCurveView.java * @author Lanfog * @datetime a01b-6-ab下午b:38:01 */package me.lanfog.myandroid.widget;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.util.AttributeSet;import android.view.View;/** * 函數曲線圖 */public class CurveView extends View {private Paint mPaint;private CurveUnit mCurveUnit = new CurveUnit() {@Overridepublic int getCenterY() {// TODO Auto-generated method stubreturn getHeight()/2;}@Overridepublic int getCenterX() {// TODO Auto-generated method stubreturn getWidth()/2;}@Overridepublic float function(float x) {// TODO Auto-generated method stubreturn (float) Math.sin(x);}};public CurveView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);// TODO Auto-generated constructor stubinit();}public CurveView(Context context, AttributeSet attrs) {super(context, attrs);// TODO Auto-generated constructor stubinit();}public CurveView(Context context) {super(context);// TODO Auto-generated constructor stubinit();}private void init(){mPaint = new Paint();mPaint.setAntiAlias(true);mPaint.setStyle(Paint.Style.STROKE);}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {int measuredWidth = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);int measuredHeight = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);setMeasuredDimension(measuredWidth, measuredHeight);}@Overrideprotected void onDraw(Canvas canvas) {int w = getWidth(); // 控制項寬度int h = getHeight(); // 控制項高度int p = mCurveUnit.getPadding(); // 控制項內部填充int cx = mCurveUnit.getCenterX(); // 中心橫座標int cy = mCurveUnit.getCenterY(); // 中心縱座標mPaint.setStrokeWidth(2);canvas.drawLine(p, cy, w - p, cy, mPaint);canvas.drawLine(cx, p, cx, h - p, mPaint);float pr = mCurveUnit.getPrecision(); // 刻度精度float sp = mCurveUnit.getSpacing(); // 刻度間隔float x, y;mPaint.setStrokeWidth(1);mPaint.setTextSize(12);int a = 3; // 短線刻度長度int b = 8; // 長線刻度長度// 中canvas.drawText("0", cx + 15, cy + 15, mPaint);// 左for(int i=-1;;i--){x = cx + i * sp;if(x < p) break;if(i % 5 == 0){ // 每5個刻度繪製一個數字canvas.drawLine(x, cy, x, cy - b, mPaint);canvas.drawText("" + i * pr, x, cy + 15, mPaint);}elsecanvas.drawLine(x, cy, x, cy - a, mPaint);}// 上for(int i=1;;i++){y = cy - i * sp;if(y < p) break;if(i % 5 == 0){canvas.drawLine(cx, y, cx + b, y, mPaint);canvas.drawText("" + i * pr, cx + 10, y, mPaint);}elsecanvas.drawLine(cx, y, cx + a, y, mPaint);}// 右for(int i=1;;i++){x = cx + i * sp;if(x > w - p) break;if(i % 5 == 0){canvas.drawLine(x, cy, x, cy - b, mPaint);canvas.drawText("" + i * pr, x, cy - 10, mPaint);}elsecanvas.drawLine(x, cy, x, cy - a, mPaint);}// 下for(int i=-1;;i--){y = cy - i * sp;if(y > h - p) break;if(i % 5 == 0){canvas.drawLine(cx, y, cx + b, y, mPaint);canvas.drawText("" + i * pr, cx + 10, y, mPaint);}elsecanvas.drawLine(cx, y, cx + a, y, mPaint);}mPaint.setColor(Color.RED);boolean s = false; // 是否為起始點float tx, ty; // 臨時儲存座標點float lx = 0f, ly = 0f; // 上一次儲存座標點// 負for(int i=0;;i--){if(cx + i * sp < p) break;tx = x = i * pr;ty = y = mCurveUnit.function(x);x = cx + i * sp;y = cy - y / pr * sp;if(i % 10 == 0) // 每10個刻度繪製一個(x,y)值canvas.drawText("(" + String.format("%.2f", tx) + "," + String.format("%.2f", ty) + ")", x, y, mPaint);if(s)canvas.drawLine(lx, ly, x, y, mPaint);else s = true;lx = x;ly = y;}s = false;// 正for(int i=0;;i++){if(cx + i * sp > w - p) break;tx = x = i * pr;ty = y = mCurveUnit.function(x);x = cx + i * sp;y = cy - y / pr * sp;if(i % 10 == 0)canvas.drawText("(" + String.format("%.2f", tx) + "," + String.format("%.2f", ty) + ")", x, y, mPaint);if(s)canvas.drawLine(lx, ly, x, y, mPaint);else s = true;lx = x;ly = y;}}public static abstract class CurveUnit {/** * 函數實現 */public abstract float function(float x);/** * 擷取中心的水平位置 */public abstract int getCenterX();/** * 擷取中心的垂直位置 */public abstract int getCenterY();/** * 擷取刻度精度 */public float getPrecision(){return 0.2f;}/** * 擷取刻度間隔 */public float getSpacing(){return 10f;}/** * 擷取內部填充 */public int getPadding(){return 5;}}public CurveUnit getCurveUnit() {return mCurveUnit;}public void setCurveUnit(CurveUnit mCurveUnit) {this.mCurveUnit = mCurveUnit;}}
自訂安卓函數曲線圖控制項