Giter VIP home page Giter VIP logo

krop's Introduction

Krop

License Build Status JitPack

The library is a small widget for image cropping in Instagram-like style.

GifDemo

Gradle

Step 1. Add this in your root build.gradle

    allprojects {
        repositories {
            maven { url 'https://jitpack.io' }
        }
    }

Step 2. Add the dependency

    dependencies {
        compile 'com.github.avito-tech:krop:VERSION'
    }

If you like to stay on the bleeding edge, or use certain commit as your dependency, you can use the short commit hash or anyBranch-SNAPSHOT as the version

Demo

Please see the demo app for library usage example.

Usage:

Add KropView to your layout:

<com.avito.android.krop.KropView
    android:id="@+id/krop_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:krop_aspectX="1"
    app:krop_aspectY="1"
    app:krop_offset="24dp"
    app:krop_overlayColor="#aaffffff"/>

To set bitmap you need to crop, just use method:

kropView.setBitmap(bitmap)

Obtaining result

To get cropped bitmap, use getCroppedBitmap:

val bitmap = kropView.getCroppedBitmap()

or get service object BitmapTransformation and crop bitmap manually when you need it:

val changes = kropView.getResultTransformation()
val result = bitmap.transformWith(changes)

Listeners

You may listen for image update event (move, scale, rotation), by defining TransformationListener:

    interface TransformationListener {

        fun onUpdate(transformation: KropTransformation)

    }

Basic View Configuration

  • Set an initial crop area's aspect ratio:

in XML layout:

app:krop_aspectX="4"
app:krop_aspectY="3"

or in code:

kropView.applyAspectRatio(aspectX = 4, aspectY = 3)
  • Set overlay color:

in XML layout:

app:krop_overlayColor="#aaffffff

or in code:

kropView.applyOverlayColor(color = 0xaaffffff)
  • Set minimum crop region offset (px):

in XML layout:

app:krop_offset="24dp"

or in code:

kropView.applyOffset(offset = 120)
  • Set overlay style:

in XML layout:

app:krop_shape="rect"

or in code:

kropView.applyOverlayShape(1)

All standart shapes are listed in OverlayShape class.

You can also create your own overlay, by inheriting OverlayView, and referencing it.

in XML layout:

app:krop_overlay="@id/custom_overlay"

or in code:

kropView.applyOverlay(CustomOverlay())

License

The MIT License (MIT)

Copyright (c) 2017 Avito Technology

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

krop's People

Contributors

alexbakker avatar dsvoronin avatar kutashov avatar lsh-silpion avatar nesterov-n avatar ntoskrnl avatar solkin avatar vlad103 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

krop's Issues

Unable to scroll image correctly

Library version: 0.64
First image - as you can see, I cant scroll to the very image's top.
Second image - as you can see, image is over scrolled (by the same offset as for image 1, I believe).
I've checked library sample - works fine. I removed all insets related stuff from my code, dunno what's the reason, but I wasnt' manage to make it work.
Tried to debug - no luck.
photo_2021-10-22 16 29 02
photo_2021-10-22 16 29 04

How to make the image fullscreen?

Is possible to make the image fullscreen?
Screenshot_20190517-192456

My layout is:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/overlay_color">

    <com.avito.android.krop.KropView
        android:id="@+id/cropView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:krop_offset="@dimen/default_offset"
        app:krop_overlayColor="@color/overlay_color"
        app:krop_shape="oval"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/buttonsLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:padding="@dimen/control_button_margin">

        <androidx.appcompat.widget.AppCompatImageView
            android:id="@+id/closeImageView"
            android:layout_width="@dimen/control_icon_size"
            android:layout_height="@dimen/control_icon_size"
            android:src="@drawable/ic_close"
            android:layout_margin="@dimen/control_button_margin"
            android:tint="@color/white"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <androidx.appcompat.widget.AppCompatImageView
            android:id="@+id/confirmImageView"
            android:layout_width="@dimen/control_icon_size"
            android:layout_height="@dimen/control_icon_size"
            android:layout_margin="@dimen/control_button_margin"
            android:src="@drawable/ic_confirm"
            android:tint="@color/white"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>



Exception when restoring KropView from savedInstanceState

The following exception is thrown when restoring KropView from savedInstanceState, causing the app to crash.

E/Parcel: Class not found when unmarshalling: com.avito.android.krop.ZoomableImageView$SavedState
    java.lang.ClassNotFoundException: com.avito.android.krop.ZoomableImageView$SavedState
        at java.lang.Class.classForName(Native Method)
        at java.lang.Class.forName(Class.java:453)
        at android.os.Parcel.readParcelableCreator(Parcel.java:2897)
        at android.os.Parcel.readParcelable(Parcel.java:2851)
        at com.avito.android.krop.KropView$SavedState.<init>(KropView.kt:263)
        at com.avito.android.krop.KropView$SavedState$CREATOR.createFromParcel(KropView.kt:268)
        at com.avito.android.krop.KropView$SavedState$CREATOR.createFromParcel(KropView.kt:266)
        at android.os.Parcel.readParcelable(Parcel.java:2860)
        at android.os.Parcel.readValue(Parcel.java:2754)
        at android.os.Parcel.readSparseArrayInternal(Parcel.java:3204)
        at android.os.Parcel.readSparseArray(Parcel.java:2416)
        at android.os.Parcel.readValue(Parcel.java:2811)
        at android.os.Parcel.readArrayMapInternal(Parcel.java:3123)
        at android.os.BaseBundle.initializeFromParcelLocked(BaseBundle.java:273)
        at android.os.BaseBundle.unparcel(BaseBundle.java:226)
        at android.os.Bundle.getSparseParcelableArray(Bundle.java:1009)
        at com.android.internal.policy.PhoneWindow.restoreHierarchyState(PhoneWindow.java:2129)
        at android.app.Activity.onRestoreInstanceState(Activity.java:1102)
        at android.app.Activity.performRestoreInstanceState(Activity.java:1057)
        at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1260)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
     Caused by: java.lang.ClassNotFoundException: com.avito.android.krop.ZoomableImageView$SavedState
        at java.lang.Class.classForName(Native Method)
        at java.lang.BootClassLoader.findClass(ClassLoader.java:1355)
        at java.lang.BootClassLoader.loadClass(ClassLoader.java:1415)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at java.lang.Class.classForName(Native Method) 
        at java.lang.Class.forName(Class.java:453) 
        at android.os.Parcel.readParcelableCreator(Parcel.java:2897) 
        at android.os.Parcel.readParcelable(Parcel.java:2851) 
        at com.avito.android.krop.KropView$SavedState.<init>(KropView.kt:263) 
        at com.avito.android.krop.KropView$SavedState$CREATOR.createFromParcel(KropView.kt:268) 
        at com.avito.android.krop.KropView$SavedState$CREATOR.createFromParcel(KropView.kt:266) 
        at android.os.Parcel.readParcelable(Parcel.java:2860) 
        at android.os.Parcel.readValue(Parcel.java:2754) 
        at android.os.Parcel.readSparseArrayInternal(Parcel.java:3204) 
        at android.os.Parcel.readSparseArray(Parcel.java:2416) 
        at android.os.Parcel.readValue(Parcel.java:2811) 
        at android.os.Parcel.readArrayMapInternal(Parcel.java:3123) 
        at android.os.BaseBundle.initializeFromParcelLocked(BaseBundle.java:273) 
        at android.os.BaseBundle.unparcel(BaseBundle.java:226) 
        at android.os.Bundle.getSparseParcelableArray(Bundle.java:1009) 
        at com.android.internal.policy.PhoneWindow.restoreHierarchyState(PhoneWindow.java:2129) 
        at android.app.Activity.onRestoreInstanceState(Activity.java:1102) 
        at android.app.Activity.performRestoreInstanceState(Activity.java:1057) 
        at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1260) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6494) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 
     Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available

Enable/Disable moving/zooming function

There is a way disable moving/zooming function ?

The idea is to activate moving/zooming function after a long press for example, with this you can use it in scroll view.

Thanks

Add utility to get cropped bitmap with given transformation without KropView

It would be nice to have the possibility to extract a cropped image from a given bitmap without using the Kropview.

Imagine the following case:

  • I have a view and crop the image
  • I just want to store the transformation data
  • at a later point I want to load the original bitmap and get the cropped bitmap with the stored transformation data, but without setting the bitmap on the Kropview and instead directly creating the cropped bitmap

ZoomableImageView won't show image when bitmap initialize with delay

I found this bug when try to set bitmap from image file that was recently shooted via camera. In this case my nexus 5x needs few seconds to render this bitmap to imageview, and looks like ZoomableImageView can't wait this time. Image never showed(except cases when I add breakepoints to it).

So, some details about problem in ZoomableImageView .
When inner image render with delay ZoomableImageView can change flag imageRenderedAtLeastOnce to true, but prevMatchViewSize will be with 0 values(because image is physically not rendered yet). And this params will broke method translateMatrixAfterRotate() (yep, divide by zero and Float.NaN as result), and after that our imgMatrix will contain NaN values.

Right now I fix it with changing logic in fitImageToView()
if (!isZoomed && !imageRenderedAtLeastOnce)
to
if ((!isZoomed && !imageRenderedAtLeastOnce) || (prevMatchViewSize.width == 0.0f && prevMatchViewSize.height == 0.0f))

and removing(because in new logic this will never be invoked)
if (prevMatchViewSize.width == 0.0f || prevMatchViewSize.height == 0.0f) { savePreviousImageValues() }

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.