ulfben

Virtual keypad (Android)

Feb 14th, 2017
154
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. view_keypad.xml:
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4.                 android:layout_gravity="bottom"
  5.                 android:padding="@dimen/keypad_size"
  6.                 android:layout_width="match_parent"
  7.                 android:layout_height="wrap_content"
  8.     android:id="@+id/keypad">
  9.  
  10.     <Button android:id="@+id/keypad_up"
  11.             android:layout_alignParentTop="true"
  12.             android:layout_toRightOf="@+id/keypad_left"
  13.             android:layout_width="@dimen/keypad_size"
  14.             android:layout_height="@dimen/keypad_size" />
  15.  
  16.     <Button android:id="@+id/keypad_down"
  17.             android:layout_below="@+id/keypad_left"
  18.             android:layout_toRightOf="@+id/keypad_left"
  19.             android:layout_width="@dimen/keypad_size"
  20.             android:layout_height="@dimen/keypad_size" />
  21.  
  22.     <Button android:id="@+id/keypad_left"
  23.             android:layout_alignParentLeft="true"
  24.             android:layout_below="@+id/keypad_up"
  25.             android:layout_width="@dimen/keypad_size"
  26.             android:layout_height="@dimen/keypad_size" />
  27.  
  28.     <Button android:id="@+id/keypad_right"
  29.             android:layout_toRightOf="@+id/keypad_up"
  30.             android:layout_below="@+id/keypad_up"
  31.             android:layout_width="@dimen/keypad_size"
  32.             android:layout_height="@dimen/keypad_size" />
  33.  
  34.     <Button android:id="@+id/keypad_jump"
  35.             android:layout_alignParentRight="true"
  36.             android:layout_alignTop="@+id/keypad_left"
  37.             android:layout_width="@dimen/keypad_size"
  38.             android:layout_height="@dimen/keypad_size" />
  39. </RelativeLayout>
  40.  
  41.  
  42. //update styles.xml with fullscreen-flags
  43.     <item name="android:windowNoTitle">true</item>
  44.     <item name="android:windowActionBar">false</item>
  45.     <item name="android:windowFullscreen">true</item>
  46.     <item name="android:windowContentOverlay">@null</item>
  47.     <item name="android:immersive">true</item>
  48.  
  49.  
  50. //activity_main.xml
  51. <?xml version="1.0" encoding="utf-8"?>
  52. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  53.      xmlns:tools="http://schemas.android.com/tools"
  54.      android:layout_width="match_parent"
  55.      android:layout_height="match_parent"
  56.      tools:context="com.ulfben.platformer.MainActivity">
  57.  
  58.     <view class="com.ulfben.platformer.GameView"
  59.           android:id="@+id/gameView"
  60.           android:layout_width="fill_parent"
  61.           android:layout_height="fill_parent"/>
  62.  
  63.     <include layout="@layout/view_keypad" />
  64. </FrameLayout>
  65.  
  66. //constructors needed for view-inflation to not crash horribly
  67. public GameView(Context context) {
  68.     super(context);
  69.     init(context);
  70. }
  71. public GameView(Context context, AttributeSet attrs) {
  72.     super(context, attrs);
  73.     init(context);
  74. }
  75. public GameView(Context context, AttributeSet attrs, int defStyle) {
  76.     super(context, attrs, defStyle);
  77.     init(context);
  78. }
  79.  
  80.  
  81. //BasicInputManager.java
  82. BasicInputManager(View view){
  83.         view.findViewById(R.id.keypad_up).setOnTouchListener(this);
  84.         view.findViewById(R.id.keypad_down).setOnTouchListener(this);
  85.         view.findViewById(R.id.keypad_left).setOnTouchListener(this);
  86.         view.findViewById(R.id.keypad_right).setOnTouchListener(this);
  87.         view.findViewById(R.id.keypad_jump).setOnTouchListener(this);
  88.     }
  89.    
  90. @Override
  91. public boolean onTouch(final View v, final MotionEvent event) {
  92.     int action = event.getActionMasked();
  93.     int id = v.getId();
  94.     if(action == MotionEvent.ACTION_DOWN){// User started pressing a key
  95.         if(id == R.id.keypad_up){
  96.             mVerticalFactor -= 1;
  97.         }else if (id == R.id.keypad_down) {
  98.             mVerticalFactor += 1;
  99.         }
  100.         if (id == R.id.keypad_left) {
  101.             mHorizontalFactor -= 1;
  102.         } else if(id == R.id.keypad_right) {
  103.             mHorizontalFactor += 1;
  104.         }
  105.         if (id == R.id.keypad_jump) {
  106.             mIsJumping = true;
  107.         }
  108.     } else if(action == MotionEvent.ACTION_UP) {
  109.         if (id == R.id.keypad_up) {
  110.             mVerticalFactor += 1;
  111.         } else if (id == R.id.keypad_down) {
  112.             mVerticalFactor -= 1;
  113.         }
  114.         if (id == R.id.keypad_left) {
  115.             mHorizontalFactor += 1;
  116.         } else if (id == R.id.keypad_right) {
  117.             mHorizontalFactor -= 1;
  118.         }
  119.         if (id == R.id.keypad_jump) {
  120.             mIsJumping = false;
  121.         }
  122.     }
  123.     return false;
  124. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×