一对一视频app源码,Android 绘制渐变色
一对一视频app源码,Android 绘制渐变色实现的相关代码
一. 在xml文件中绘制
startColor:渐变色起始颜色
endColor:渐变色结束颜色
angle:绘制角度
代码:
```handlebars <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <gradient android:startColor="@color/colorStart" android:angle="-45" android:endColor="@color/colorEnd"/> <size android:width="@dimen/color_size" android:height="@dimen/color_size"/> <corners android:radius="@dimen/radius"/> </shape> ```
二.在代码中绘制
重写ondraw方法,使用LinearGradient绘制渐变View
```handlebars override fun onDraw(canvas: Canvas?) { if (mStartColor == null || mEndColor == null) { return } val w: Float = measuredWidth.toFloat() val h: Float = measuredHeight.toFloat() val linearGradient = LinearGradient(0f, 0f, w, h, mStartColor!!, mEndColor!!, Shader.TileMode.CLAMP) val rectF = RectF(0f, 0f, w, h) mPaint.shader = linearGradient //角度从(0,0)到(w,h)的渐变色 canvas?.drawRoundRect(rectF, mRectRadius, mRectRadius, mPaint); } ```
三.绘制一组渐变色
把起始颜色和结束颜色定义在xml中:
```handlebars <?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="gradient_start"> <item>#2ae0c8 </item> <item>#a2e1d4</item> <item>#acf6ef</item> <item>#cbf5fb</item> <item>#bdf3d4</item> <item>#e6e2c3</item> <item>#e3c887</item> <item>#fad8be</item> <item>#fbb8ac</item> <item>#fe6673</item> <item>#2ae0c8 </item> <item>#a2e1d4</item> <item>#acf6ef</item> <item>#cbf5fb</item> <item>#bdf3d4</item> <item>#e6e2c3</item> <item>#e3c887</item> <item>#fad8be</item> <item>#fbb8ac</item> <item>#fe6673</item> </string-array> <string-array name="gradient_end"> <item>#e3c887</item> <item>#fad8be</item> <item>#e3c887</item> <item>#fad8be</item> <item>#fe6673</item> <item>#acf6ef</item> <item>#EE8EBE</item> <item>#a2e1d4</item> <item>#acf6ef</item> <item>#cbf5fb</item> <item>#e3c887</item> <item>#fad8be</item> <item>#e3c887</item> <item>#fad8be</item> <item>#fe6673</item> <item>#acf6ef</item> <item>#EE8EBE</item> <item>#a2e1d4</item> <item>#acf6ef</item> <item>#cbf5fb</item> </string-array> </resources> ```
自定义一个布局,在布局中添加绘制的渐变色方块:
```handlebars private fun initColor() { val startColor = resources.getStringArray(R.array.gradient_start) val endColor = resources.getStringArray(R.array.gradient_end) for (i in startColor.indices) { val view = getView(Color.parseColor(startColor[i]), Color.parseColor(endColor[i])) addView(view) } } ```
以上就是,一对一视频app源码,Android 绘制渐变色实现的相关代码 更多内容欢迎关注之后的文章