User Tools

Site Tools


script_paint_free_hand

====== About the script ====== * Purpose : demonstrate the use of the Canvas APIs together with direct handling of touch events * Author : Pierre Hébert * Link: [[http://www.pierrox.net/android/applications/lightning_launcher/script/reference/net/pierrox/lightning_launcher/script/api/Image.html|Images and canvas APIs]], [[http://www.pierrox.net/android/applications/lightning_launcher/help/topic.php?id=160|Use of touch events]] * Requires LL v10.4b3 ====== How to use the script ====== Copy paste the code below in a new script, check "menu item". Come back on a desktop, pick some random shortcut, enlarge it so that there is room to draw, and set its touch event to the newly created script (customize / more / tab +) ====== Script code ====== <sxh javascript;> var x = event.getX(); var y = event.getY(); switch(event.getAction()) { case MotionEvent.ACTION_DOWN: prev_x = x; prev_y = y; hue = 0; paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setStrokeWidth(5); paint.setStrokeJoin(Paint.Join.ROUND); image = item.getBoxBackground("n"); if(image == null) { image = LL.createImage(512, 512); item.setBoxBackground(image, "n"); } canvas = image.draw(); canvas.scale( image.getWidth() / item.getWidth(), image.getHeight() / item.getHeight()); canvas.drawARGB(255,255,255,255); break; case MotionEvent.ACTION_MOVE: hue += 10; hue = hue % 360; var c = Color.HSVToColor([hue, 1, 1]); paint.setColor(c); canvas.drawLine(prev_x, prev_y, x, y, paint); prev_x = x; prev_y = y; image.update(); break; case MotionEvent.ACTION_UP: image.save(); paint = null; canvas = null; break; } return true; </sxh>

script_paint_free_hand.txt · Last modified: 2014/07/21 09:29 by pierrox