AI2 Component  (Version nb184)
ImageSprite.java
Go to the documentation of this file.
1 // -*- mode: java; c-basic-offset: 2; -*-
2 // Copyright 2009-2011 Google, All Rights reserved
3 // Copyright 2011-2019 MIT, All rights reserved
4 // Released under the Apache License, Version 2.0
5 // http://www.apache.org/licenses/LICENSE-2.0
6 
7 package com.google.appinventor.components.runtime;
8 
9 import java.io.IOException;
10 
11 import android.graphics.Bitmap;
12 import android.graphics.drawable.BitmapDrawable;
13 import android.util.Log;
14 
26 
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)
64 @SimpleObject
65 @UsesPermissions(permissionNames = "android.permission.INTERNET")
66 public class ImageSprite extends Sprite {
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 = ""; // Picture property
72  private boolean rotates;
73 
74 
80  public ImageSprite(ComponentContainer container) {
81  super(container);
82  form = container.$form();
83  rotates = true;
84  }
85 
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);
98  // If the sprite doesn't rotate, just draw the drawable
99  // within the bounds of the sprite rectangle
100  if (!rotates) {
101  drawable.draw(canvas);
102  } else {
103  // if the sprite does rotate, draw the sprite on the canvas
104  // that has been rotated in the opposite direction
105  // Still within those same image bounds.
106  canvas.save();
107  // rotate the canvas for drawing. This pivot point of the
108  // rotation will be the center of the sprite
109  canvas.rotate((float) (- Heading()), xinit + w/2, yinit + h/2);
110  drawable.draw(canvas);
111  canvas.restore();
112  }
113  }
114  }
115 
122  description = "The picture that determines the ImageSprite's appearance.",
123  category = PropertyCategory.APPEARANCE)
124  public String Picture() {
125  return picturePath;
126  }
127 
138  defaultValue = "")
140  public void Picture(String path) {
141  picturePath = (path == null) ? "" : path;
142  try {
143  drawable = MediaUtil.getBitmapDrawable(form, picturePath);
144  } catch (IOException ioe) {
145  Log.e("ImageSprite", "Unable to load " + picturePath);
146  drawable = null;
147  }
148  // note: drawable can be null!
149  registerChange();
150  }
151 
152  // The actual width/height of an ImageSprite whose Width/Height property is set to Automatic or
153  // Fill Parent will be the width/height of the image.
154 
155  @Override
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) {
159  // Drawable.getIntrinsicWidth/Height gives weird values, but Bitmap.getWidth/Height works.
160  return drawable == null ? 0 : (int)(drawable.getBitmap().getHeight() / form.deviceDensity());
161  }
162  return heightHint;
163  }
164 
169  @Override
171  public void Height(int height) {
172  heightHint = height;
173  registerChange();
174  }
175 
176  @Override
177  public void HeightPercent(int pCent) {
178  // Ignore
179  }
180 
181  @Override
182  @SimpleProperty(description = "The width of the ImageSprite in pixels.")
183  public int Width() {
184  if (widthHint == LENGTH_PREFERRED || widthHint == LENGTH_FILL_PARENT || widthHint <= LENGTH_PERCENT_TAG) {
185  // Drawable.getIntrinsicWidth/Height gives weird values, but Bitmap.getWidth/Height works.
186  return drawable == null ? 0 : (int)(drawable.getBitmap().getWidth() / form.deviceDensity());
187  }
188  return widthHint;
189  }
190 
195  @Override
197  public void Width(int width) {
198  widthHint = width;
199  registerChange();
200  }
201 
202  @Override
203  public void WidthPercent(int pCent) {
204  // Ignore
205  }
206 
214  description = "Whether the image should rotate to match the ImageSprite's heading. " +
215  "The sprite rotates around its centerpoint.",
216  category = PropertyCategory.BEHAVIOR)
217  public boolean Rotates() {
218  return rotates;
219  }
220 
229  defaultValue = "True")
231  public void Rotates(boolean rotates) {
232  this.rotates = rotates;
233  registerChange();
234  }
235 
236  // We need to override methods defined in the superclass to generate appropriate documentation.
237 
239  description = "The horizontal coordinate of the left edge of the ImageSprite, " +
240  "increasing as the ImageSprite moves right.")
241  @Override
242  public double X() {
243  return super.X();
244  }
245 
247  description = "The vertical coordinate of the top edge of the ImageSprite, " +
248  "increasing as the ImageSprite moves down.")
249  @Override
250  public double Y() {
251  return super.Y();
252  }
253 
260  description = "Moves the ImageSprite so that its left top corner is at " +
261  "the specified x and y coordinates.")
262  @Override
263  public void MoveTo(double x, double y) {
264  super.MoveTo(x, y);
265  }
266 }
com.google.appinventor.components.runtime.util.MediaUtil.getBitmapDrawable
static BitmapDrawable getBitmapDrawable(Form form, String mediaPath)
Definition: MediaUtil.java:419
com.google.appinventor.components.runtime.ImageSprite
Definition: ImageSprite.java:66
com.google.appinventor.components.runtime.ImageSprite.Rotates
void Rotates(boolean rotates)
Definition: ImageSprite.java:231
com.google.appinventor.components.annotations.SimpleFunction
Definition: SimpleFunction.java:23
com.google.appinventor.components.runtime.ImageSprite.HeightPercent
void HeightPercent(int pCent)
Definition: ImageSprite.java:177
com.google.appinventor.components.runtime.util
-*- mode: java; c-basic-offset: 2; -*-
Definition: AccountChooser.java:7
com.google.appinventor.components.common.YaVersion
Definition: YaVersion.java:14
com.google.appinventor.components.runtime.ImageSprite.Y
double Y()
Definition: ImageSprite.java:250
com.google.appinventor.components.annotations.DesignerProperty
Definition: DesignerProperty.java:25
com.google.appinventor.components
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_BOOLEAN
static final String PROPERTY_TYPE_BOOLEAN
Definition: PropertyTypeConstants.java:35
com.google.appinventor.components.runtime.Sprite
Definition: Sprite.java:37
com.google.appinventor.components.runtime.ImageSprite.onDraw
void onDraw(android.graphics.Canvas canvas)
Definition: ImageSprite.java:91
com.google.appinventor.components.runtime.util.MediaUtil
Definition: MediaUtil.java:53
com.google.appinventor.components.annotations.DesignerComponent
Definition: DesignerComponent.java:22
com.google.appinventor.components.annotations.PropertyCategory.BEHAVIOR
BEHAVIOR
Definition: PropertyCategory.java:15
com.google.appinventor.components.annotations.UsesPermissions
Definition: UsesPermissions.java:21
com.google.appinventor.components.runtime.ImageSprite.ImageSprite
ImageSprite(ComponentContainer container)
Definition: ImageSprite.java:80
com.google.appinventor.components.annotations.SimpleProperty
Definition: SimpleProperty.java:23
com.google.appinventor.components.runtime.ImageSprite.WidthPercent
void WidthPercent(int pCent)
Definition: ImageSprite.java:203
com.google.appinventor.components.annotations.PropertyCategory
Definition: PropertyCategory.java:13
com.google.appinventor.components.runtime.ComponentContainer
Definition: ComponentContainer.java:16
com.google.appinventor.components.runtime.ImageSprite.MoveTo
void MoveTo(double x, double y)
Definition: ImageSprite.java:263
com.google.appinventor.components.runtime.ImageSprite.Width
void Width(int width)
Definition: ImageSprite.java:197
com.google.appinventor.components.runtime
Copyright 2009-2011 Google, All Rights reserved.
Definition: AccelerometerSensor.java:8
com.google.appinventor.components.runtime.ImageSprite.Picture
void Picture(String path)
Definition: ImageSprite.java:140
com.google.appinventor.components.common
Definition: ComponentCategory.java:7
com.google.appinventor.components.common.ComponentCategory
Definition: ComponentCategory.java:48
com.google.appinventor.components.annotations.SimpleObject
Definition: SimpleObject.java:23
com.google.appinventor.components.runtime.ImageSprite.X
double X()
Definition: ImageSprite.java:242
com.google
com
com.google.appinventor.components.runtime.ComponentContainer.$form
Form $form()
com.google.appinventor.components.runtime.ImageSprite.Height
void Height(int height)
Definition: ImageSprite.java:171
com.google.appinventor.components.runtime.Form
Definition: Form.java:126
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_ASSET
static final String PROPERTY_TYPE_ASSET
Definition: PropertyTypeConstants.java:22
com.google.appinventor.components.annotations.PropertyCategory.APPEARANCE
APPEARANCE
Definition: PropertyCategory.java:16
com.google.appinventor.components.common.PropertyTypeConstants
Definition: PropertyTypeConstants.java:14
com.google.appinventor.components.annotations
com.google.appinventor