7 package com.google.appinventor.components.runtime;
9 import java.io.IOException;
11 import android.graphics.Bitmap;
12 import android.graphics.drawable.BitmapDrawable;
13 import android.util.Log;
43 @DesignerComponent(version = YaVersion.IMAGESPRITE_COMPONENT_VERSION,
44 description =
"<p>A 'sprite' that can be placed on a " +
45 "<code>Canvas</code>, where it can react to touches and drags, " +
46 "interact with other sprites (<code>Ball</code>s and other " +
47 "<code>ImageSprite</code>s) and the edge of the Canvas, and move " +
48 "according to its property values. Its appearance is that of the " +
49 "image specified in its <code>Picture</code> property (unless its " +
50 "<code>Visible</code> property is <code>False</code>).</p> " +
51 "<p>To have an <code>ImageSprite</code> move 10 pixels to the left " +
52 "every 1000 milliseconds (one second), for example, " +
53 "you would set the <code>Speed</code> property to 10 [pixels], the " +
54 "<code>Interval</code> property to 1000 [milliseconds], the " +
55 "<code>Heading</code> property to 180 [degrees], and the " +
56 "<code>Enabled</code> property to <code>True</code>. A sprite whose " +
57 "<code>Rotates</code> property is <code>True</code> will rotate its " +
58 "image as the sprite's <code>Heading</code> changes. Checking for collisions " +
59 "with a rotated sprite currently checks the sprite's unrotated position " +
60 "so that collision checking will be inaccurate for tall narrow or short " +
61 "wide sprites that are rotated. Any of the sprite properties " +
62 "can be changed at any time under program control.</p> ",
63 category = ComponentCategory.ANIMATION)
65 @UsesPermissions(permissionNames =
"android.permission.INTERNET")
67 private final Form form;
68 private BitmapDrawable drawable;
69 private int widthHint = LENGTH_PREFERRED;
70 private int heightHint = LENGTH_PREFERRED;
71 private String picturePath =
"";
72 private boolean rotates;
82 form = container.
$form();
91 public void onDraw(android.graphics.Canvas canvas) {
92 if (drawable !=
null && visible) {
93 int xinit = (int) (Math.round(xLeft) * form.deviceDensity());
94 int yinit = (int) (Math.round(yTop) * form.deviceDensity());
95 int w = (int)(Width() * form.deviceDensity());
96 int h = (int)(Height() * form.deviceDensity());
97 drawable.setBounds(xinit, yinit, xinit + w, yinit + h);
101 drawable.draw(canvas);
109 canvas.rotate((
float) (- Heading()), xinit + w/2, yinit + h/2);
110 drawable.draw(canvas);
122 description =
"The picture that determines the ImageSprite's appearance.",
124 public String Picture() {
141 picturePath = (path ==
null) ?
"" : path;
144 }
catch (IOException ioe) {
145 Log.e(
"ImageSprite",
"Unable to load " + picturePath);
156 @
SimpleProperty(description =
"The height of the ImageSprite in pixels.")
157 public
int Height() {
158 if (heightHint == LENGTH_PREFERRED || heightHint == LENGTH_FILL_PARENT || heightHint <= LENGTH_PERCENT_TAG) {
160 return drawable ==
null ? 0 : (int)(drawable.getBitmap().getHeight() / form.deviceDensity());
182 @
SimpleProperty(description =
"The width of the ImageSprite in pixels.")
184 if (widthHint == LENGTH_PREFERRED || widthHint == LENGTH_FILL_PARENT || widthHint <= LENGTH_PERCENT_TAG) {
186 return drawable ==
null ? 0 : (int)(drawable.getBitmap().getWidth() / form.deviceDensity());
214 description =
"Whether the image should rotate to match the ImageSprite's heading. " +
215 "The sprite rotates around its centerpoint.",
217 public
boolean Rotates() {
229 defaultValue =
"True")
232 this.rotates = rotates;
239 description =
"The horizontal coordinate of the left edge of the ImageSprite, " +
240 "increasing as the ImageSprite moves right.")
247 description =
"The vertical coordinate of the top edge of the ImageSprite, " +
248 "increasing as the ImageSprite moves down.")
260 description =
"Moves the ImageSprite so that its left top corner is at " +
261 "the specified x and y coordinates.")