Android layout that simulates physics using JBox2D

Overview

PhysicsLayout

Build Status

Android layout that simulates physics using JBox2D. Simply add views, enable physics, and watch them fall!

See it in action with the sample app:

Google Play

Gradle Dependency

Add this in your root build.gradle file (not your module build.gradle file):

allprojects {
	repositories {
		...
		maven { url "https://jitpack.io" }
	}
}

Then, add the library to your project build.gradle

dependencies {
    implementation("com.github.Jawnnypoo:PhysicsLayout:latest.version.here")
}

Basic Usage

If you want to see what your layout looks like when physics is applied to it, simply change your root layout to a physics layout.

<com.jawnnypoo.physicslayout.PhysicsLinearLayout
  android:id="@+id/physics_layout"
  android:layout_width="match_parent"
  android:layout_height="200dp">
            
      <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"/>

      <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"/>
              
      <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello world, I have physics!"/>
            
</com.jawnnypoo.physicslayout.PhysicsLinearLayout>

Custom XML Attributes

You can also further customize the behaviour of your PhysicsLayout

<com.jawnnypoo.physicslayout.PhysicsLinearLayout
  android:id="@+id/physics_layout"
  android:layout_width="match_parent"
  android:layout_height="200dp"
  app:physics="true"
  app:gravityX="0.0"
  app:gravityY="9.8"
  app:bounds="true"
  app:boundsSize="50dp"/>
  • physics boolean, Determines if physics will be applied to the layout (Default true)
  • gravityX float, Sets the gravity in the X direction (positive is right, negative is left) (Default 0)
  • gravityY float, Sets the gravity in the Y direction (positive is down, negative is up) (Default 9.8)
  • bounds boolean, Determines if the layout should have bounds on the edges of itself (Default true)
  • boundsSize dimenstion, Sets the width/height of the bounds on the edges (Default 20dp)

Custom Physics Configuration

Each view contained within the layout has a physics configuration that it uses to create itself in the Box2D world. This defines its shape, mass, restitutaion, and other physics related variables. A custom configuration can be applied to each view as well:

<TextView
  android:id="@+id/text"
  android:layout_width="20dp"
  android:layout_height="20dp"
  app:layout_shape="circle"
  app:layout_circleRadius="20dp"
  app:layout_bodyType="kinematic"
  app:layout_fixedRotation="true"
  app:layout_friction="0.8"
  app:layout_restitution="0.3"
  app:layout_density="0.5" />

or alternatively, the Physics definition can be made programmatically:

val circleView = findViewById<View>(R.id.circle)
val config = PhysicsConfig(
    shape = Shape.CIRCLE,
    fixtureDef = fixtureDef,
    bodyDef = bodyDef
)
Physics.setPhysicsConfig(circleView, config)

This is useful especially if you have view that would be considered circular, as the default for all views is a RECTANGLE shape. Most of the time, if you are just dealing with rectangular views, the defaults will work for you and you will not have to worry about this.

Check out the sample app to see most of these things in action.

Making a Game?

This library was designed with the intention of allowing for playful animations within normal Android apps. It is not built to be a game engine or meant to compete with the likes. If you are looking to do more intense mobile games, we recommend libraries such as libGDX or Unity

License

Copyright 2020 John Carlson

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Comments
  • Get Body after adding View dynamically

    Get Body after adding View dynamically

    Hi, I'm adding Views to the PhysicsLayout with addView(). I want to create a Joint between multiple dynamically added Views. How can I access the Body Instance right after adding the View so I can create a Joint?

    opened by OFmarting 6
  • Fling not working

    Fling not working

    I have app:fling="true" and app:physics="true" in my PhysicsFrameLayout, but I cant't fling the ImageVew inside.

    Is there something extra I need to do?

    opened by sptallent 4
  • Help with adding view to PhysicsLinearLayout

    Help with adding view to PhysicsLinearLayout

    Hi John, thank you for making this awesome library. I am trying to use it with my project. But when I tried to add child views to the PhysicsLinearLayout, the animation paused when new child view was added, like

    physicsLayout = (ViewGroup) rootView.findViewById(R.id.physics_layout);
    physicsLayout.addView(textView, params);
    

    When a new TextView was added to the PhysicalLinearLayout meanwhile a previously added TextView was falling down, the latter will pause at the moment the new view was added. Any idea to solve this ?

    Thanks again for this awesome library!

    opened by jianzhongli 4
  • Performance issue when I add more than 10 views

    Performance issue when I add more than 10 views

    Hi,

    I have encounter a performance issue when I built my own app. That is when I add more than 10 object inside the view. It start getting lag and sometimes crashed. I also download your sample app and everything seems fine. So I make my code almost as same as yours. But the problem is still there. Could you give my some advice or how you make your app's performance better?

    opened by norman8154 2
  • Applying force to views

    Applying force to views

    Hi, i found your great Project and was amazed how quick this leads to cool looking results. After fiddling around a bit i was wondering how to apply a force to views. I want them to "fall" into the middle of the screen like there is the center of a huge mass. I'm trying to misuse the gravity property to simulate a force, but it would be nice to have an interface to the "bodys" of the box2d entitys.

    Thanks for you work :)

    opened by pythoneer 2
  • Help with onClickListener on PhysicsLayout child view

    Help with onClickListener on PhysicsLayout child view

    Hi, my name is Eduardo and i have been trying to figure out a way to set a onClickListener on a PhysicsLayout child view to change its background color. The thing is, when i do, all the physics behaviour stop working. Do you have any suggestions that could help me?

    Sorry if this is a obvious question, i am just starting android developing :P

    opened by edunuzzi 2
  • Difference between the sample on Google Play and sample on github

    Difference between the sample on Google Play and sample on github

    There is alot of lag in the github sample where as i can add multiple views without any lag in the sample on Google Play without any lag.

    why is that?

    opened by AraibKarim 1
  • How to update radius of already added view

    How to update radius of already added view

    I'm working on scaling the objects within physics layout.When scale objects their radius does not change and the view goes beyond boundaries. Is there any way to change the radius of already added view in the layout?

    opened by farhancroem 1
  • Hello, ask the next question

    Hello, ask the next question

    I imitated your project, but the two balls did not collide with each other. This is the project I wrote, can help me find https://github.com/xufeifandj/Ball

    opened by chongbo2013 1
  • Gravitation and gesture additions

    Gravitation and gesture additions

    Added gravity to a point in the world with optional gravitation to the captured view at a definable speed

    Gesture detector to allow easy single, double and long clicking on added views

    opened by BWMuller 1
  • Consider making PixelsPerMeter a function of DP by default

    Consider making PixelsPerMeter a function of DP by default

    Testing out this great library, and was a little puzzled when different DPI phones had boundaries at different positions.

    I've been able to set this myself using setPixelsPerMeter but I think it would be a great default if 50 was replaced by 50/3 * density (since most phone densities today are 3 or 4)

    opened by mkonecny 1
  • set

    set "layout_fixedRotation" dynamically

    Hi, Thanks for your great lib.

    Is there anyway to set layout_fixedRotation dynamically? I want to add an inflated view to PhysicsRelativeLayout in runtime, but i cannot set this feature for the inflated view. how can I do that?

    Thanks in advance.

    opened by rkarimi88 0
  • drag & drop object

    drag & drop object

    Hello. I have imported your lib in my app and am using its 'statik' feature for drag & drop ImageViews. It is a great library, thank you :)

    My question is: Is there a way to detect where user drops the object?

    Edit: When I use this code to detect user's touch release, the object (ball) doesn't move anymore:

    ball.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if(event.getAction() == MotionEvent.ACTION_UP)
                        Toast.makeText(MainActivity.this, "Dropped.", Toast.LENGTH_SHORT).show();
                    return true;
                }
            });
    
    opened by rkarimi88 0
  • How to use fling=true in a scrollview?

    How to use fling=true in a scrollview?

    I've experimented with a number of different ways to intercept the touch events, but I cannot seem to figure out how to enable fling when the PhysicsFrameLayout is in a NestedScrollView, and be able to drag the views inside it vertically.

    I can drag them horizontally fine, but as soon as I start dragging vertically I get an ACTION_CANCEL MotionEvent, and my dragging of the view inside the PhysicsFrameLayout stops.

    opened by dphillipsprosper 0
Releases(3.0.1)
  • 3.0.1(Feb 17, 2022)

  • 3.0.0(Apr 27, 2020)

    PhysicsLayout is now written in Kotlin! A few things changed with the API, but you should be able to migrate using the sample project or the README as a reference. If you were using PhysicsLayout only using XML, you should be good to go right away!

    Source code(tar.gz)
    Source code(zip)
  • 2.1.0(Aug 23, 2016)

  • 2.0.0(May 28, 2016)

    This release is a 2.0 even though many APIs have remained relatively the same, it adds support for:

    -Parsing the XML properties of children -Simply PhysicsConfig to work more like their Box2D counterparts -Process the Physics body that is currently being dragged

    Source code(tar.gz)
    Source code(zip)
  • 1.0.0(May 28, 2016)

Owner
John Carlson
Android Developer @instacart Professional griefer @Commit451
John Carlson
GoolgePlusLayout is a custom layout that plays animation on the children views while scrolling as the layout in the Google Plus (android) main page

Google Plus Layout Google Plus Layout is a custom layout that support playing animation on child view(s) in a serialize manner like the the main

Ahmed Nammari 224 Nov 25, 2022
A layout to transition between two views using a Floating Action Button as shown in many Material Design concepts

⚠ This library is no longer maintained ⚠️ FABRevealLayout A layout to transition between two views using a Floating Action Button as shown in many Mat

Tomás Ruiz-López 901 Dec 9, 2022
A Playground repository to showcase UI's and transitions built using Motion Layout.

A collection of UI's built to showcase the capabilities of Motion Layout and Constraint Layout 2.0/2.1

Saurabh 163 Dec 22, 2022
An Android Layout which has a same function like https://github.com/romaonthego/RESideMenu

ResideLayout An Android Layout which has a same function like https://github.com/romaonthego/RESideMenu. Can be used on Android 1.6(I haven't try it.)

Yang Hui 392 Oct 12, 2022
A very simple arc layout library for Android

ArcLayout A very simple arc layout library for Android. Try out the sample application on the Play Store. Usage (For a working implementation of this

ogaclejapan 1.4k Dec 26, 2022
An Android demo of a foldable layout implementation. Engineered by Vincent Brison.

Foldable Layout This code is a showcase of a foldable animation I created for Worldline. The code is fully written with java APIs from the Android SDK

Worldline 599 Dec 23, 2022
A 3D Layout for Android,When you use it warp other view,it can became a 3D view,一秒让你的view拥有3D效果!

ThreeDLayout A 3D Layout,When you use it warp other view,it can became a 3D view 中文文档 preview USAGE 1.compile library allprojects { repositories {

androidwing 490 Oct 27, 2022
It's an Android library that allows you to use Layout as RadioButton or CheckBox.

Android - CompoundLayout It's an Android library that allows you to use Layout as RadioButton or CheckBox. The librarie is Android 14+ compatible. Gra

null 483 Nov 25, 2022
A pull to refresh layout for android, the RecyclerRefreshLayout is based on the SwipeRefreshLayout. support all the views, highly customizable, code simplicity, etc. really a practical RefreshLayout!

RecyclerRefreshLayout English | 中文版 RecyclerRefreshLayout based on the {@link android.support.v4.widget.SwipeRefreshLayout} The RecyclerRefreshLayout

dinus_developer 1.7k Nov 10, 2022
Easy, flexible and powerful Swipe Layout for Android

SwipeRevealLayout A layout that you can swipe/slide to show another layout. Demo Overview Drag mode Drag mode normal: Drag mode same_level: Features F

Chau Thai 1.5k Jan 4, 2023
[UNMAINTAINED]: AndroidMosaicLayout is android layout to display group of views as grid consists of different asymmetric patterns (90 different patterns).

AndroidMosaicLayout AndroidMosaicLayout is android layout to display group of views in more that 90 different patterns. What is AndroidMosaicLayout? I

Adham Enaya 474 Nov 12, 2022
Scalable Layout For Android

ScalableLayout for Android. Class: com.ssomai.android.scalablelayout.ScalableLayout 한글버전 README.md: https://github.com/ssomai/ScalableLayout/blob/mast

Youngmann Kim 273 Nov 21, 2022
Android implementation of FlowLayout. Layout arranges its children in multiple rows depending on their width.

FlowLayout FlowLayout is an opensource Android library that alows developers to easily integrate flow layout into their app. FlowLayout is an layout t

Blaž Šolar 754 Dec 15, 2022
Circular layout for android

CircleLayout Circular layout for android. Installlation Add CircleLayout as Android Library to your project. How to add project as Android Library Usa

Dmitry Zaytsev 244 Nov 18, 2022
An Android layout for arranging children along a circle

CircleLayout An Android layout for arranging children along a circle You can customize the following options: cl_centerView: Set a specific view ID to

Francois Campbell 374 Nov 18, 2022
Android - A layout that arranges its children in relation to a background image

ImageLayout A layout that arranges its children in relation to a background image. The layout of each child is specified in image coordinates (pixels)

Manuel Peinado Gallego 419 Nov 14, 2022
null 2.4k Dec 30, 2022
Android Shadow Layout

?? Before using this library, read information below ?? This library is not more supported. If you want to add new feature or fix a bug, grab source

Dmytro Danylyk 1.8k Jan 2, 2023