AI2 Component  (Version nb184)
ZoomControlView.java
Go to the documentation of this file.
1 // -*- mode: java; c-basic-offset: 2; -*-
2 // Copyright 2018 MIT, All rights reserved
3 // Released under the Apache License, Version 2.0
4 // http://www.apache.org/licenses/LICENSE-2.0
5 
6 package com.google.appinventor.components.runtime.view;
7 
8 import android.content.res.ColorStateList;
9 import android.graphics.Color;
10 import android.graphics.drawable.Drawable;
11 import android.graphics.drawable.ShapeDrawable;
12 import android.graphics.drawable.shapes.RoundRectShape;
13 import android.view.Gravity;
14 import android.view.View;
15 import android.widget.Button;
16 import android.widget.LinearLayout;
18 import org.osmdroid.views.MapView;
19 
20 public class ZoomControlView extends LinearLayout {
21 
22  private final MapView parent;
23  private final Button zoomIn;
24  private final Button zoomOut;
25 
26  private float density;
27 
28  public ZoomControlView(MapView parent) {
29  super(parent.getContext());
30 
31  density = parent.getContext().getResources().getDisplayMetrics().density;
32 
33  this.parent = parent;
34  this.setOrientation(LinearLayout.VERTICAL);
35  zoomIn = new Button(parent.getContext());
36  zoomOut = new Button(parent.getContext());
37  initButton(zoomIn, "+");
38  initButton(zoomOut, "−");
39  zoomIn.setOnClickListener(new OnClickListener() {
40  @Override
41  public void onClick(View view) {
42  ZoomControlView.this.parent.getController().zoomIn();
43  }
44  });
45  zoomOut.setOnClickListener(new OnClickListener() {
46  @Override
47  public void onClick(View view) {
48  ZoomControlView.this.parent.getController().zoomOut();
49  }
50  });
51 
52  ViewUtil.setBackgroundDrawable(zoomIn, getZoomInDrawable(density));
53  ViewUtil.setBackgroundDrawable(zoomOut, getZoomOutDrawable(density));
54 
55  int[][] states = new int[][] {
56  new int[] {-android.R.attr.state_enabled },
57  new int[] { android.R.attr.state_enabled }
58  };
59  int[] colors = new int[] {
60  Color.LTGRAY,
61  Color.BLACK
62  };
63  zoomIn.setTextColor(new ColorStateList(states, colors));
64  zoomOut.setTextColor(new ColorStateList(states, colors));
65 
66  addView(zoomIn);
67  addView(zoomOut);
68 
69  this.setPadding((int)(12 * density), (int)(12 * density), 0, 0);
70  updateButtons();
71  }
72 
76  @SuppressWarnings("WeakerAccess")
77  public final void updateButtons() {
78  zoomIn.setEnabled(parent.canZoomIn());
79  zoomOut.setEnabled(parent.canZoomOut());
80  }
81 
82  private void initButton(Button button, String text) {
83  button.setText(text);
84  button.setTextSize(22);
85  button.setPadding(0, 0, 0, 0);
86  button.setWidth((int)(30 * density));
87  button.setHeight((int)(30 * density));
88  button.setSingleLine();
89  button.setGravity(Gravity.CENTER);
90  }
91 
92  private static Drawable getZoomInDrawable(float density) {
93  final int R = (int)(4 * density);
94  ShapeDrawable drawable = new ShapeDrawable(new RoundRectShape(new float[] { R, R, R, R, 0, 0, 0, 0 }, null, null));
95  drawable.getPaint().setColor(Color.WHITE);
96  return drawable;
97  }
98 
99  private static Drawable getZoomOutDrawable(float density) {
100  final int R = (int)(4 * density);
101  ShapeDrawable drawable = new ShapeDrawable(new RoundRectShape(new float[] { 0, 0, 0, 0, R, R, R, R }, null, null));
102  drawable.getPaint().setColor(Color.WHITE);
103  return drawable;
104  }
105 }
com.google.appinventor.components.runtime.util
-*- mode: java; c-basic-offset: 2; -*-
Definition: AccountChooser.java:7
com.google.appinventor.components.runtime.view.ZoomControlView.ZoomControlView
ZoomControlView(MapView parent)
Definition: ZoomControlView.java:28
com.google.appinventor.components
com.google.appinventor.components.runtime.util.ViewUtil.setBackgroundDrawable
static void setBackgroundDrawable(View view, Drawable drawable)
Definition: ViewUtil.java:200
com.google.appinventor.components.runtime
Copyright 2009-2011 Google, All Rights reserved.
Definition: AccelerometerSensor.java:8
com.google.appinventor.components.runtime.view.ZoomControlView
Definition: ZoomControlView.java:20
com.google
com
com.google.appinventor.components.runtime.view.ZoomControlView.updateButtons
final void updateButtons()
Definition: ZoomControlView.java:77
com.google.appinventor
com.google.appinventor.components.runtime.util.ViewUtil
Definition: ViewUtil.java:22