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.drawable.Drawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.HapticFeedbackConstants;
import android.view.View;
@ -67,18 +68,24 @@ public final class TouchscreenControllerButtonView extends View {
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
final int paddingLeft = getPaddingLeft();
final int paddingTop = getPaddingTop();
final int paddingRight = getPaddingRight();
final int paddingBottom = getPaddingBottom();
final int contentWidth = getWidth() - paddingLeft - paddingRight;
final int contentHeight = getHeight() - paddingTop - paddingBottom;
int leftBounds = 0;
int rightBounds = leftBounds + getWidth();
int topBounds = 0;
int bottomBounds = topBounds + getHeight();
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.
Drawable drawable = mPressed ? mPressedDrawable : mUnpressedDrawable;
if (drawable != null) {
drawable.setBounds(paddingLeft, paddingTop,
paddingLeft + contentWidth, paddingTop + contentHeight);
drawable.setBounds(leftBounds, topBounds, rightBounds, bottomBounds);
drawable.draw(canvas);
}
}

View file

@ -2,9 +2,11 @@ package com.github.stenzek.duckstation;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.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) {
final int leftBounds = subX * buttonWidth;
final int rightBounds = leftBounds + buttonWidth;
final int topBounds = subY * buttonHeight;
final int bottomBounds = topBounds + buttonHeight;
int leftBounds = subX * buttonWidth;
int rightBounds = leftBounds + buttonWidth;
int topBounds = subY * 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];
drawable.setBounds(leftBounds, topBounds, rightBounds, bottomBounds);

View file

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

View file

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

View file

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

View file

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