Last active
January 9, 2018 15:34
-
-
Save yeelone/93572c9f2af1f7f3441a5080515f25eb to your computer and use it in GitHub Desktop.
surfaceview闪屏代码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context | |
import android.graphics.* | |
import android.util.AttributeSet | |
import android.util.Log | |
import android.view.MotionEvent | |
import android.view.SurfaceHolder | |
import android.view.SurfaceView | |
import com.elone.android.ecgtodo.extensions.ctx | |
import org.jetbrains.anko.startActivity | |
import android.graphics.Bitmap | |
import java.util.* | |
class MySurfaceView : SurfaceView, SurfaceHolder.Callback { | |
private var mWidth = 0f | |
private var mHeight = 100f | |
constructor(context: Context) : super(context) | |
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) | |
private val lock = Object() | |
private var canvas = Canvas() | |
private var canRun = false | |
private var threadQuit = false | |
private var firstFrame = true | |
private var frame = 0 | |
private var thread = Thread { | |
while (!threadQuit) { | |
if (!canRun) { | |
synchronized(lock) { | |
try { | |
lock.wait() | |
} catch (e: Exception) { | |
} | |
} | |
} | |
try { | |
test() | |
} catch (e: Exception) { | |
e.printStackTrace() | |
} | |
} | |
} | |
override fun surfaceCreated(surfaceHolder: SurfaceHolder) { | |
val frame = holder.surfaceFrame | |
mWidth = (frame.right - frame.left).toFloat() | |
mHeight = (frame.bottom - frame.top).toFloat() | |
startThread() | |
} | |
override fun surfaceChanged(surfaceHolder: SurfaceHolder, i: Int, i1: Int, i2: Int) { | |
canRun = true | |
startThread() | |
} | |
override fun surfaceDestroyed(surfaceHolder: SurfaceHolder) { | |
canRun = false | |
} | |
override fun onTouchEvent(event: MotionEvent): Boolean { | |
return true | |
} | |
init { | |
holder.addCallback(this) | |
setWillNotDraw(false) | |
setZOrderOnTop(true) | |
holder.setFormat(PixelFormat.TRANSLUCENT) | |
thread.start() | |
} | |
fun test(){ | |
var paint = Paint() | |
var before = System.currentTimeMillis() | |
try { | |
canvas = holder.lockCanvas()//获取canvas | |
var bak = Bitmap.createBitmap(canvas.width,canvas.height,Bitmap.Config.ARGB_8888) | |
val bakCanvas = Canvas(bak) | |
// canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR) | |
if (firstFrame) { | |
paint.apply { | |
isAntiAlias = true | |
color = Color.parseColor("#3498db") | |
style = Paint.Style.FILL | |
strokeWidth =5f | |
} | |
bakCanvas.drawRect(Rect(100,100,120,120),paint) | |
}else{ | |
paint.apply { | |
isAntiAlias = true | |
color = Color.parseColor("#e74c3c") | |
style = Paint.Style.FILL | |
strokeWidth =5f | |
} | |
bakCanvas.drawRect(Rect(140,140,160,160),paint) | |
} | |
canvas.drawBitmap(bak,0f,0f,null) | |
before = System.currentTimeMillis() - before | |
if (before < 17) { | |
Thread.sleep(1000) //休眠 | |
} | |
} catch (e: InterruptedException) { | |
Log.i(TAG,e.toString()) | |
e.printStackTrace() | |
} finally { | |
firstFrame = !firstFrame | |
frame++ | |
Log.i(TAG,"frame:" + frame.toString() ) | |
holder.unlockCanvasAndPost(canvas) //解锁canvas,提交画好的图像 | |
} | |
} | |
fun startThread(){ | |
threadQuit = false | |
canRun = true | |
try { | |
// 如果没有执行wait的话,这里notify会抛异常 | |
synchronized(lock) { | |
lock.notify() | |
} | |
} catch (e: Exception) { | |
e.printStackTrace() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment