刻度尺

来源:互联网 发布:企业邮箱的域名 编辑:程序博客网 时间:2024/04/28 06:48

如图所示图的上面就是一个可以旋转的刻度尺

代码如下:

import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.drawable.Drawable;import android.media.SoundPool;import android.view.MotionEvent;import android.view.SurfaceHolder;import android.view.SurfaceView;import com.ijoysoft.flashlight.R;import com.ijoysoft.flashlight.activity.MainActivity;import com.ijoysoft.flashlight.util.Arithmetic;import com.ijoysoft.flashlight.util.ScreenUtil;public class StaffView extends SurfaceView implements SurfaceHolder.Callback,Runnable {private SurfaceHolder mSurfaceHolder = null;private Canvas canvas;private Paint paint;// 刻度游标private Drawable pointer;// 图片private Drawable imgBitmap;// 背景private Drawable background;// 初始值private float initValue;// 刻度单位最小值private float interval;// 单位private String unit;// 是否初始private boolean isInit = true;// 显示结果X坐标private int result_x = 36; // 结果的X轴位置// 显示结果Y坐标private int result_y = 25; // 结果的Y轴位置/** * 显示结果字体大小 */private int result_size = 24; // 结果的字体大小private int size;private float result = 0;// 结果画笔private Paint reslutPaint;// 字体画笔private Paint textPaint;// 黑线画笔private Paint bPaint;// 白线画笔private Paint wPaint;// 显示的文字private String value[] = { "8", "9", "sos", "0", "1", "2", "3", "4", "5","6", "7" };// 线的间隔private int mSpace;// 图片的间隔private int imgSpace;// view宽private int mWidth;// view高private int mHeight;// 选择的值private int rate;// 滑动时变化的值private int mNumber = 0;private int index = 1;private boolean isLeft = true;private int isLeftChange = 1;private Context context;private SoundPool pool;private int hitOkSfx;public StaffView(Context context, float initValue, float interval,SoundPool pool, int i) {super(context);this.context = context;this.pool = pool;hitOkSfx = i;mSurfaceHolder = this.getHolder();mSurfaceHolder.addCallback(this);paint = new Paint();this.setFocusable(true);pointer = getResources().getDrawable(R.drawable.kedu_pointer);imgBitmap = getResources().getDrawable(R.drawable.turn_img);background = getResources().getDrawable(R.drawable.staff_bg);this.initValue = initValue;this.interval = interval;this.unit = unit;init(context);}@Overridepublic void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {}@Overridepublic void surfaceCreated(SurfaceHolder holder) {new Thread(this).start();}@Overridepublic void surfaceDestroyed(SurfaceHolder holder) {}@Overridepublic void run() {draw(0);}private void init(Context context) {size = ScreenUtil.dip2px(context, 1);// 显示结果reslutPaint = new Paint();reslutPaint.setColor(Color.BLACK);reslutPaint.setTextSize(result_size);// 字体textPaint = new Paint();textPaint.setStrokeWidth(ScreenUtil.dip2px(context, 3));// 黑线bPaint = new Paint();bPaint.setColor(Color.BLACK);bPaint.setStrokeWidth(size);// 白线wPaint = new Paint();wPaint.setColor(Color.WHITE);wPaint.setStrokeWidth(size);}/** * @param direction *            方向,-1向左,1向右,0不动 */public void draw(int direction) {// 获取画布canvas = mSurfaceHolder.lockCanvas();if (mSurfaceHolder == null || canvas == null) {return;}canvas.drawColor(Color.WHITE);paint.setAntiAlias(true);paint.setColor(Color.GRAY);background.setBounds(0, 0, getWidth(), getHeight());background.draw(canvas);if (isInit) {result = initValue;} else {switch (direction) {case 1:result = Arithmetic.add(result, interval);mNumber = (int) ((result - initValue) / interval);initValue = result;isLeft = true;break;case -1:result = Arithmetic.sub(result, interval);mNumber = (int) ((result - initValue) / interval);initValue = result;mNumber = Math.abs(mNumber);isLeft = false;break;}if (MainActivity.isSound) {if (index % 2 == 0) {pool.play(hitOkSfx, 1, 1, 0, 0, 0);}}}initStaff();pointer.setBounds(mWidth / 2 - ScreenUtil.dip2px(context, 17), mHeight* 5 / 6 - ScreenUtil.dip2px(context, 20), mWidth / 2+ ScreenUtil.dip2px(context, 17),mHeight * 5 / 6 + ScreenUtil.dip2px(context, 12));pointer.draw(canvas);// canvas.drawText(Float.toString(result) + " " + unit, result_x,// result_y, reslutPaint);// 画上面的图if (isInit) {for (int i = 0; i < 8; i++) {int top = mHeight / 9;int left = imgSpace + i * imgSpace * 2;int right = imgSpace * 2 + i * imgSpace * 2;imgBitmap.setBounds(left, top, right, mHeight * 5 / 9);imgBitmap.draw(canvas);}} else {if (index % 2 == 0) {for (int i = 0; i < 8; i++) {int top = mHeight / 9;int left = imgSpace + i * imgSpace * 2;int right = imgSpace * 2 + i * imgSpace * 2;imgBitmap.setBounds(left, top, right, mHeight * 5 / 9);imgBitmap.draw(canvas);}} else {for (int i = 0; i < 9; i++) {int top = mHeight / 9;int left = 0 + i * imgSpace * 2;int right = imgSpace + i * imgSpace * 2;imgBitmap.setBounds(left, top, right, mHeight * 5 / 9);imgBitmap.draw(canvas);}}}// 解锁画布,提交画好的图像mSurfaceHolder.unlockCanvasAndPost(canvas);}@Overrideprotected void onSizeChanged(int w, int h, int oldw, int oldh) {super.onSizeChanged(w, h, oldw, oldh);mWidth = getWidth();mHeight = getHeight();mSpace = getWidth() / 14;imgSpace = getWidth() / 17;}private void initStaff() {canvas.drawLine(0, mHeight * 2 / 3, mWidth, mHeight * 2 / 3, wPaint);canvas.drawLine(0, mHeight * 2 / 3 - 2, mWidth, mHeight * 2 / 3 - 2,bPaint);canvas.drawLine(0, mHeight, mWidth, mHeight, wPaint);canvas.drawLine(0, mHeight - 2, mWidth, mHeight - 2, bPaint);if (mNumber > 0) {//实现从新排列数组if (!isLeft) {String temp;if (isLeftChange % 2 == 0) {for (int k = value.length - 1; k > 0; k--) {temp = value[k];value[k] = value[k - 1];value[k - 1] = temp;}}isLeftChange++;} else {String mp;if (isLeftChange % 2 == 1) {for (int k = 0; k < value.length - 1; k++) {mp = value[k];value[k] = value[k + 1];value[k + 1] = mp;}}isLeftChange++;}}int j = 0;for (int i = 1; i < 14; i++) {if (isInit) {if (i % 2 == 1) {canvas.drawLine(mSpace * i, mHeight - 2, mSpace * i,mHeight - mHeight / 12, bPaint);if (value[j].equals("sos")) {textPaint.setColor(Color.YELLOW);} else if (value[j].equals("0")) {textPaint.setColor(Color.RED);} else {textPaint.setColor(Color.WHITE);}if (i == 7) {textPaint.setTextSize(ScreenUtil.dip2px(context, 18));if (value[j].equals("sos")) {rate = 0;} else if (value[j].equals("0")) {rate = -1;} else {rate = Integer.parseInt(value[j]);}} else {textPaint.setTextSize(ScreenUtil.dip2px(context, 12));}if (value[j].equals("sos")) {if (i == 7) {canvas.drawText(value[j],mSpace * i - ScreenUtil.dip2px(context, 13),mHeight - mHeight / 6, textPaint);} else {canvas.drawText(value[j],mSpace * i - ScreenUtil.dip2px(context, 10),mHeight - mHeight / 6, textPaint);}} else {canvas.drawText(value[j],mSpace * i - ScreenUtil.dip2px(context, 3),mHeight - mHeight / 6, textPaint);}j++;} else {canvas.drawLine(mSpace * i, mHeight - 2, mSpace * i,mHeight - mHeight / 24, bPaint);}} else {if (index % 2 == 0) {if (i % 2 == 1) {canvas.drawLine(mSpace * i, mHeight - 2, mSpace * i,mHeight - mHeight / 12, bPaint);if (value[j].equals("sos")) {textPaint.setColor(Color.YELLOW);} else if (value[j].equals("0")) {textPaint.setColor(Color.RED);} else {textPaint.setColor(Color.WHITE);}if (i == 7) {textPaint.setTextSize(ScreenUtil.dip2px(context, 18));if (value[j].equals("sos")) {rate = 0;} else if (value[j].equals("0")) {rate = -1;} else {rate = Integer.parseInt(value[j]);}} else {textPaint.setTextSize(ScreenUtil.dip2px(context, 12));}if (value[j].equals("sos")) {if (i == 7) {canvas.drawText(value[j], mSpace * i- ScreenUtil.dip2px(context, 13),mHeight - mHeight / 6, textPaint);} else {canvas.drawText(value[j], mSpace * i- ScreenUtil.dip2px(context, 10),mHeight - mHeight / 6, textPaint);}} else {canvas.drawText(value[j],mSpace * i - ScreenUtil.dip2px(context, 3),mHeight - mHeight / 6, textPaint);}j++;} else {canvas.drawLine(mSpace * i, mHeight - 2, mSpace * i,mHeight - mHeight / 24, bPaint);}} else {if (i % 2 == 1) {canvas.drawLine(mSpace * i, mHeight - 2, mSpace * i,mHeight - mHeight / 24, bPaint);} else {canvas.drawLine(mSpace * i, mHeight - 2, mSpace * i,mHeight - mHeight / 12, bPaint);if (value[j].equals("sos")) {textPaint.setColor(Color.YELLOW);} else if (value[j].equals("0")) {textPaint.setColor(Color.RED);} else {textPaint.setColor(Color.WHITE);}if (i == 7) {textPaint.setTextSize(ScreenUtil.dip2px(context, 18));if (value[j].equals("sos")) {rate = 0;} else if (value[j].equals("0")) {rate = -1;} else {rate = Integer.parseInt(value[j]);}} else {textPaint.setTextSize(ScreenUtil.dip2px(context, 12));}if (value[j].equals("sos")) {if (i == 7) {canvas.drawText(value[j], mSpace * i- ScreenUtil.dip2px(context, 13),mHeight - mHeight / 6, textPaint);} else {canvas.drawText(value[j], mSpace * i- ScreenUtil.dip2px(context, 10),mHeight - mHeight / 6, textPaint);}} else {canvas.drawText(value[j],mSpace * i - ScreenUtil.dip2px(context, 3),mHeight - mHeight / 6, textPaint);}j++;}}if (i == 13) {index++;}}}}private float initx = 0;@Overridepublic boolean onTouchEvent(MotionEvent event) {switch (event.getAction()) {case MotionEvent.ACTION_DOWN:initx = event.getX();break;case MotionEvent.ACTION_MOVE:float lastx = event.getX();if (lastx > initx + mSpace) {isInit = false;draw(-1);initx = lastx;} else if (lastx < initx - mSpace) {isInit = false;draw(1);initx = lastx;}if (callback != null) {callback.run();}break;}return true;}private Runnable callback;//这个接口监听刻度尺的旋转public void setCallback(Runnable callback) {this.callback = callback;}//获取选中的值public int getRate() {return rate;}}

0 0
原创粉丝点击