AI2 Component  (Version nb184)
Image.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-2020 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 android.Manifest;
29 
30 import android.graphics.drawable.Drawable;
31 import android.util.Log;
32 import android.view.View;
33 import android.view.View.OnClickListener;
34 import android.widget.ImageView;
35 
36 import java.io.IOException;
37 
44 @DesignerComponent(version = YaVersion.IMAGE_COMPONENT_VERSION,
45  category = ComponentCategory.USERINTERFACE,
46  description = "Component for displaying images. The picture to display, " +
47  "and other aspects of the Image's appearance, can be specified in the " +
48  "Designer or in the Blocks Editor.")
49 @SimpleObject
50 @UsesPermissions(permissionNames = "android.permission.INTERNET," +
51  "android.permission.READ_EXTERNAL_STORAGE")
52 public final class Image extends AndroidViewComponent {
53 
54  private final ImageView view;
55 
56  private String picturePath = ""; // Picture property
57 
58  private double rotationAngle = 0.0;
59 
60  private int scalingMode = Component.SCALING_SCALE_PROPORTIONALLY;
61 
62  private boolean clickable = false;
63 
69  public Image(ComponentContainer container) {
70  super(container);
71 
72  view = new ImageView(container.$context()) {
73  @Override
74  public boolean verifyDrawable(Drawable dr) {
75  super.verifyDrawable(dr);
76  // TODO(user): multi-image animation
77  return true;
78  }
79  };
80  view.setFocusable(true);
81 
82  // Adds the component to its designated container
83  container.$add(this);
84  }
85 
86  @Override
87  public View getView() {
88  return view;
89  }
90 
91  @SimpleEvent(description = "An event that occurs when an image is clicked.")
92  public void Click() {
93  EventDispatcher.dispatchEvent(this, "Click");
94  }
95 
97  defaultValue = "False")
98  @SimpleProperty(description = "Specifies whether the image should be clickable or not.")
99  public void Clickable(boolean clickable) {
100  this.clickable = clickable;
101  view.setClickable(this.clickable);
102  if (this.clickable) {
103  view.setOnClickListener(new View.OnClickListener() {
104  @Override
105  public void onClick(View v) {
106  Click();
107  }
108  });
109  } else {
110  view.setOnClickListener(null);
111  }
112  }
113 
114  @SimpleProperty(description = "Specifies whether the image should be clickable or not.", category = PropertyCategory.APPEARANCE)
115  public boolean Clickable() {
116  return this.clickable;
117  }
118 
125  category = PropertyCategory.APPEARANCE)
126  public String Picture() {
127  return picturePath;
128  }
129 
140  defaultValue = "")
142  public void Picture(final String path) {
143  if (MediaUtil.isExternalFile(container.$context(), path)
144  && container.$form().isDeniedPermission(Manifest.permission.READ_EXTERNAL_STORAGE)) {
145  container.$form().askPermission(Manifest.permission.READ_EXTERNAL_STORAGE,
147  @Override
148  public void HandlePermissionResponse(String permission, boolean granted) {
149  if (granted) {
150  Picture(path);
151  } else {
152  container.$form().dispatchPermissionDeniedEvent(Image.this, "Picture", permission);
153  }
154  }
155  });
156  return;
157  }
158  picturePath = (path == null) ? "" : path;
159 
160  Drawable drawable;
161  try {
162  drawable = MediaUtil.getBitmapDrawable(container.$form(), picturePath);
163  } catch (IOException ioe) {
164  Log.e("Image", "Unable to load " + picturePath);
165  drawable = null;
166  }
167 
168  ViewUtil.setImage(view, drawable);
169  }
170 
177  defaultValue = "0.0")
179  public void RotationAngle(double rotationAngle) {
180  if (this.rotationAngle == rotationAngle) {
181  return; // Nothing to do...
182  // This also means that you can always set the
183  // the angle to 0.0 even on older Android devices
184  }
186  container.$form().dispatchErrorOccurredEvent(this, "RotationAngle",
188  return;
189  }
190  HoneycombUtil.viewSetRotate(view, rotationAngle);
191  this.rotationAngle = rotationAngle;
192  }
193 
194  @SimpleProperty(description = "The angle at which the image picture appears rotated. " +
195  "This rotation does not appear on the designer screen, only on the device.",
196  category = PropertyCategory.APPEARANCE)
197  public double RotationAngle() {
198  return rotationAngle;
199  }
200 
202  defaultValue = "False")
203  // @Deprecated -- We will deprecate this in a future release (jis: 2/12/2016)
204  @SimpleProperty(description = "Specifies whether the image should be resized to match the size of the ImageView.")
205  public void ScalePictureToFit(boolean scale) {
206  if (scale)
207  view.setScaleType(ImageView.ScaleType.FIT_XY);
208  else
209  view.setScaleType(ImageView.ScaleType.FIT_CENTER);
210  }
211 
221  @SimpleProperty(description = "This is a limited form of animation that can attach " +
222  "a small number of motion types to images. The allowable motions are " +
223  "ScrollRightSlow, ScrollRight, ScrollRightFast, ScrollLeftSlow, ScrollLeft, " +
224  "ScrollLeftFast, and Stop",
225  category = PropertyCategory.APPEARANCE)
226  // TODO(user): This should be changed from a property to an "animate" method, and have the choices
227  // placed in a dropdown. Aternatively the whole thing should be removed and we should do
228  // something that is more consistent with sprites.
229  public void Animation(String animation) {
230  AnimationUtil.ApplyAnimation(view, animation);
231  }
232 
233  @Deprecated
234 // @DesignerProperty(editorType = PropertyTypeConstants.PROPERTY_TYPE_SCALING,
235 // defaultValue = Component.SCALING_SCALE_PROPORTIONALLY + "")
236  @SimpleProperty(description = "This property determines how the picture " +
237  "scales according to the Height or Width of the Image. Scale " +
238  "proportionally (0) preserves the picture aspect ratio. Scale to fit " +
239  "(1) matches the Image area, even if the aspect ratio changes.")
240  public void Scaling(int mode) {
241  switch (mode) {
242  case 0:
243  view.setScaleType(ImageView.ScaleType.FIT_CENTER);
244  break;
245  case 1:
246  view.setScaleType(ImageView.ScaleType.FIT_XY);
247  break;
248  default:
249  throw new IllegalArgumentError("Illegal scaling mode: " + mode);
250  }
251  scalingMode = mode;
252  }
253 
258  public int Scaling() {
259  return scalingMode;
260  }
261 }
com.google.appinventor.components.runtime.EventDispatcher
Definition: EventDispatcher.java:22
com.google.appinventor.components.runtime.util.MediaUtil.getBitmapDrawable
static BitmapDrawable getBitmapDrawable(Form form, String mediaPath)
Definition: MediaUtil.java:419
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_FLOAT
static final String PROPERTY_TYPE_FLOAT
Definition: PropertyTypeConstants.java:76
com.google.appinventor.components.annotations.SimpleFunction
Definition: SimpleFunction.java:23
com.google.appinventor.components.runtime.util.MediaUtil.isExternalFile
static boolean isExternalFile(String mediaPath)
Definition: MediaUtil.java:218
com.google.appinventor.components.runtime.util.ErrorMessages
Definition: ErrorMessages.java:17
com.google.appinventor.components.runtime.util
-*- mode: java; c-basic-offset: 2; -*-
Definition: AccountChooser.java:7
com.google.appinventor.components.runtime.util.ErrorMessages.ERROR_IMAGE_CANNOT_ROTATE
static final int ERROR_IMAGE_CANNOT_ROTATE
Definition: ErrorMessages.java:210
com.google.appinventor.components.common.YaVersion
Definition: YaVersion.java:14
com.google.appinventor.components.annotations.DesignerProperty
Definition: DesignerProperty.java:25
com.google.appinventor.components.runtime.util.HoneycombUtil.viewSetRotate
static void viewSetRotate(View view, double rotationAngle)
Definition: HoneycombUtil.java:37
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.util.AnimationUtil
Definition: AnimationUtil.java:24
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.SimpleEvent
Definition: SimpleEvent.java:20
com.google.appinventor.components.runtime.errors.IllegalArgumentError
Definition: IllegalArgumentError.java:17
com.google.appinventor.components.runtime.Component.SCALING_SCALE_PROPORTIONALLY
static final int SCALING_SCALE_PROPORTIONALLY
Definition: Component.java:100
com.google.appinventor.components.runtime.ComponentContainer.$add
void $add(AndroidViewComponent component)
com.google.appinventor.components.runtime.util.AnimationUtil.ApplyAnimation
static void ApplyAnimation(View view, String animation)
Definition: AnimationUtil.java:54
com.google.appinventor.components.annotations.UsesPermissions
Definition: UsesPermissions.java:21
com.google.appinventor.components.runtime.PermissionResultHandler
Definition: PermissionResultHandler.java:15
com.google.appinventor.components.runtime.Image.RotationAngle
void RotationAngle(double rotationAngle)
Definition: Image.java:179
com.google.appinventor.components.runtime.Image.Scaling
int Scaling()
Definition: Image.java:258
com.google.appinventor.components.runtime.EventDispatcher.dispatchEvent
static boolean dispatchEvent(Component component, String eventName, Object...args)
Definition: EventDispatcher.java:188
com.google.appinventor.components.runtime.util.SdkLevel.LEVEL_HONEYCOMB
static final int LEVEL_HONEYCOMB
Definition: SdkLevel.java:28
com.google.appinventor.components.runtime.util.ViewUtil.setImage
static void setImage(ImageView view, Drawable drawable)
Definition: ViewUtil.java:192
com.google.appinventor.components.runtime.util.SdkLevel
Definition: SdkLevel.java:19
com.google.appinventor.components.annotations.SimpleProperty
Definition: SimpleProperty.java:23
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.util.SdkLevel.getLevel
static int getLevel()
Definition: SdkLevel.java:45
com.google.appinventor.components.runtime
Copyright 2009-2011 Google, All Rights reserved.
Definition: AccelerometerSensor.java:8
com.google.appinventor.components.runtime.Component
Definition: Component.java:17
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.Image.Image
Image(ComponentContainer container)
Definition: Image.java:69
com.google
com
com.google.appinventor.components.runtime.errors
Definition: ArrayIndexOutOfBoundsError.java:7
com.google.appinventor.components.runtime.ComponentContainer.$context
Activity $context()
com.google.appinventor.components.runtime.AndroidViewComponent
Definition: AndroidViewComponent.java:27
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_ASSET
static final String PROPERTY_TYPE_ASSET
Definition: PropertyTypeConstants.java:22
com.google.appinventor.components.runtime.Image.Picture
void Picture(final String path)
Definition: Image.java:142
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.components.runtime.Image
Definition: Image.java:52
com.google.appinventor
com.google.appinventor.components.runtime.util.HoneycombUtil
Definition: HoneycombUtil.java:18
com.google.appinventor.components.runtime.Image.getView
View getView()
Definition: Image.java:87
com.google.appinventor.components.runtime.util.ViewUtil
Definition: ViewUtil.java:22