GithubHelp home page GithubHelp logo

jwkelso / aspectratioimageview Goto Github PK

View Code? Open in Web Editor NEW
5.0 3.0 0.0 808 KB

An ImageView that determines a default size for a WRAP_CONTENT parameter when the aspect ratio of an image that will be loaded into it is known, and the other param is MATCH_PARENT.

License: MIT License

Java 100.00%

aspectratioimageview's Introduction

AspectRatioImageView

An ImageView with a height or width dimension set to WRAP_CONTENT will experience jank when loading an image from the network or after a long-running background task.

Common solutions to this include setting the height and width to explicit values or setting a minimum height or width on the ImageView. Unfortunately, sometimes the images that are loaded from the network may be much larger than the size of the target view. In this case, the image must be resized to the bounds of the ImageView. This is simple when either the height or width of the ImageView is known.

If the constraint is MATCH_PARENT, however, the only way we can know the missing value of the image is to attach an OnGlobalLayoutListener or OnLayoutChangeListener and update the height after the first layout. Since the values are only known after the layout occurs, jank can still occur because of updating the layout.

The AspectRatioImageView eliminates the jank. It requires that the desired aspect ratio or the height and width of the original image to be set before measurement occurs. It will calculate an aspect ratio and update the measured dimension of the view to match what it will be when the image load is complete.

That's a lot to take in. Here's an image of what it does. The top cell is the AspectRatioImageView. The bottom cell is a standard ImageView:

alt text

##Usage

repositories {
    maven { url "https://github.com/jwkelso/android-maven/raw/master/" }
}

dependencies {
    compile 'com.jameskelso:aspect-ratio-imageview:1.1.1'
}
<com.jameskelso.android.widget.AspectRatioImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:originalImageWidth="3415px"
    app:originalImageHeight="2268px"/>

aspectratioimageview's People

Stargazers

Zeeshan rasool avatar 吴上阿吉 avatar yoshimaa avatar Shumpei Kobayashi avatar 李小义 avatar

Watchers

James Kelso avatar 吴上阿吉 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.