GithubHelp home page GithubHelp logo

radiokot / physical-object-layout Goto Github PK

View Code? Open in Web Editor NEW
14.0 1.0 1.0 426 KB

💳 An Android layout that properly scales its content in order to design physical objects such as credit cards, passports, etc.

License: MIT License

Kotlin 100.00%
android layout ui credit-card

physical-object-layout's Introduction

PhysicalObjectLayout

Latest release License: MIT

An Android layout that properly scales its content in order to display physical objects such as credit cards, passports, etc.

Example

Download Sample App APK

Dependency

Ensure that you have JitPack repo added to your project build.gradle file:

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

Add the library to your app build.gradle file:

dependencies {
	implementation 'com.github.radiokot:physical-object-layout:1.0.1'
}

What problem does it solve?

If you have ever tried to design and display a representation of physical world object, i.e. a credit card, in your Android app I’m pretty sure you have noticed, that the way it appears on the screen is… different 🤔

In Android we make fluid layouts. We specify sizes and margins in device-related units, set gravity for views and constraining them to each other to avoid overlaps. As a result, our layout looks good on different screens.

But when it comes to displaying a physical object, things get messy:

The problem

Unlike our regular layouts, physical objects are not fluid, they must be proportionally scaled instead. When you figure this out you also realize, that such scaling of a layout is not an ordinary thing to do in Android.

  • You can’t use post-scaling because it looks blurry and requires drawing outside of the view frame
  • You can’t just make this view a vector drawable because some of the elements have variable content
  • Creating many-many dimens for every screen width is just crazy

PhysicalObjectLayout comes to the rescue!

All you need to do is:

  1. Create your layout with all sizes and margins fixed and set in px. Without any tricky layouts, without dimens, literally a copy of what the designers sent to you
  2. Put the view into the PhysicalObjectLayout
  3. Enjoy your view properly scaled to fit the container width or height, just like if it was an image

Example

Layout attributes

PhysicalObjectLayout has a few attributes that controls scaling and appearance:

Name Description Default
pol_scaleBy Specifies a dimension that children will be scaled to fit to width
pol_addChildrenInvisible If enabled the layout will set its children visibility to View.INVISIBLE on add. As soon as the scaling is not instant it is recommended to keep this attribute enabled in order to avoid unscaled child blink true
pol_makeChildrenVisibleAfterScale If enabled the layout will set its children visibility to View.VISIBLE once they are scaled. Recommended to use in a combination with the previous attribute true

Scaling custom views

PhysicalObjectLayout allows you to specify custom scaling strategies in order to scale custom views.

You can implement your own ViewScalingStrategy and apply it programmatically using addScalingStrategies or addScalingStrategy methods.

Examples of custom scaling strategies can be found in the library itself: CardViewScalingStrategy, TextViewScalingStrategy

License

The MIT License

Copyright (c) 2020 Oleg Koretsky

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the “Software”), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

physical-object-layout's People

Contributors

radiokot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

allenpig

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.