Advertisement
ipdan4ik

[lb11] DrawSurface.kt

Apr 24th, 2021
1,690
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 3.71 KB | None | 0 0
  1. package com.example.lb11
  2.  
  3. import android.content.Context
  4. import android.graphics.Canvas
  5. import android.graphics.Color
  6. import android.graphics.Paint
  7. import android.graphics.Rect
  8. import android.util.AttributeSet
  9. import android.view.SurfaceHolder
  10. import android.view.SurfaceView
  11. import kotlinx.coroutines.GlobalScope
  12. import kotlinx.coroutines.Job
  13. import kotlinx.coroutines.launch
  14. import kotlin.math.PI
  15. import kotlin.math.cos
  16. import kotlin.math.sin
  17.  
  18. class DrawSurface : SurfaceView, SurfaceHolder.Callback {
  19.  
  20.     private var cx = 0f
  21.  
  22.  
  23.     private var paint = Paint()
  24.     private lateinit var job: Job
  25.  
  26.     private var r: Float = 50F
  27.     private var h = 3*r
  28.     private var l = 7*r
  29.     private var l2 = 11*r
  30.     private var dx = 10f
  31.     private var dy: Float = h+r
  32.     private var df = 2f
  33.     private var f = 0f
  34.  
  35.     private var cy = 0f
  36.  
  37.     private var cx2 = 0f
  38.     private var cy2 = 0f
  39.     private var dx2 = 10f
  40.     private var dy2 = 10f
  41.     private var r2 = 5f
  42.  
  43.     constructor(context: Context) : this(context, null)
  44.     constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
  45.     constructor(context: Context, attrs: AttributeSet?,
  46.                 defStyleAttr: Int) : super(context, attrs, defStyleAttr) {}
  47.     override fun surfaceChanged(holder: SurfaceHolder, format: Int,
  48.                                 width: Int, height: Int) {
  49.     }
  50.     override fun surfaceCreated(holder: SurfaceHolder) {
  51.         paint.color = Color.WHITE
  52.         job = GlobalScope.launch {
  53.             var canvas: Canvas?
  54.             cy = height-dy*2f
  55.             while (true) {
  56.                 canvas = holder.lockCanvas(null)
  57.                 if (canvas != null) {
  58.                     canvas.drawColor(Color.argb(255, 255, 255, 255))
  59.                     paint.color = Color.GREEN
  60.                     canvas.drawRect(0f, cy+dy, width.toFloat(), height.toFloat(), paint)
  61.  
  62.                     paint.color = Color.BLACK
  63.                     canvas.drawCircle(2*r+cx, h+cy, r, paint)
  64.                     canvas.drawCircle(9*r+cx, h+cy, r, paint)
  65.  
  66.                     paint.color = Color.WHITE
  67.                     var dfx = ( r*cos(f* PI/180) ).toFloat()
  68.                     var dfy = ( r*sin(f* PI/180) ).toFloat()
  69.                     var dfx2 = ( r*cos((f+90)* PI/180) ).toFloat()
  70.                     var dfy2 = ( r*sin((f+90)* PI/180) ).toFloat()
  71.                     canvas.drawLine(2*r+cx-dfx, h+cy-dfy,2*r+cx+dfx, h+cy+dfy, paint)
  72.                     canvas.drawLine(9*r+cx-dfx, h+cy-dfy,9*r+cx+dfx, h+cy+dfy, paint)
  73.                     canvas.drawLine(2*r+cx-dfx2, h+cy-dfy2,2*r+cx+dfx2, h+cy+dfy2, paint)
  74.                     canvas.drawLine(9*r+cx-dfx2, h+cy-dfy2,9*r+cx+dfx2, h+cy+dfy2, paint)
  75.                     paint.color = Color.GRAY
  76.                     canvas.drawRect(cx, cy, l2+cx, h+cy, paint)
  77.                     paint.color = Color.WHITE
  78.                     canvas.drawCircle(cx+r2+cx2, cy+r2+cy2, r2, paint)
  79.  
  80.                     holder.unlockCanvasAndPost(canvas)
  81.                 }
  82.                 cx+=dx
  83.                 f+=df
  84.                 cx2+=dx2
  85.                 cy2+=dy2
  86.                 if (cx+l2 > width || cx < 0) {
  87.                     dx=-dx
  88.                     df = -df
  89.                 }
  90.                 if (f>360) { f -= 360 }
  91.                 if (f<-360) { f += 360 }
  92.                 if (cx+r2+cx2+r2 > l2+cx || cx+r2+cx2-r2 < cx) {
  93.                     dx2= -dx2
  94.                 }
  95.                 if (cy+r2+cy2+r2 > h+cy || cy+r2+cy2-r2 < cy) {
  96.                     dy2 = -dy2
  97.                 }
  98.  
  99.             }
  100.         }
  101.  
  102.     }
  103.  
  104.     override fun surfaceDestroyed(holder: SurfaceHolder) {
  105.         job.cancel()
  106.     }
  107.  
  108.     init {
  109.         holder.addCallback(this)
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement