牙叔教程 简单易懂
"ui"; engines.all().map((ScriptEngine) => { if (engines.myEngine().toString() !== ScriptEngine.toString()) { ScriptEngine.forceStop(); } }); importClass("android.animation.ValueAnimator"); importClass("android.content.Context"); importClass("android.graphics.Matrix"); importClass("android.graphics.PorterDuffXfermode"); importClass("android.graphics.PorterDuff"); importClass("android.graphics.Color"); importClass("android.util.AttributeSet"); importClass("android.view.View"); importClass("android.view.animation.RotateAnimation"); ui.layout( <vertical> <canvas id="board"></canvas> </vertical> ); // 设置画笔参数 let paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setStyle(Paint.Style.FILL); color = colors.parseColor("#ffffff"); paint.setColor(color); // 设置动画参数 let anim = ValueAnimator.ofFloat(0, -360); anim.repeatCount = android.view.animation.Animation.INFINITE; anim.setDuration(1000); anim.setInterpolator(null); anim.start(); let camera = android.graphics.Camera(); let rotateMatrix = Matrix(); let xfermode = PorterDuffXfermode(PorterDuff.Mode.DST_OUT); /* -------------------------------------------------------------------------- */ ui.post(function () { let width = ui.board.getWidth(); let height = ui.board.getHeight(); // log(width.toFloat()); ui.board.on("draw", function (canvas) { canvas.drawColor(Color.BLACK); color = "#ff0000"; drawSword(canvas, 35, -45, 0, width, height, color); color = "#00ff00"; drawSword(canvas, 50, 10, 120, width, height, color); color = "#0000ff"; drawSword(canvas, 35, 55, 240, width, height, color); }); }); function drawSword(canvas, rotateX, rotateY, startValue, width, height, color) { let colorNum = colors.parseColor(color); paint.setColor(colorNum); let layerId = canvas.saveLayer(0, 0, width, height, null, android.graphics.Canvas.ALL_SAVE_FLAG); rotateMatrix.reset(); camera.save(); /* ----------------相机旋转---------------------------------------------------------- */ camera.rotateX(rotateX); camera.rotateY(rotateY); camera.rotateZ(anim.animatedValue + startValue); camera.getMatrix(rotateMatrix); camera.restore(); /* -------------------------------------------------------------------------- */ let halfW = width / 2; let halfH = height / 2; let radius = Math.min(width, height) / 4; rotateMatrix.preTranslate(-halfW, -halfH); rotateMatrix.postTranslate(halfW, halfH); canvas.concat(rotateMatrix); canvas.drawCircle(halfW, halfH, radius, paint); paint.xfermode = xfermode; canvas.drawCircle(halfW, halfH - 0.05 * radius, radius * 1.01, paint); canvas.restoreToCount(layerId); paint.xfermode = null; }
参考
名人名言
思路是最重要的, 其他的百度, bing, stackoverflow, 安卓文档, autojs文档, 最后才是群里问问
--- 牙叔教程
声明
部分内容来自网络
本教程仅用于学习, 禁止用于其他用途