Coder Social home page Coder Social logo

physicslayout's Introduction

PhysicsLayout

Sample Gif

Android Arsenal

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

The gif is choppy, see it in action with the sample app:

Get it on Google Play

Gradle Dependency (jCenter)

Easily reference the library in your Android projects using this dependency in your module's build.gradle file:

dependencies {
    compile 'com.jawnnypoo:physicslayout:1.0.0'
}

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"/>
  • 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)

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, using the PhysicsConfiguration builder:

final View circleView = findViewById(R.id.circle);
PhysicsConfig config = new PhysicsConfig.Builder()
                .setShapeType(PhysicsConfig.ShapeType.CIRCLE)
                .setRadius(100)
                .setAllowRotation(true)
                .setBodyDefType(BodyType.STATIC)
                .setDensity(1.0f)
                .setFriction(1.0f)
                .setRestitution(1.0f)
                .build();
physicsLayout.getPhysics().setPhysicsConfig(circleView, config);

This is useful especially if you have view that would be considered circular, as the default for all views is a RETANGLE shape type. 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.

Using this library?

I wanna see the cool ideas and stuff you guys have made with this. If you're using this library in one of your projects just send me a tweet and I'll add your project to the list.

physicslayout's People

Contributors

jawnnypoo avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.