GithubHelp home page GithubHelp logo

react-native-responsive-image's Introduction

ResponsiveImage

A responsive Image component that chooses the best-resolution image for the current screen.

Installation

yarn add @exponent/react-native-responsive-image

Import it with:

import ResponsiveImage from '@exponent/react-native-responsive-image';

Usage

ResponsiveImage accepts the same props as Image plus a new prop called sources. The sources prop is an object whose keys are pixel ratios (that is, screen scales like "2" or "3"). Its values are Image sources to display on screens with the respective pixel ratio. This is how you use it:

<ResponsiveImage
  sources={{
    // The values are anything that Image's source prop accepts
    2: require('[email protected]'),
    3: { uri: 'https://example.com/[email protected]' },
  }}
  // (optional) force ResponsiveImage to load a specified pixel ratio
  preferredPixelRatio={2}
  // (optional) specify a custom function for rendering the image. You control
  // the props that are passed to the custom component. By default, React
  // Native's Image component is rendered.
  renderImageElement={(props) => <CustomImage {...props} />}
/>

Implementation

ResponsiveImage chooses the image source for the closest pixel ratio that is greater than or equal to screen's pixel ratio. You will get a crisp image while conserving bandwidth and system resources.

As an example, if you were to provide 2x and 3x sources as in the Usage example, these are the pixel ratios that ResponsiveImage will choose for various screens:

Screen Scale Chosen Pixel Ratio
1x 2x (use lowest available)
2x 2x (exact match)
2.5x 3x (round up to nearest)
3x 3x (exact match)
4x 3x (use highest available)

react-native-responsive-image's People

Watchers

James Cloos 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.