簡單介紹android實現可以滑動的平滑曲線圖
導讀 | 這篇文章主要為大家詳細介紹了android實現可以滑動的平滑曲線圖,文中示例程式碼介紹的非常詳細,具有一定的參考價值,感興趣的小夥伴們可以參考一下 |
本文例項為大家分享了android實現可以滑動的平滑曲線圖的具體程式碼,供大家參考,具體內容如下
直接上程式碼,裡面有詳細註解
1 attr 屬性編寫
< attr name="xy_line_color" format="color" /> < !-- xy座標軸寬度 --> < attr name="xy_line_width" format="dimension" /> < !-- xy座標軸文字顏色 --> < attr name="xy_text_color" format="color" /> < !-- xy座標軸文字大小 --> < attr name="xy_text_size" format="dimension" /> < !-- 折線圖中折線的顏色 --> < attr name="line_color" format="color" /> < attr name="interval" format="dimension" /> < !-- 背景顏色 --> < attr name="bg_color" format="color" /> < !-- 曲線選中外部顏色 --> < attr name="select_circle_color" format="color" /> < !-- 曲線選中內部顏色 --> < attr name="select_reminder_color" format="color" /> < !--是否抬手滾動--> < attr name="isScroll" format="boolean" /> < declare-styleable name="ChartView"> < attr name="xy_line_color" /> < attr name="xy_line_width" /> < attr name="xy_text_color" /> < attr name="xy_text_size" /> < attr name="line_color" /> < attr name="interval" /> < attr name="bg_color" /> < attr name="select_circle_color" /> < attr name="select_reminder_color" /> < attr name="isScroll" /> < !--提示框跟滑動顯示的位置--> < attr name="show_position"> < enum name="first" value="1" /> < enum name="middle" value="2" /> < enum name="end" value="3" /> < /attr> < /declare-styleable>
2 ChartView
package com.laisontech.commonuilibrary.customviews; import android.animation.Animator; import android.animation.ValueAnimator; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.graphics.PorterDuff; import android.graphics.PorterDuffXfermode; import android.graphics.Rect; import android.graphics.RectF; import android.util.AttributeSet; import android.util.Log; import android.util.TypedValue; import android.view.MotionEvent; import android.view.VelocityTracker; import android.view.View; import android.view.animation.DecelerateInterpolator; import com.laisontech.commonuilibrary.R; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * 自定義折線圖 */ public class ChartView extends View { private static final int FIRST = 1; private static final int MIDDLE = 2; private static final int END = 3; //xy座標軸顏色 private int xyLineColor = 0xffCFE2CF; //折線選中的圓形顏色 private int selectCircleColor = 0xff00A8FF; //選中資料提示框顏色 private int selectReminderColor = 0xff00A8FF; //折線中圓形內部部顏色 private int xyTextColor = 0xff0014FF; //折線圖中折線的顏色 private int lineColor = 0xffFD00FF; //xy座標軸寬度 private int xyLineWidth = dpToPx(1); //xy座標軸文字大小 private int xyTextSize = spToPx(12); //x軸各個座標點水平間距 private int interval = dpToPx(40); //背景顏色 private int bgColor = 0xffffffff; //是否有起手時的滑動感 private boolean isScroll = false; //提示框顯示位置 private int mShowPositionType = 3; //繪製XY軸座標對應的畫筆 private Paint mXYPaint; //繪製XY軸的文字對應的畫筆 private Paint mXYTextPaint; //畫折線對應的畫筆 private Paint mSpinnerLinePaint; private int width; private int height; //x軸的原點座標 private int mXOri; //y軸的原點座標 private int mYOri; //第一個點X的座標 private float mXInit; //第一個點對應的最大X座標 private float maxXInit; //第一個點對應的最小X座標 private float minXInit; //x軸座標對應的資料 private ListmXData = new ArrayList<>(); //y軸座標對應的資料 private ListmYData = new ArrayList<>(); //折線對應的資料 private MapmSpinnerValue = new HashMap<>(); //點選的點對應的X軸的第幾個點,預設1 private int selectIndex = 1; //X軸刻度文字對應的最大矩形,為了選中時,在x軸文字畫的框框大小一致,獲取從資料中得到的x軸資料,獲得最長資料 private Rect xValueRect; //速度檢測器 private VelocityTracker mTracker; //是否為短距離滑動 private boolean isShortSlide = false; //獲取尺寸的的中間 private int mSelectMiddle = 0; //曲線切率 private float mLineSmoothness = 0.18f; public ChartView(Context context) { this(context, null); } public ChartView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public ChartView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context, attrs, defStyleAttr); initPaint(); } //設定切率 public void setLineSmoothness(float lineSmoothness) { if (lineSmoothness != this.mLineSmoothness) { this.mLineSmoothness = lineSmoothness; } } /** * 初始化 */ private void initPaint() { mXYPaint = new Paint(); mXYPaint.setAntiAlias(true); mXYPaint.setStrokeWidth(xyLineWidth); mXYPaint.setStrokeJoin(Paint.Join.ROUND); mXYPaint.setColor(xyLineColor); mXYTextPaint = new Paint(); mXYTextPaint.setAntiAlias(true); mXYTextPaint.setTextSize(xyTextSize); mXYTextPaint.setStrokeJoin(Paint.Join.ROUND); mXYTextPaint.setColor(xyTextColor); mXYTextPaint.setStyle(Paint.Style.STROKE); mSpinnerLinePaint = new Paint(); mSpinnerLinePaint.setAntiAlias(true); mSpinnerLinePaint.setStrokeWidth(xyLineWidth); mSpinnerLinePaint.setColor(lineColor); mSpinnerLinePaint.setStyle(Paint.Style.STROKE); mSpinnerLinePaint.setStrokeJoin(Paint.Join.ROUND); } /** * 初始化 * * @param context * @param attrs * @param defStyleAttr */ private void init(Context context, AttributeSet attrs, int defStyleAttr) { TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.ChartView, defStyleAttr, 0); int count = array.getIndexCount(); for (int i = 0; i < count; i++) { int attr = array.getIndex(i); if (attr == R.styleable.ChartView_xy_line_color) { xyLineColor = array.getColor(attr, xyLineColor); } else if (attr == R.styleable.ChartView_xy_line_width) { xyLineWidth = (int) array.getDimension(attr, TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, xyLineWidth, getResources().getDisplayMetrics())); } else if (attr == R.styleable.ChartView_xy_text_color) { xyTextColor = array.getColor(attr, xyTextColor); } else if (attr == R.styleable.ChartView_xy_text_size) { xyTextSize = (int) array.getDimension(attr, TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, xyTextSize, getResources().getDisplayMetrics())); } else if (attr == R.styleable.ChartView_line_color) { lineColor = array.getColor(attr, lineColor); } else if (attr == R.styleable.ChartView_interval) { interval = (int) array.getDimension(attr, TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, interval, getResources().getDisplayMetrics())); } else if (attr == R.styleable.ChartView_bg_color) { bgColor = array.getColor(attr, bgColor); } else if (attr == R.styleable.ChartView_select_circle_color) { selectCircleColor = array.getColor(attr, selectCircleColor); } else if (attr == R.styleable.ChartView_select_reminder_color) { selectReminderColor = array.getColor(attr, selectReminderColor); } else if (attr == R.styleable.ChartView_isScroll) { isScroll = array.getBoolean(attr, isScroll); } else if (attr == R.styleable.ChartView_show_position) { mShowPositionType = array.getInt(attr, mShowPositionType); } } array.recycle(); } @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { if (changed) { width = getWidth(); height = getHeight(); //Y軸文字的最大寬度 float textYWdith = getTextBounds(mYData.get(getListItemMaxIndex(mYData)) + "", mXYTextPaint).width(); for (int i = 0; i < mYData.size(); i++) {//求取y軸文字最大的寬度 float temp = getTextBounds(mYData.get(i) + "", mXYTextPaint).width(); if (temp > textYWdith) textYWdith = temp; } int dp2 = dpToPx(2); int dp3 = dpToPx(3); mXOri = (int) (dp2 + textYWdith + dp2 + xyLineWidth); //獲取x軸的最長文字的寬度所佔的矩形 xValueRect = getTextBounds(mXData.get(getListItemMaxIndex(mXData)), mXYTextPaint); //X軸文字高度 float textXHeight = xValueRect.height(); for (int i = 0; i < mXData.size(); i++) { Rect rect = getTextBounds(mXData.get(i) + "", mXYTextPaint); if (rect.height() > textXHeight) textXHeight = rect.height(); if (rect.width() > xValueRect.width()) xValueRect = rect; } mYOri = (int) (height - dp2 - textXHeight - dp3 - xyLineWidth); mXInit = mXOri + xValueRect.width() / 2 + dpToPx(5); minXInit = width - (width - mXOri) * 0.1f - interval * (mXData.size() - 1); maxXInit = mXInit; } selectIndex = getSelectIndexFromShowType(mShowPositionType); super.onLayout(changed, left, top, right, bottom); } @Override protected void onDraw(Canvas canvas) { canvas.drawColor(bgColor); drawXY(canvas); drawBrokenLineAndPoint(canvas); } /** * 繪製交點處對應的點 */ private void drawBrokenLineAndPoint(Canvas canvas) { if (mXData.size() <= 0) return; int layerId = canvas.saveLayer(0, 0, width, height, null, Canvas.ALL_SAVE_FLAG); drawBrokenLine(canvas); drawBrokenPoint(canvas); // 將超出x軸座標的部分截掉 mSpinnerLinePaint.setStyle(Paint.Style.FILL); mSpinnerLinePaint.setColor(bgColor); mSpinnerLinePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); RectF rectF = new RectF(0, 0, mXOri, height); canvas.drawRect(rectF, mSpinnerLinePaint); mSpinnerLinePaint.setXfermode(null); canvas.restoreToCount(layerId); } /** * 繪製曲線對應的點 */ private void drawBrokenPoint(Canvas canvas) { float dp2 = dpToPx(2); float dp4 = dpToPx(4); float dp7 = dpToPx(7); Log.e("selectIndex", "index:" + selectIndex); //繪製節點 for (int i = 0; i < mXData.size(); i++) { float x = mXInit + interval * i; float y = mYOri - mYOri * (1 - 0.1f) * mSpinnerValue.get(mXData.get(i)) / mYData.get(mYData.size() - 1); //繪製選中點 if (i == selectIndex - 1) { mSpinnerLinePaint.setStyle(Paint.Style.FILL); //設定選中顏色 mSpinnerLinePaint.setColor(selectCircleColor); canvas.drawCircle(x, y, dp7, mSpinnerLinePaint); mSpinnerLinePaint.setColor(selectReminderColor); canvas.drawCircle(x, y, dp4, mSpinnerLinePaint); drawFloatTextBox(canvas, x, y - dp7, mSpinnerValue.get(mXData.get(i))); } //繪製普通節點 mSpinnerLinePaint.setStyle(Paint.Style.FILL); mSpinnerLinePaint.setColor(Color.WHITE); canvas.drawCircle(x, y, dp2, mSpinnerLinePaint); mSpinnerLinePaint.setStyle(Paint.Style.STROKE); mSpinnerLinePaint.setColor(lineColor); canvas.drawCircle(x, y, dp2, mSpinnerLinePaint); } } /** * 繪製浮動框 * */ private void drawFloatTextBox(Canvas canvas, float x, float y, int text) { int dp6 = dpToPx(6); int dp18 = dpToPx(18); //p1 Path path = new Path(); path.moveTo(x, y); //p2 path.lineTo(x - dp6, y - dp6); //p3 path.lineTo(x - dp18, y - dp6); //p4 path.lineTo(x - dp18, y - dp6 - dp18); //p5 path.lineTo(x + dp18, y - dp6 - dp18); //p6 path.lineTo(x + dp18, y - dp6); //p7 path.lineTo(x + dp6, y - dp6); //p1 path.lineTo(x, y); canvas.drawPath(path, mSpinnerLinePaint); mSpinnerLinePaint.setColor(Color.WHITE); mSpinnerLinePaint.setTextSize(spToPx(14)); Rect rect = getTextBounds(text + "", mSpinnerLinePaint); canvas.drawText(text + "", x - rect.width() / 2, y - dp6 - (dp18 - rect.height()) / 2, mSpinnerLinePaint); } /** * 繪製平滑曲線 */ private void drawBrokenLine(Canvas canvas) { mSpinnerLinePaint.setStyle(Paint.Style.STROKE); mSpinnerLinePaint.setColor(lineColor); //繪製折線 Path path = new Path(); float prePreviousPointX = Float.NaN; float prePreviousPointY = Float.NaN; float previousPointX = Float.NaN; float previousPointY = Float.NaN; float currentPointX = Float.NaN; float currentPointY = Float.NaN; float nextPointX; float nextPointY; int lineSize = mXData.size(); for (int i = 0; i < lineSize; i++) { float x; float y; if (Float.isNaN(currentPointX)) { currentPointX = getSpinnerPoint(i).x; currentPointY = getSpinnerPoint(i).y; } if (Float.isNaN(previousPointX)) { //是第一個點? if (i > 0) { previousPointX = getSpinnerPoint(i - 1).x; previousPointY = getSpinnerPoint(i - 1).y; } else { //用當前點表示上一個點 previousPointX = currentPointX; previousPointY = currentPointY; } } if (Float.isNaN(prePreviousPointX)) { //是前兩個點? if (i > 1) { prePreviousPointX = getSpinnerPoint(i - 2).x; prePreviousPointY = getSpinnerPoint(i - 2).y; } else { //當前點表示上上個點 prePreviousPointX = previousPointX; prePreviousPointY = previousPointY; } } // 判斷是不是最後一個點了 if (i < lineSize - 1) { nextPointX = getSpinnerPoint(i + 1).x; nextPointY = getSpinnerPoint(i + 1).y; } else { //用當前點表示下一個點 nextPointX = currentPointX; nextPointY = currentPointY; } if (i == 0) { // 將Path移動到開始點 path.moveTo(currentPointX, currentPointY); } else { // 求出控制點座標 final float firstDiffX = (currentPointX - prePreviousPointX); final float firstDiffY = (currentPointY - prePreviousPointY); final float secondDiffX = (nextPointX - previousPointX); final float secondDiffY = (nextPointY - previousPointY); final float firstControlPointX = previousPointX + (mLineSmoothness * firstDiffX); final float firstControlPointY = previousPointY + (mLineSmoothness * firstDiffY); final float secondControlPointX = currentPointX - (mLineSmoothness * secondDiffX); final float secondControlPointY = currentPointY - (mLineSmoothness * secondDiffY); //畫出曲線 path.cubicTo(firstControlPointX, firstControlPointY, secondControlPointX, secondControlPointY, currentPointX, currentPointY); } // 更新 prePreviousPointX = previousPointX; prePreviousPointY = previousPointY; previousPointX = currentPointX; previousPointY = currentPointY; currentPointX = nextPointX; currentPointY = nextPointY; } canvas.drawPath(path, mSpinnerLinePaint); } /** * 繪製XY座標 */ private void drawXY(Canvas canvas) { int length = dpToPx(5);//刻度的長度 //繪製Y座標 canvas.drawLine(mXOri - xyLineWidth / 2, 0, mXOri - xyLineWidth / 2, mYOri, mXYPaint); //繪製箭頭 mXYPaint.setStyle(Paint.Style.STROKE); Path path = new Path(); path.moveTo(mXOri - xyLineWidth / 2 - dpToPx(5), dpToPx(12)); path.lineTo(mXOri - xyLineWidth / 2, xyLineWidth / 2); path.lineTo(mXOri - xyLineWidth / 2 + dpToPx(5), dpToPx(12)); canvas.drawPath(path, mXYPaint); //繪製刻度 int yLength = (int) (mYOri * (1 - 0.1f) / (mYData.size() - 1));//y軸上面空出10%,計算出y軸刻度間距 for (int i = 0; i < mYData.size(); i++) { //繪製刻度 canvas.drawLine(mXOri, mYOri - yLength * i + xyLineWidth / 2, mXOri + length, mYOri - yLength * i + xyLineWidth / 2, mXYPaint); mXYTextPaint.setColor(xyTextColor); //繪製文字 String text = mYData.get(i) + ""; Rect rect = getTextBounds(text, mXYTextPaint); canvas.drawText(text, 0, text.length(), mXOri - xyLineWidth - dpToPx(2) - rect.width(), mYOri - yLength * i + rect.height() / 2, mXYTextPaint); } //繪製座標 canvas.drawLine(mXOri, mYOri + xyLineWidth / 2, width, mYOri + xyLineWidth / 2, mXYPaint); //繪製箭頭 mXYPaint.setStyle(Paint.Style.STROKE); path = new Path(); //整個長度 float xLength = mXInit + interval * (mXData.size() - 1) + (width - mXOri) * 0.1f; if (xLength < width) xLength = width; path.moveTo(xLength - dpToPx(12), mYOri + xyLineWidth / 2 - dpToPx(5)); path.lineTo(xLength - xyLineWidth / 2, mYOri + xyLineWidth / 2); path.lineTo(xLength - dpToPx(12), mYOri + xyLineWidth / 2 + dpToPx(5)); canvas.drawPath(path, mXYPaint); //繪製x軸刻度 for (int i = 0; i < mXData.size(); i++) { float x = mXInit + interval * i; if (x >= mXOri) {//只繪製從原點開始的區域 mXYTextPaint.setColor(xyTextColor); canvas.drawLine(x, mYOri, x, mYOri - length, mXYPaint); //繪製X軸文字 String text = mXData.get(i); Rect rect = getTextBounds(text, mXYTextPaint); if (i == selectIndex - 1) { mXYTextPaint.setColor(lineColor); canvas.drawText(text, 0, text.length(), x - rect.width() / 2, mYOri + xyLineWidth + dpToPx(2) + rect.height(), mXYTextPaint); canvas.drawRoundRect(x - xValueRect.width() / 2 - dpToPx(3), mYOri + xyLineWidth + dpToPx(1), x + xValueRect.width() / 2 + dpToPx(3), mYOri + xyLineWidth + dpToPx(2) + xValueRect.height() + dpToPx(2), dpToPx(2), dpToPx(2), mXYTextPaint); } else { canvas.drawText(text, 0, text.length(), x - rect.width() / 2, mYOri + xyLineWidth + dpToPx(2) + rect.height(), mXYTextPaint); } } } } private float startX; private float startx; @Override public boolean onTouchEvent(MotionEvent event) { if (isScrolling) return super.onTouchEvent(event); //當該view獲得點選事件,就請求父控制元件不攔截事件 this.getParent().requestDisallowInterceptTouchEvent(true); obtainVelocityTracker(event); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: startX = event.getX(); startx = event.getX(); Log.e("XXXX", "down:" + startX + ""); break; case MotionEvent.ACTION_MOVE: //滑動距離小於等於8的時候任務為短距離滑動 //當前x軸的尺寸與設定的x軸間隔的距離之乘積大於 螢幕中的顯示佈局寬度與x軸七點之差時,開始移動 if (interval * mXData.size() > width - mXOri) { //獲取滑動的距離 float dis = event.getX() - startX; //重新賦值給startX startX = event.getX(); //當前x原點距離與左右滑動的距離之和沒有最小值大,則將當前x距離賦值為最小,以下相似 if (mXInit + dis < minXInit) { mXInit = minXInit; } else if (mXInit + dis > maxXInit) { mXInit = maxXInit; } else { mXInit = mXInit + dis; } invalidate(); } break; case MotionEvent.ACTION_UP: isShortSlide = Math.abs(event.getX() - startx) <= dpToPx(8); clickAction(event); scrollAfterActionUp(); this.getParent().requestDisallowInterceptTouchEvent(false); recycleVelocityTracker(); break; case MotionEvent.ACTION_CANCEL: //增加這行程式碼防止與父類的滑動事件衝突 this.getParent().requestDisallowInterceptTouchEvent(false); recycleVelocityTracker(); break; } return true; } //是否正在滑動 private boolean isScrolling = false; /** * 手指抬起後的滑動處理 */ private void scrollAfterActionUp() { if (!isScroll) return; final float velocity = getVelocity(); float scrollLength = maxXInit - minXInit; if (Math.abs(velocity) < 10000) scrollLength = (maxXInit - minXInit) * Math.abs(velocity) / 10000; ValueAnimator animator = ValueAnimator.ofFloat(0, scrollLength); animator.setDuration((long) (scrollLength / (maxXInit - minXInit) * 1000));//時間最大為1000毫秒,此處使用比例進行換算 animator.setInterpolator(new DecelerateInterpolator()); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator valueAnimator) { float value = (float) valueAnimator.getAnimatedValue(); if (velocity < 0 && mXInit > minXInit) {//向左滑動 if (mXInit - value <= minXInit) mXInit = minXInit; else mXInit = mXInit - value; } else if (velocity > 0 && mXInit < maxXInit) {//向右滑動 if (mXInit + value >= maxXInit) mXInit = maxXInit; else mXInit = mXInit + value; } invalidate(); } }); animator.addListener(new Animator.AnimatorListener() { @Override public void onAnimationStart(Animator animator) { isScrolling = true; } @Override public void onAnimationEnd(Animator animator) { isScrolling = false; } @Override public void onAnimationCancel(Animator animator) { isScrolling = false; } @Override public void onAnimationRepeat(Animator animator) { } }); animator.start(); } /** * 獲取速度 * * @return */ private float getVelocity() { if (mTracker != null) { mTracker.computeCurrentVelocity(1000); return mTracker.getXVelocity(); } return 0; } /** * 點選X軸座標或者折線節點 * */ // 44 142 139 private void clickAction(MotionEvent event) { int dp8 = dpToPx(8); float eventX = event.getX(); float eventY = event.getY(); if (!isShortSlide) { for (int i = 0; i < mXData.size(); i++) { float x = mXInit + interval * i; float start = mXOri; if (x >= start + (mSelectMiddle - 1) * interval && x < start + mSelectMiddle * interval) { selectIndex = i + 1; invalidate(); } } return; } for (int i = 0; i < mXData.size(); i++) { //節點 float x = mXInit + interval * i; float y = mYOri - mYOri * (1 - 0.1f) * mSpinnerValue.get(mXData.get(i)) / mYData.get(mYData.size() - 1); if (eventX >= x - dp8 && eventX <= x + dp8 && eventY >= y - dp8 && eventY <= y + dp8 && selectIndex != i + 1) {//每個節點周圍範圍內都是可點選區域 selectIndex = i + 1; invalidate(); return; } //X軸刻度 String text = mXData.get(i); Rect rect = getTextBounds(text, mXYTextPaint); x = mXInit + interval * i; y = mYOri + xyLineWidth + dpToPx(2); if (eventX >= x - rect.width() / 2 - dp8 && eventX <= x + rect.width() + dp8 / 2 && eventY >= y - dp8 && eventY <= y + rect.height() + dp8 && selectIndex != i + 1) { selectIndex = i + 1; invalidate(); return; } } } /** * 獲取速度跟蹤器 * * @param event */ private void obtainVelocityTracker(MotionEvent event) { if (!isScroll) return; if (mTracker == null) { mTracker = VelocityTracker.obtain(); } mTracker.addMovement(event); } /** * 回收速度跟蹤器 */ private void recycleVelocityTracker() { if (mTracker != null) { mTracker.recycle(); mTracker = null; } } /** * 根據使用者輸入顯示型別,在滑動時在不同的位置顯示提示框 */ private int getSelectIndexFromShowType(int showPositionType) { int visibleScale = (width - mXOri) / interval; switch (showPositionType) { case FIRST: mSelectMiddle = 1; return mSelectMiddle; case MIDDLE: if (mXData.size() <= visibleScale) { mSelectMiddle = middleIndex(mXData.size()); } else { mSelectMiddle = middleIndex(visibleScale); } return mSelectMiddle; //螢幕可顯示的刻度 case END: if (mXData.size() <= visibleScale) { mSelectMiddle = mXData.size(); } else { mSelectMiddle = visibleScale; } return visibleScale; default: mSelectMiddle = 0; return mSelectMiddle; } } public void setValue(Mapvalue) { this.mSpinnerValue = value; invalidate(); } public void setValue(Mapvalue, ListxValue, ListyValue) { this.mSpinnerValue = value; this.mXData = xValue; this.mYData = yValue; invalidate(); } public MapgetValue() { return mSpinnerValue; } /** * 獲取丈量文字的矩形 * * @param text * @param paint * @return */ private Rect getTextBounds(String text, Paint paint) { Rect rect = new Rect(); paint.getTextBounds(text, 0, text.length(), rect); return rect; } /** * dp轉化成為px * * @param dp * @return */ private int dpToPx(int dp) { float density = getContext().getResources().getDisplayMetrics().density; return (int) (dp * density + 0.5f * (dp >= 0 ? 1 : -1)); } /** * sp轉化為px * * @param sp * @return */ private int spToPx(int sp) { float scaledDensity = getContext().getResources().getDisplayMetrics().scaledDensity; return (int) (scaledDensity * sp + 0.5f * (sp >= 0 ? 1 : -1)); } /** * 獲取集合中最長的index */ private static final int NULL_INDEX = -1; public int getListItemMaxIndex(List data) { if (data == null || data.size() < 1) { return NULL_INDEX; } int max = (data.get(0) + "").length(); for (int i = 0; i < data.size(); i++) { String s = data.get(i) + ""; if (s.length() > max) { return i; } } return NULL_INDEX; } //獲得在滑動結束的時候在螢幕內的點 private int middleIndex(int size) { if (size % 2 == 0) { return size / 2; } else { return size / 2 + 1; } } /** * 根據兩點座標獲取中間某個點 * * @param from 座標1 * @param to 座標2 */ //獲取已知點的斜率 y = kx+b private float getSlope(Point from, Point to) { float k = (to.y - from.y) / (to.x - from.x); Log.e("Point", "引數b:" + k); return k; } //獲取引數 b private float getParams(Point from, Point to) { float b = from.y - (getSlope(from, to) * from.x); Log.e("Point", "引數b:" + b); return b; } //根據兩點間的座標獲取x軸的任意一個座標x值, private float getArbitrarilyX(Point from, Point to, int grade, int needGrade) { //獲得輸入的新座標 float x = ((to.x - from.x) * needGrade) / grade + from.x; Log.e("Point", "x座標值:" + x); return x; } //獲取座標值 private Point getPoint(Point from, Point to, int grade, int needGrade) { Point point = new Point(); point.setX(getArbitrarilyX(from, to, grade, needGrade)); float slope = getSlope(from, to); point.setY(slope * point.x + getParams(from, to)); return point; } //獲取繪製折線的點 private Point getSpinnerPoint(int valueIndex) { float x = mXInit + interval * (valueIndex); float y = mYOri - mYOri * (1 - 0.1f) * mSpinnerValue.get(mXData.get(valueIndex)) / mYData.get(mYData.size() - 1); return new Point(x, y); } private class Point { float x; float y; public Point() { } public float getX() { return x; } public void setX(float x) { this.x = x; } public float getY() { return y; } public void setY(float y) { this.y = y; } public Point(float x, float y) { this.x = x; this.y = y; } @Override public String toString() { return "Point{" + "x=" + x + ", y=" + y + '}'; } } }
以上就是本文的全部內容,希望對大家的學習有所幫助。
原文來自:
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69955379/viewspace-2906103/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Flutter 實現平滑曲線折線圖Flutter
- 簡單介紹NMS的實現方法
- 簡單介紹Android自定義View實現時鐘功能AndroidView
- 用ListView簡單實現滑動列表View
- 簡單介紹numpy實現RNN原理實現RNN
- jquery 實現滑動條的簡單驗證jQuery
- RPC模式的介紹以及簡單的實現RPC模式
- Android 身高體重曲線的實現Android
- 簡單介紹vue3.x 使用jsplumb實現拖拽連線VueJS
- 簡單介紹Vue實現滑鼠懸浮切換圖片srcVue
- 簡單介紹Go 字串比較的實現示例Go字串
- 簡單介紹VBS 批次Ping的專案實現
- 簡單介紹pytorch中log_softmax的實現PyTorch
- 簡單介紹python中的單向連結串列實現Python
- html裡列表滑動刪除的實現如此簡單HTML
- 簡單介紹Pytorch實現WGAN用於動漫頭像生成PyTorch
- Android-貝塞爾曲線實現水波紋動畫Android動畫
- 簡單介紹SpringMVC RESTFul實現列表功能SpringMVCREST
- 簡單的圖神經網路介紹神經網路
- Android通過輔助功能實現搶微信紅包原理簡單介紹Android
- SVG繪製直線簡單介紹SVG
- Android 的滑動分析以及各種實現Android
- 使用jQuery實現的平滑滾動輪播圖jQuery
- 簡單介紹Angular單元測試之事件觸發的實現Angular事件
- Android開發:RecyclerView平滑流暢的滑動到指定位置AndroidView
- 簡單易懂的 Go 泛型使用和實現原理介紹Go泛型
- Webpack 的簡單介紹Web
- AOP的簡單介紹
- 使用 ActionScript 實現簡單滑動驗證碼識別
- 簡單介紹shell中的curl網路請求的實現
- form表單的簡單介紹ORM
- Flownet 介紹 及光流的簡單介紹
- canvas小畫板--(1)平滑曲線Canvas
- Android中SharePreferences的簡單實現Android
- android可以無限迴圈滑動的ViewPagerAndroidViewpager
- RSA加密演算法簡單介紹以及python實現加密演算法Python
- canvas進階——如何畫出平滑的曲線?Canvas
- Android動畫曲線庫AndroidEasingFunctionsAndroid動畫IdeaFunction