内心想法xx
compose已经发布好久了,还没有用过compose写过ui,之前只是在官网上了解过,看着这可组合函数嵌套,我就脑袋大,更flutter一个德行,我的内心是抵触的,还是觉得用xml写香,抱着试一试的心态,打算用compose应用于实际项目中。(还是得多接触,多学习,真的是很香yyds)
无图无真相
上图,测试:通过滑动slider也是安卓称progressbar来改变速度
代码部分
@composable fun speedcontent(){ //添加数字动画,避免速度变化过于突兀 var mytargetvalue by remember { mutablestateof(0) } val progressint: int by animateintasstate(targetvalue = mytargetvalue) column(modifier = modifier .background(color = color.white) .fillmaxsize(), verticalarrangement = arrangement.bottom, horizontalalignment = alignment.centerhorizontally) { slider(value = mytargetvalue.tofloat(), onvaluechange = { mytargetvalue = it.toint() }, modifier = modifier.fillmaxwidth(), valuerange = 0f..180f) speedermeter(progressint) } }
具体实现
@optin(experimentaltextapi::class) @composable fun speedermeter(speednum: int,modifier: modifier = modifier .width(220.dp) .height(120.dp) .background(color = color.white)){ val centerpointcolor = color.blue val colorcenterpoint1 = color(0xff000000) val colorcenterpoint2 = color(0xffee3e07) val mersure = remembertextmeasurer() fun getpointx(centerx:float,angle:int,circlerdius:float,step:int):float{ val angles = (angle*step).todouble() val angle = math.toradians(angles) return centerx - cos(angle).tofloat()*(circlerdius) } fun getpointy(centery:float,angle:int,circlerdius:float,step:int):float{ val angles = (angle*step).todouble() val angle = math.toradians(angles) return centery- sin(angle).tofloat()*(circlerdius) } val myunit = "km/h" val speedtextlist = array(7){(it*20).tostring()} box(modifier, contentalignment = alignment.bottomcenter ) { canvas(modifier, ondraw = { val centerx = size.width/2 val bottomy = size.height-50 val mycolorstops = arrayof(0.0f to color.white,1.0f to color.black) //画指针底座 drawcircle( brush = brush.radialgradient(colorstops = mycolorstops), radius = 30f, center = offset(centerx,bottomy+15.dp.value), ) drawcircle( color = color.white, radius = 23f, center = offset(centerx,bottomy+15.dp.value) ) drawcircle( color = color.red, radius = 20f, center = offset(centerx,bottomy+15.dp.value) ) //画单位 drawtext( textmeasurer = mersure, text = myunit, style = textstyle(color = color.black, fontsize = 12.sp, fontweight = fontweight(20), fontfamily = fontfamily.serif), topleft = offset(centerx-50.dp.value,bottomy-90) ) //画刻度 repeat(61){ val fontweights = if( it%5==0){ if(it%10==0){ 7f }else{ 4f } }else{ 2f } val lengths = if(it%5==0){ if(it%10==0){ 25 }else{ 22 } }else{ 20 } drawline( color = color.black, start = offset( getpointx(centerx,it,bottomy,3), getpointy(centerx,it,bottomy,3) ), end = offset( getpointx(centerx,it,bottomy-lengths,3), getpointy(centerx,it,bottomy-lengths,3) ), strokewidth = fontweights, cap = strokecap.round ) } //画数字 repeat(speedtextlist.size){ val mytopleft = offset( getpointx(centerx,it,bottomy-70,30)-25.dp.value, getpointy(bottomy,it,bottomy-70,30)) val colortype = if(it > 4){ color.red }else{ color.black } drawtext( textmeasurer = mersure, text = speedtextlist[it], topleft =mytopleft, style = textstyle(colortype, fontsize = 11.sp, fontfamily = fontfamily.serif) ) } //画指针 drawline( color = color.red, start = offset( getpointx(centerx,speednum,bottomy-30,1), getpointy(centerx,speednum,bottomy-30,1) ), end = offset( getpointx(centerx,speednum,0f,1), getpointy(centerx,speednum,0f,1) ), strokewidth = 15f, cap = strokecap.round ) }) } }
compose还是蛮香的,用了就回不去了<捂脸>,只是这个代码的可读性没有java的好。
到此这篇关于android compose学习之绘制速度表盘的文章就介绍到这了,更多相关compose绘制速度表盘内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论