Scratchoff


Source link: https://github.com/jackpocket/android_scratchoff

Scratchoff

A simple library for implementing scratchable Views.

Installation

repositories {

  jcenter() 
}
  dependencies {

  compile('com.jackpocket:scratchoff:1.1.0')
  compile 'com.android.support:support-v4:23.4.0' 
}

Usage

The goal of this library is to create a scratchoff interface. By storing and manipulating the drawing cache of a View, we can create the effect of scratching it away to reveal what's hidden below.

First, you need a RelativeLayout (to align layouts on top of one another) consisting of 2 sub-layouts, a behind-View and the View to be scratched away from the foreground. Here is a simple example using the ScratchableLinearLayout:

<RelativeLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent" >

<RelativeLayout

android:id="@+id/scratch_view_behind"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background="#818B8D" >

<ImageView

 android:layout_width="fill_parent"

 android:layout_height="wrap_content"

 android:layout_centerInParent="true"

 android:layout_margin="25dip"

 android:adjustViewBounds="true"

 android:src="@drawable/some_drawable_to_be_revealed" />
  </RelativeLayout>

<com.jackpocket.scratchoff.views.ScratchableLinearLayout

android:id="@+id/scratch_view"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background="#3C9ADF" >

<ImageView

 android:layout_width="fill_parent"

 android:layout_height="wrap_content"

 android:layout_gravity="center"

 android:adjustViewBounds="true"

 android:gravity="center"

 android:src="@drawable/some_top_drawable" />
  </com.jackpocket.scratchoff.views.ScratchableLinearLayout> </RelativeLayout>

Note: be careful with the dimensions of both the behind-View and the foreground View. The ScratchableLayoutDrawer will attempt to set the foreground View's LayoutParam width and height attributes to match that of the behind-View so that their dimensions line up perfectly.

Now that you have a layout, we need to attach the ScratchoffController to it:

ScratchoffController controller = new ScratchoffController(context)

.setThresholdPercent(0.40d)

.setTouchRadiusDip(context, 30)

.setFadeOnClear(true)

.setClearOnThresholdReached(true)

.setCompletionCallback(() -> {
  
}
)

.attach(findViewById(R.id.scratch_view), findViewById(R.id.scratch_view_behind));

In this example, you only need the constructor and the attach(View, View) method to enable scratching. The default values for all the other methods are configurable by overriding the appropriate resources.

Since the foreground View in our example is a ScratchableLinearLayout (which implements ScratchableLayout), the ScratchoffController will automatically attach itself to the View and drawing will work correctly (the same goes for the ScratchableRelativeLayout).

Please note: If you're not using one of the supplied ScratchableLayouts, you must manually call ScratchoffController.draw(Canvas) from your custom View's onDraw(Canvas) method.

Re-using the ScratchController

With version 1.0.2, the ScratchController can be correctly reset using ScratchController.reset(), but you must set the background color of your ScratchableLayout back to something opaque before calling it, as the ScratchableLayoutDrawer must set it to transparent afterwards in order to efficiently process scratched paths. e.g.

controller = new ScratchoffController(this)

.setCompletionCallback(() ->{

 findViewById(R.id.scratch_view)

.setBackgroundColor(0xFF3C9ADF);
 // Make sure to set the background. Don't worry, it's still hidden if it cleared

  new Handler(Looper.getMainLooper())

.postDelayed(() ->

controller.reset(), 2000);

}
)

.attach(findViewById(R.id.scratch_view), findViewById(R.id.scratch_view_behind));

Lifecycle

As a final note, if using the ScratchoffController in the context of an Activity, you may want to also ensure you call the correct lifecycle methods for onPause(), onResume(), and onDestroy() as needed, to ensure the processors will stop/restart and not run needlessly in the background. e.g.

@Override public void onPause(){

  controller.onPause();

  super.onPause();
 
}
  @Override public void onResume(){

  super.onResume();

  controller.onResume();
 
}
  @Override public void onDestroy(){

  controller.onDestroy();

  super.onDestroy();
 
}

Since version 1.1.0, the processors will stop if ViewCompat.isAttachedToWindow(View) returns false for whatever scratchable layout the controller was attached to (that's why there's now a support package dependency...).

Resources

An Android image picker and cropper library.

Android - FilePickerFilter Library.

Simple text counter animation. You need to set from number and to number! And this library will create animation. Also you can use different modes.

CameraKit is an extraordinarily easy to use utility to work with the infamous Android Camera and Camera2 APIs.

Android library to display markdown text.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes