Skip to content

rom1v/AImageView

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 
 
 

AImageView

An ImageView using an alternative to the ScaleType mechanism.

Why?

The ScaleType enum constants do not cover all common cases, and the choice of the right constant (if any) is not always intuitive.

Principle

AImageView provides 4 parameters:

  • xWeight and yWeight (floats in [0;1]) indicate where to bind the image to the component;
  • fit indicates whether the image must fit inside the component (by adding margins), outside the component (by cropping), always horizontally or always vertically;
  • scale indicates whether the image can be upscaled and/or downscaled.

Currently, it always preserves aspect ratio.

Example

    <com.rom1v.aimageview.AImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/myimage"
        app:xWeight="0.5"
        app:yWeight="0.5"
        app:fit="inside"
        app:scale="downscale|upscale" />

Here, the image will fit inside the component (margins will be added if necessary), exactly (downscaling and upscaling are accepted) and will be centered (0.5, 0.5).

ScaleType constants equivalences

Actually, the ScaleType enum constants correspond to specific parameters values. As you can notice, they do not cover all the combinations, and are not always explicit…

ScaleType.CENTER

  app:xWeight="0.5"
  app:yWeight="0.5"
  app:scale="disabled"
  // app:fit has no meaning when scale is disabled

ScaleType.CENTER_CROP

  app:xWeight="0.5"
  app:yWeight="0.5"
  app:fit="outside"
  app:scale="downscale|upscale"

ScaleType.CENTER_INSIDE

  app:xWeight="0.5"
  app:yWeight="0.5"
  app:fit="inside"
  app:scale="downscale"

ScaleType.FIT_CENTER

  app:xWeight="0.5"
  app:yWeight="0.5"
  app:fit="inside"
  app:scale="downscale|upscale"

ScaleType.FIT_END

  app:xWeight="1"
  app:yWeight="1"
  app:fit="inside"
  app:scale="downscale|upscale"

ScaleType.FIT_START

  app:xWeight="0"
  app:yWeight="0"
  app:fit="inside"
  app:scale="downscale|upscale"

ScaleType.FIT_XY

This configuration cannot be reproduced using AImageView parameters, since this component always preserve aspect ratio.

ScaleType.MATRIX

AImageView extends ImageView and force the scaleType to ScaleType.MATRIX (to scale and move the image content). Therefore, there is no "equivalent", AImageView is built upon it.

Build

The library is built using Gradle.

You can build the aar project library using:

./gradlew assembleRelease

It will be generated to library/build/outputs/aar/aimageview.aar.

You can use then use it in your projects.

The easiest way is to import it using Android Studio: File → New module… → Import .JAR or .AAR Project.

You can achieve the same result manually. First, put the aar into /aimageview/aimageview.aar (from your root project). Then, create /aimageview/build.gradle, containing:

configurations.create("default")
artifacts.add("default", file('aimageview.aar'))

Finally, declare it in your app dependencies:

dependencies {
    compile project(':aimageview')
}

Sample application

A sample application using it is (as a submodule) is also available:

git clone --recursive http://git.rom1v.com/AImageViewSample.git
cd AImageViewSample
./gradlew assembleDebug  # for example

(github mirror)

License

This library is published under the terms of the GNU/LGPLv3.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published