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
andyWeight
(float
s in [0;1]) indicate where to bind the image to the component;fit
indicates whether the image must fitinside
the component (by adding margins),outside
the component (by cropping), alwayshorizontal
ly or alwaysvertical
ly;scale
indicates whether the image can beupscale
d and/ordownscale
d.
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.