Android ImageView that transforms its drawable according to user input
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
app
gradle/wrapper
scalingimageview
.gitignore
CHANGELOG.md
Makefile
README.md
UNLICENSE
build.gradle
gradlew
gradlew.bat
settings.gradle

README.md

ScalingImageView

An ImageView that transforms its drawable according to touch input.

Supports rotated images and keeps transformation when you exchange the image for another one with a different size.

All of that in 374 lines of code (excluding blanks and comments).

Screencast

How to include

Android Archive

Just download the latest aar from Releases and put it into app/libs in your app.

Then make sure your app/build.gradle contains the following line in the dependencies block:

dependencies {
	implementation fileTree(dir: 'libs', include: '*')
	...
}

How to use

Maybe in a layout:

<de.markusfisch.android.scalingimageview.widget.ScalingImageView
	android:layout_width="match_parent"
	android:layout_height="match_parent"
	android:src="@drawable/your_drawable"/>

Or from java:

import de.markusfisch.android.scalingimageview.widget.ScalingImageView;

ScalingImageView scalingImageView = new ScalingImageView(context);

Changing the scale type must happen in source since reading attributes would require a declare-styleable resource and some overhead I think would outweigh its value for this.

So you need to call setScaleType():

scalingImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

Only CENTER, CENTER_CROP and CENTER_INSIDE are supported. CENTER_INSIDE is the default.

Please note that using android:adjustViewBounds="true" will implicitly set ScaleType to FIT_CENTER what is not supported and will result in an UnsupportedOperationException.

Supported Scale Types

ImageView.ScaleType.CENTER

Center the image in the view, but perform no scaling.

    +-------+ FRAME
    |       |
  +-|-------|-+
  | | IMAGE | |
  +-|-------|-+
    |       |
    +-------+

ImageView.ScaleType.CENTER_CROP

Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or larger than the corresponding dimension of the view.

      FRAME
+---+-------+---+
|   |       |   |
|   |       |   |
| I |M  A  G| E |
|   |       |   |
|   |       |   |
+---+-------+---+

ImageView.ScaleType.CENTER_INSIDE

Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or less than the corresponding dimension of the view.

    +-------+ FRAME
    |       |
    +-------+
    | IMAGE |
    +-------+
    |       |
    +-------+

Demo

In app/ you'll find a demo. Either import it into Android Studio or, if you're not on that thing from Redmond, just type make to build, install and run.

License

This widget is so basic, it should be Public Domain. And it is.