48 this(context, onZoomIn, onZoomOut,
null);
57 this.onZoomInListener = onZoomIn;
58 this.onZoomOutListener = onZoomOut;
63 private void setupUI() {
68 Button zoomInButton =
new Button(getContext(),
null, android.R.attr.borderlessButtonStyle);
70 zoomInButton.setCompoundDrawablesWithIntrinsicBounds(
null, c.
getZoomInIcon(),
null,
null);
71 zoomInButton.setText(
"");
73 zoomInButton.setText(
"+");
75 zoomInButton.setTextSize(WidgetStyles.BUTTON_FONT_SIZE);
76 zoomInButton.setTextColor(textColor);
77 zoomInButton.setBackground(createRoundedBackground(
true, bgColor));
78 zoomInButton.setPadding(0, 0, 0, 0);
79 zoomInButton.setStateListAnimator(
null);
81 Button zoomOutButton =
new Button(getContext(),
null, android.R.attr.borderlessButtonStyle);
83 zoomOutButton.setCompoundDrawablesWithIntrinsicBounds(
null, c.
getZoomOutIcon(),
null,
null);
84 zoomOutButton.setText(
"");
86 zoomOutButton.setText(
"−");
88 zoomOutButton.setTextSize(WidgetStyles.BUTTON_FONT_SIZE);
89 zoomOutButton.setTextColor(textColor);
90 zoomOutButton.setBackground(createRoundedBackground(
false, bgColor));
91 zoomOutButton.setPadding(0, 0, 0, 0);
92 zoomOutButton.setStateListAnimator(
null);
94 zoomInButton.setOnClickListener(v -> {
95 if (onZoomInListener !=
null) {
99 zoomOutButton.setOnClickListener(v -> {
100 if (onZoomOutListener !=
null) {
101 onZoomOutListener.onZoomOut();
105 LinearLayout stack =
new LinearLayout(getContext());
106 stack.setOrientation(LinearLayout.VERTICAL);
107 stack.setBackgroundColor(Color.TRANSPARENT);
109 stack.setElevation(dpToPx((
int) WidgetStyles.SHADOW_ELEVATION));
110 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) {
111 stack.setOutlineAmbientShadowColor(WidgetStyles.SHADOW_COLOR);
112 stack.setOutlineSpotShadowColor(WidgetStyles.SHADOW_COLOR);
115 LinearLayout.LayoutParams btnLp =
new LinearLayout.LayoutParams(
116 ViewGroup.LayoutParams.MATCH_PARENT, 0, 1f);
117 stack.addView(zoomInButton, btnLp);
118 stack.addView(zoomOutButton, btnLp);
120 FrameLayout.LayoutParams stackLp =
new FrameLayout.LayoutParams(
121 ViewGroup.LayoutParams.MATCH_PARENT,
122 ViewGroup.LayoutParams.MATCH_PARENT
124 stack.setLayoutParams(stackLp);
129 private ShapeDrawable createRoundedBackground(
boolean isTopButton) {
130 return createRoundedBackground(isTopButton, WidgetStyles.BUTTON_BACKGROUND_COLOR);
133 private ShapeDrawable createRoundedBackground(
boolean isTopButton,
int color) {
134 float radius = dpToPx((
int) WidgetStyles.BORDER_RADIUS);
135 float[] radii = isTopButton
136 ?
new float[]{radius, radius, radius, radius, 0, 0, 0, 0}
137 :
new float[]{0, 0, 0, 0, radius, radius, radius, radius};
139 ShapeDrawable drawable =
new ShapeDrawable(
new RoundRectShape(radii,
null,
null));
140 drawable.getPaint().setAntiAlias(
true);
141 drawable.getPaint().setColor(color);
146 protected void onMeasure(
int widthMeasureSpec,
int heightMeasureSpec) {
150 int width = dpToPx((
int) widthDp);
151 int height = dpToPx((
int) heightDp);
152 int widthSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);
153 int heightSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
154 super.onMeasure(widthSpec, heightSpec);
160 if (getChildCount() > 0) {
161 View stack = getChildAt(0);
162 if (stack instanceof android.widget.LinearLayout) {
163 android.widget.LinearLayout linearLayout = (android.widget.LinearLayout) stack;
164 if (linearLayout.getChildCount() >= 2) {
168 View zoomIn = linearLayout.getChildAt(0);
169 View zoomOut = linearLayout.getChildAt(1);
170 if (zoomIn instanceof Button) {
171 Button btn = (Button) zoomIn;
172 btn.setTextColor(textColor);
173 btn.setBackground(createRoundedBackground(
true, bgColor));
175 btn.setCompoundDrawablesWithIntrinsicBounds(
null, c.
getZoomInIcon(),
null,
null);
178 btn.setCompoundDrawablesWithIntrinsicBounds(
null,
null,
null,
null);
182 if (zoomOut instanceof Button) {
183 Button btn = (Button) zoomOut;
184 btn.setTextColor(textColor);
185 btn.setBackground(createRoundedBackground(
false, bgColor));
187 btn.setCompoundDrawablesWithIntrinsicBounds(
null, c.
getZoomOutIcon(),
null,
null);
190 btn.setCompoundDrawablesWithIntrinsicBounds(
null,
null,
null,
null);
199 private int dpToPx(
int dp) {
200 return (
int) (dp * getResources().getDisplayMetrics().density + 0.5f);