Android: Make touchscreen buttons larger when pushed

This commit is contained in:
Connor McLaughlin 2021-04-10 19:10:41 +10:00
parent 20ed6913a9
commit f72cbfc653
6 changed files with 38 additions and 16 deletions

View file

@ -5,6 +5,7 @@ import android.content.res.TypedArray;
import android.graphics.Canvas; import android.graphics.Canvas;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.HapticFeedbackConstants; import android.view.HapticFeedbackConstants;
import android.view.View; import android.view.View;
@ -67,18 +68,24 @@ public final class TouchscreenControllerButtonView extends View {
protected void onDraw(Canvas canvas) { protected void onDraw(Canvas canvas) {
super.onDraw(canvas); super.onDraw(canvas);
final int paddingLeft = getPaddingLeft(); int leftBounds = 0;
final int paddingTop = getPaddingTop(); int rightBounds = leftBounds + getWidth();
final int paddingRight = getPaddingRight(); int topBounds = 0;
final int paddingBottom = getPaddingBottom(); int bottomBounds = topBounds + getHeight();
final int contentWidth = getWidth() - paddingLeft - paddingRight;
final int contentHeight = getHeight() - paddingTop - paddingBottom; if (mPressed) {
final int expandSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
10.0f, getResources().getDisplayMetrics());
leftBounds -= expandSize;
rightBounds += expandSize;
topBounds -= expandSize;
bottomBounds += expandSize;
}
// Draw the example drawable on top of the text. // Draw the example drawable on top of the text.
Drawable drawable = mPressed ? mPressedDrawable : mUnpressedDrawable; Drawable drawable = mPressed ? mPressedDrawable : mUnpressedDrawable;
if (drawable != null) { if (drawable != null) {
drawable.setBounds(paddingLeft, paddingTop, drawable.setBounds(leftBounds, topBounds, rightBounds, bottomBounds);
paddingLeft + contentWidth, paddingTop + contentHeight);
drawable.draw(canvas); drawable.draw(canvas);
} }
} }

View file

@ -2,9 +2,11 @@ package com.github.stenzek.duckstation;
import android.content.Context; import android.content.Context;
import android.graphics.Canvas; import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.Log; import android.util.Log;
import android.util.TypedValue;
import android.view.View; import android.view.View;
public final class TouchscreenControllerDPadView extends View { public final class TouchscreenControllerDPadView extends View {
@ -122,10 +124,19 @@ public final class TouchscreenControllerDPadView extends View {
} }
private void drawDirection(int direction, int subX, int subY, Canvas canvas, int buttonWidth, int buttonHeight) { private void drawDirection(int direction, int subX, int subY, Canvas canvas, int buttonWidth, int buttonHeight) {
final int leftBounds = subX * buttonWidth; int leftBounds = subX * buttonWidth;
final int rightBounds = leftBounds + buttonWidth; int rightBounds = leftBounds + buttonWidth;
final int topBounds = subY * buttonHeight; int topBounds = subY * buttonHeight;
final int bottomBounds = topBounds + buttonHeight; int bottomBounds = topBounds + buttonHeight;
if (mDirectionStates[direction]) {
final int expandSize = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
10.0f, getResources().getDisplayMetrics());
leftBounds -= expandSize;
rightBounds += expandSize;
topBounds -= expandSize;
bottomBounds += expandSize;
}
final Drawable drawable = mDirectionStates[direction] ? mPressedDrawables[direction] : mUnpressedDrawables[direction]; final Drawable drawable = mDirectionStates[direction] ? mPressedDrawables[direction] : mUnpressedDrawables[direction];
drawable.setBounds(leftBounds, topBounds, rightBounds, bottomBounds); drawable.setBounds(leftBounds, topBounds, rightBounds, bottomBounds);

View file

@ -5,7 +5,8 @@
android:id="@+id/constraintLayout" android:id="@+id/constraintLayout"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical"> android:orientation="vertical"
android:clipChildren="false">
<com.github.stenzek.duckstation.TouchscreenControllerButtonView <com.github.stenzek.duckstation.TouchscreenControllerButtonView
android:id="@+id/controller_button_r2" android:id="@+id/controller_button_r2"

View file

@ -5,7 +5,8 @@
android:id="@+id/constraintLayout" android:id="@+id/constraintLayout"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical"> android:orientation="vertical"
android:clipChildren="false">
<com.github.stenzek.duckstation.TouchscreenControllerButtonView <com.github.stenzek.duckstation.TouchscreenControllerButtonView
android:id="@+id/controller_button_r2" android:id="@+id/controller_button_r2"

View file

@ -5,7 +5,8 @@
android:id="@+id/constraintLayout" android:id="@+id/constraintLayout"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical"> android:orientation="vertical"
android:clipChildren="false">
<com.github.stenzek.duckstation.TouchscreenControllerButtonView <com.github.stenzek.duckstation.TouchscreenControllerButtonView
android:id="@+id/controller_button_r2" android:id="@+id/controller_button_r2"

View file

@ -5,7 +5,8 @@
android:id="@+id/constraintLayout" android:id="@+id/constraintLayout"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical"> android:orientation="vertical"
android:clipChildren="false">
<com.github.stenzek.duckstation.TouchscreenControllerButtonView <com.github.stenzek.duckstation.TouchscreenControllerButtonView
android:id="@+id/controller_button_a" android:id="@+id/controller_button_a"