0
Follow
0
View

Android draws multiple curves for color problems

koxingchen 注册会员
2023-02-28 12:28
) ); linePaint.set AntiAlias( true ) ; linePaint.set TextSize( dp2px ( getContext () , 16 )); linePaint.set StrokeWidth( dp2px ( getContext () , 1 )); valuePaint.set Color(Color. parseColor ( "#FF3DCC76" ) ); valuePaint.set AntiAlias( true ) ; valuePaint.set TextSize( dp2px ( getContext () , 16 )); valuePaint.set StrokeWidth( dp2px ( getContext () , 1 )); //画背景的画笔 shadowPaint = new Paint() ; shadowPaint.set Color(Color. parseColor ( "#CBF2ED" ) ); shadowPaint.set AntiAlias( true ) ; //画最下方文字的画笔 textPaint = new Paint() ; textPaint.set Color(Color. parseColor ( "#999999" ) ); textPaint.set AntiAlias( true ) ; textPaint.set TextSize( dp2px ( getContext () , 14 )); circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG) ; circlePaint.set Color(Color. parseColor ( "#8BE4D4" ) ); circlePaint.set StrokeWidth( dp2px ( getContext () , 2 )); circlePaint.set Style(Paint.Style.STROKE) ; circlePaint2 = new Paint(Paint.ANTI_ALIAS_FLAG) ; circlePaint2.set Color(Color.WHITE) ; circlePaint2.set Style(Paint.Style.FILL) ; baseShadow = new Paint() ; baseShadow.set AntiAlias( true ) ; baseShadow.set Color((Color.WHITE & 0x40FFFFFF) | 0x10000000); base Shadow .set Style(Paint.Style.FILL) ; buttom Heiht = dp2px(2); / / 线距离底部高度 m Paint = new Paint(Paint.ANTI_ALIAS_FLAG) ; m Paint .set Color(Color.GREEN) ; m Paint .set StrokeWidth( dp2px ( getContext () , 1)); m Paint .set Style(Paint.Style.STROKE) ; m Paint .set AntiAlias( true ) ; m Paint .set StrokeCap(Paint.Cap.ROUND) ; score Paint = new Paint(Paint.ANTI_ALIAS_FLAG) ; score Paint .set Style(Paint.Style.STROKE) ; score Paint .set StrokeCap(Paint.Cap.ROUND) ; score Paint .set Color(Color. parseColor ( "#DDDDDD" ) ); score Paint .set StrokeWidth( dp2px (0.5f) ); base LinePath = new Path() ; } @ Override protected void on Measure( int widthMeasureSpec , int heightMeasureSpec ) { super.on Measure( widthMeasureSpec , heightMeasureSpec ) ; sum Height = get MeasuredHeight() ; sum Width = get MeasuredWidth() ; } private void measure() { / / max Time = get MaxTime( timeList ) ; / / 最大分数为120 String text = " P "; Rect rect = new Rect() ; text Paint .get TextBounds( text , 0, text . length () , rect); one Height = ((sum Height - buttom Heiht - 2 * rect.height()) / 40); one Width = sum Width / (300 * 4); } / ** * activity进行hangdle更新,每隔一秒调取该方法 * / public void update Time( float value ) { x Index ++ ; to GetXy( value ) ; / / 获取x和y的坐标 invalidate(); } private int x Index = 0; private Canvas my Canvas ; @ Override protected void on Draw(Canvas canvas ) { super.on Draw( canvas ) ; my Canvas = canvas; measure(); draw BottomLine( myCanvas ) ; draw Line( myCanvas ) ; to DrawLine( myCanvas ) ; } private void to GetXy( float value ) { if (x Index == 1) { xy List .add( new PointF(0, sumHeight ) ); float x = one Width + x Index * 4 * one Width ; float y = (sum Height - (one Height * value)); xy List .add( new PointF( x + oneHeight , y - buttomHeiht ) ); } else { float x = one Width + x Index * 4 * one Width ; float y = (sum Height - (one Height * value)); xy List .add( new PointF( x + oneHeight , y - buttomHeiht ) ); } } private float l X = 0; private float l Y = 0; / ** * 画线 * / private void to DrawLine(Canvas canvas ) { if (xy List == null || xy List .size() == 0 || xy List .size() < 3) { return; } new Points = new ArrayList <>(); new Points .add All( xyList ) ; base LinePath .move To( newPoints . get ( newPoints . size () - 3).x, new Points .get( new Points .size() - 3).y); PointF p = new Points .get( new Points .size() - 2); PointF first PointF = new Points .get( new Points .size() - 3); float x1 = first PointF .x + l X ; float y1 = first PointF .y + l Y ; PointF second PointF = new Points .get( new Points .size() - 1); l X = (second PointF .x - first PointF .x) / 2 * smoothness; l Y = (second PointF .y - first PointF .y) / 2 * smoothness; float x2 = p.x - l X ; float y2 = p.y - l Y ; if (y1 == p.y) { y2 = y1; } base LinePath .cubic To( x1 , y1 , x2 , y2 , p . x , p . y ) ; if (0 == invale Index ) { m Paint .set Color(Color. parseColor ( "#FF4F7FFF" ) ); } else if (1 == invale Index ) { m Paint .set Color(Color. parseColor ( "#FFFFA640" ) ); } else if (2 == invale Index ) { m Paint .set Color(Color. parseColor ( "#FF3DCCC2" ) ); } canvas.draw Path( baseLinePath , mPaint ) ; } / ** * @param is Finish true 表示绘制三条完成 * / public void end Invale( boolean isFinish ) { if (is Finish ) { } else { if (invale Index == 2) { } else { invale Index ++ ; x Index = 0; xy List .clear(); / / m Paint = new Paint(Paint.ANTI_ALIAS_FLAG) ; / / m Paint .set Color(Color.GREEN) ; / / m Paint .set StrokeWidth( dp2px ( getContext () , 1)); / / m Paint .set Style(Paint.Style.STROKE) ; / / m Paint .set AntiAlias( true ) ; / / m Paint .set StrokeCap(Paint.Cap.ROUND) ; } } } private int click Position = -1; public void show TimeAndValue( int index ) { click Position = index; invalidate(); } / ** * 分割线 * * @param canvas * / private void draw Line(Canvas canvas ) { float m Max1 = 40; float m Max2 = m Max1 / 4 * 3; float m Max3 = m Max1 / 4 * 2; float m Max4 = m Max1 / 4 * 1; String text = "0"; Rect rect = new Rect() ; score Paint .get TextBounds( text , 0, text . length () , rect); text = m Max1 + ""; float y1 = (sum Height - (one Height * m Max1 )); / / canvas.draw Text( text , 0, y1 - buttomHeiht - rect . height () / 2 - dp2px(4), text Paint ); canvas.draw Line(0, 0 + buttomHeiht , sumWidth , 0 + buttomHeiht , scorePaint ) ; text = m Max3 + ""; float y2 = (sum Height - (one Height * m Max2 )); / / canvas.draw Text( text , 0, y2 - buttomHeiht - rect . height () / 2 - dp2px(4), text Paint ); canvas.draw Line(0, ( sumHeight - buttomHeiht * 2) / 4 * 3 + buttom Heiht , sum Width , (sum Height - buttom Heiht * 2) / 4 * 3 + buttom Heiht , score Paint ); text = m Max4 + ""; float y3 = (sum Height - (one Height * m Max3 )); / / canvas.draw Text( text , 0, y3 - buttomHeiht - rect . height () / 2 - dp2px(4), text Paint ); canvas.draw Line(0, ( sumHeight - buttomHeiht * 2) / 4 * 2 + buttom Heiht , sum Width , (sum Height - buttom Heiht * 2) / 4 * 2 + buttom Heiht , score Paint ); text = m Max1 + ""; float y4 = (sum Height - (one Height * m Max4 )); / / canvas.draw Text( text , 0, y4 - buttomHeiht - rect . height () / 2 - dp2px(4), text Paint ); canvas.draw Line(0, ( sumHeight - buttomHeiht * 2) / 4 * 1 + buttom Heiht , sum Width , (sum Height - buttom Heiht * 2) / 4 * 1 + buttom Heiht , score Paint ); } / ** * 底部标线 * * @param canvas * / private void draw BottomLine(Canvas canvas ) { Rect rect = new Rect() ; / / score Paint .set Color(Color.GREEN) ; score Paint .get TextBounds( "0" , 0, "0" . length () , rect); score Paint .set StrokeWidth(4) ; canvas.draw Line(0, 0, 0, sumHeight - dp2px (0) - rect.height() / 2, score Paint ); / / 画底部灰线 canvas.draw Line(0, sumHeight - dp2px (0) - rect.height() / 2, sum Width , sum Height - dp2px(0) - rect.height() / 2, score Paint ); / / 画底部灰线 / / canvas.draw Line(0, sumHeight - dp2px (15) - rect.height() / 2, sum Width , sum Height - dp2px(15) - rect.height() / 2, score Paint ); / / 画底部灰线 canvas.draw Line( sumWidth / 6 * 1, sumHeight - dp2px (0) - rect.height() / 2, sum Width / 6 * 1, sum Height - dp2px(0) - rect.height() - dp2px(3), score Paint ); / / 画底部灰线 canvas.draw Line( sumWidth / 6 * 2, sumHeight - dp2px (0) - rect.height() / 2, sum Width / 6 * 2, sum Height - dp2px(0) - rect.height() - dp2px(3), score Paint ); / / 画底部灰线 canvas.draw Line( sumWidth / 6 * 3, sumHeight - dp2px (0) - rect.height() / 2, sum Width / 6 * 3, sum Height - dp2px(0) - rect.height() - dp2px(3), score Paint ); / / 画底部灰线 canvas.draw Line( sumWidth / 6 * 4, sumHeight - dp2px (0) - rect.height() / 2, sum Width / 6 * 4, sum Height - dp2px(0) - rect.height() - dp2px(3), score Paint ); / / 画底部灰线 canvas.draw Line( sumWidth / 6 * 5, sumHeight - dp2px (0) - rect.height() / 2, sum Width / 6 * 5, sum Height - dp2px(0) - rect.height() - dp2px(3), score Paint ); / / 画底部灰线 canvas.draw Line( sumWidth / 6 * 6, sumHeight - dp2px (0) - rect.height() / 2, sum Width / 6 * 6, sum Height - dp2px(0) - rect.height() - dp2px(3), score Paint ); / / 画底部灰线 } / ** * 阴影层叠 * * @param canvas * @param Points * / private void draw Area(Canvas canvas , List Points) { LinearGradient m Shader = new LinearGradient(0, 0, 0, getMeasuredHeight () , new int []{ Color .parse Color( "#BAEFE6" ) , Color .parse Color( "#D7F5F0" ) , Color .parse Color( "#F9FEFD" ) }, new float []{0.5f, 0.65f, 0.85f}, Shader . TileMode . REPEAT ); base Shadow .set Shader( mShader ) ; if ( Points .size() > 0 && xy List != null && xy List .size() > 0) { base LinePath .line To( xyList . get (Points. size () - 1).x, sum Height - buttom Heiht ); base LinePath .line To( xyList . get (0) .x, sum Height - buttom Heiht ); base LinePath .close(); canvas.draw Path( baseLinePath , baseShadow ) ; } } public int dp2px( float dp) { final float scale = this.get Resources() .get DisplayMetrics() .density; return ( int ) (dp * scale + 0.5f); } / ** * 取出时间里面的最大的一个用来计算总长度 * * @param time List * @return * / public float get MaxTime(List timeList ) { max Time = 0; for ( int i = 0; i < time List .size(); i ++ ) { if (max Time < time List .get(i)) { max Time = time List .get(i); } } if (max Time <= 1000) { max Time = 1000; } else { max Time = 10000; } return max Time ; } public int dp2px( Context context, float dp) { final float scale = context.get Resources() .get DisplayMetrics() .density; return ( int ) (dp * scale + 0.5f); } public float dp2px( Resources resources, float dp) { final float scale = resources.get DisplayMetrics() .density; return dp * scale + 0.5f; } public float sp2px( Resources resources, float sp) { final float scale = resources.get DisplayMetrics() .scaled Density ; return sp * scale; } public String get CurrenTimeFromLongToGraph(String longTime ) { SimpleDateFormat formatter = new SimpleDateFormat( "MM-dd HH:mm" ) ; long milli Second = Long .value Of( longTime ) ; Date date = new Date() ; date.set Time( milliSecond ) ; return formatter.format(date); } } .
< !- - - - - >
ecdysis3238 注册会员
2023-02-28 12:28
< div class = " md_content_show“数据- v - 3967 e397 = " " >

< div class = " aw-list-img " > img

kritos 注册会员
2023-02-28 12:28

onDraw is not an incremental draw, and what you draw shows up. Color inconsistencies that can only happen if the properties you use to determine which line to draw are not consistent with the properties that determine the color, or are not synchronized.

kaishui1314 注册会员
2023-02-28 12:28

Where did you change the invaleIndex property?

admin 超级管理员
2023-02-28 12:28

The class is notified when a curve is finished, and then invaleIndex ++ is added before the next curve is drawn. The next one can be drawn, and it's the right color, but all the previous curves have changed color. And my curves are drawn in real time, not all at once

About the Author

Question Info

Publish Time
2023-02-28 12:28
Update Time
2023-02-28 12:28

Related Question

android,framework,aidl

继承的字段不能自动递增的房间Android

Android Http链接不上问题!

Android xml shape怎么绘制斜边矩形?

如何打开Android系统WebView的应用程序信息页面

reactnative axios模拟器iOS和模拟器Android获得网络错误

自定义视图视差滚动像Instagram个人资料页面(在Android)

无法解决所有文件的配置错误,而设置在android

android studio报错

为什么Android Studio我完成不了安装