Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.lb11
- import android.content.Context
- import android.graphics.Canvas
- import android.graphics.Color
- import android.graphics.Paint
- import android.graphics.Rect
- import android.util.AttributeSet
- import android.view.SurfaceHolder
- import android.view.SurfaceView
- import kotlinx.coroutines.GlobalScope
- import kotlinx.coroutines.Job
- import kotlinx.coroutines.launch
- import kotlin.math.PI
- import kotlin.math.cos
- import kotlin.math.sin
- class DrawSurface : SurfaceView, SurfaceHolder.Callback {
- private var cx = 0f
- private var paint = Paint()
- private lateinit var job: Job
- private var r: Float = 50F
- private var h = 3*r
- private var l = 7*r
- private var l2 = 11*r
- private var dx = 10f
- private var dy: Float = h+r
- private var df = 2f
- private var f = 0f
- private var cy = 0f
- private var cx2 = 0f
- private var cy2 = 0f
- private var dx2 = 10f
- private var dy2 = 10f
- private var r2 = 5f
- constructor(context: Context) : this(context, null)
- constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
- constructor(context: Context, attrs: AttributeSet?,
- defStyleAttr: Int) : super(context, attrs, defStyleAttr) {}
- override fun surfaceChanged(holder: SurfaceHolder, format: Int,
- width: Int, height: Int) {
- }
- override fun surfaceCreated(holder: SurfaceHolder) {
- paint.color = Color.WHITE
- job = GlobalScope.launch {
- var canvas: Canvas?
- cy = height-dy*2f
- while (true) {
- canvas = holder.lockCanvas(null)
- if (canvas != null) {
- canvas.drawColor(Color.argb(255, 255, 255, 255))
- paint.color = Color.GREEN
- canvas.drawRect(0f, cy+dy, width.toFloat(), height.toFloat(), paint)
- paint.color = Color.BLACK
- canvas.drawCircle(2*r+cx, h+cy, r, paint)
- canvas.drawCircle(9*r+cx, h+cy, r, paint)
- paint.color = Color.WHITE
- var dfx = ( r*cos(f* PI/180) ).toFloat()
- var dfy = ( r*sin(f* PI/180) ).toFloat()
- var dfx2 = ( r*cos((f+90)* PI/180) ).toFloat()
- var dfy2 = ( r*sin((f+90)* PI/180) ).toFloat()
- canvas.drawLine(2*r+cx-dfx, h+cy-dfy,2*r+cx+dfx, h+cy+dfy, paint)
- canvas.drawLine(9*r+cx-dfx, h+cy-dfy,9*r+cx+dfx, h+cy+dfy, paint)
- canvas.drawLine(2*r+cx-dfx2, h+cy-dfy2,2*r+cx+dfx2, h+cy+dfy2, paint)
- canvas.drawLine(9*r+cx-dfx2, h+cy-dfy2,9*r+cx+dfx2, h+cy+dfy2, paint)
- paint.color = Color.GRAY
- canvas.drawRect(cx, cy, l2+cx, h+cy, paint)
- paint.color = Color.WHITE
- canvas.drawCircle(cx+r2+cx2, cy+r2+cy2, r2, paint)
- holder.unlockCanvasAndPost(canvas)
- }
- cx+=dx
- f+=df
- cx2+=dx2
- cy2+=dy2
- if (cx+l2 > width || cx < 0) {
- dx=-dx
- df = -df
- }
- if (f>360) { f -= 360 }
- if (f<-360) { f += 360 }
- if (cx+r2+cx2+r2 > l2+cx || cx+r2+cx2-r2 < cx) {
- dx2= -dx2
- }
- if (cy+r2+cy2+r2 > h+cy || cy+r2+cy2-r2 < cy) {
- dy2 = -dy2
- }
- }
- }
- }
- override fun surfaceDestroyed(holder: SurfaceHolder) {
- job.cancel()
- }
- init {
- holder.addCallback(this)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement