实现放射线效果
2020-09-09 本文已影响0人
WLHere
效果图
image.pngxml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation="0dp"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<View
android:id="@+id/view"
android:layout_width="200dp"
android:layout_height="200dp" />
</LinearLayout>
code
findViewById<View>(R.id.view).background = RadiateLightsDrawable()
/**
* 放射线
*/
private const val START_COLOR: Int = 0xffff0000.toInt()
private const val END_COLOR: Int = 0x00ff0000
class RadiateLightsDrawable : Drawable() {
private val mPaint = Paint(Paint.ANTI_ALIAS_FLAG)
private val mTempRect = RectF()
private val mAreaCount = 16
private val mSingleAreaAngle = 360f / mAreaCount
private val mSweepAngle = mSingleAreaAngle / 2
private val mStartAngle = -mSweepAngle / 2
init {
mPaint.style = Paint.Style.FILL
mPaint.alpha = (255 * 0.5).toInt()
}
override fun draw(canvas: Canvas) {
canvas.save()
val centerX = canvas.width / 2f
val centerY = canvas.height / 2f
val radius = min(centerX, centerY)
mTempRect.set(centerX - radius, centerY - radius, centerX + radius, centerY + radius)
mPaint.shader = LinearGradient(centerX, centerY, centerX + radius, centerY, START_COLOR, END_COLOR, Shader.TileMode.CLAMP)
for (i in (0 until mAreaCount)) {
canvas.drawArc(mTempRect, mStartAngle, mSweepAngle, true, mPaint)
canvas.rotate(mSingleAreaAngle, centerX, centerY)
}
canvas.restore()
}
override fun setAlpha(alpha: Int) {
}
override fun getOpacity(): Int {
return PixelFormat.OPAQUE
}
override fun setColorFilter(colorFilter: ColorFilter?) {
}
}