• Stars
    star
    504
  • Rank 87,537 (Top 2 %)
  • Language
    Java
  • License
    MIT License
  • Created over 8 years ago
  • Updated almost 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Simple and powerful library to emulate iOS's "3D Touch" preview functionality on Android.

Android 3D Touch - PeekView

preview

iOS uses 3D Touch as a way to "peek" into full content, such as emails, pictures, web searches, etc. While they have dedicated hardware for this functionality, it is still possible to get similar functionality out of Android, with a long click, rather than the dedicated hardware.

This library aims to create a simple and powerful API to enable 3D Touch style "Peeking" on Android.

Features

features

  1. Simple API
  2. Quick and easy to implement in any app
  3. Long clicking a view will overlay the PeekView until you stop touching the screen again
  4. The PeekView is smart. It will be displayed relative to the touch location, not on some random place on the screen. It will also move out of the way of your finger so that it isn't covered up when displayed.
  5. Customize the PeekView's window size (fullscreen or percent of screen size)
  6. Change the background dim amount
  7. Haptic feedback is optional
  8. Peek into ANY type of views. Example even uses a WebView
  9. Blur the background behind the PeekView for an extra "WOW" factor over the dim percentage

Installation

There are two ways to use this library:

As a Gradle dependency

This is the preferred way. Simply add:

dependencies {
    compile 'com.klinkerapps:peekview:1.2.3'
}

to your project dependencies and run ./gradlew build or ./gradlew assemble.

As a library project

Download the source code and import it as a library project in Eclipse. The project is available in the folder library. For more information on how to do this, read here.

Example Usage

This library is extremely easy to use. There are two steps:

  • Any activities you want to implement the PeekView on, you must use PeekViewActivity as the superclass.

PeekViewActivity is a descendant of AppCompatActivity, so for almost every case, this will just be plug and play, a simple switch. PeekView requires this activity to montitor the touch events and determine when the user lifts their finger to remove the PeekView.

  • Use the Peek.into(... builder to start creating your PeekView on a UI element. There are examples of this in the MainActivity, but here is a very simple one that will display an image full screen.
Peek.into(R.layout.image_peek, new SimpleOnPeek() {
    @Override
    public void onInflated(View rootView) {
        // rootView is the layout inflated from R.layout.image_peek
        ((ImageView) rootView.findViewById(R.id.image))
                .setImageDrawable(getResources().getDrawable(R.drawable.klinker_apps));
    }
}).applyTo(this, findViewById(R.id.image_peek));

More advanced usage

Here are a few of the more advanced things that you can do with PeekView

PeekViewOptions

This is how you customize your PeekView. After creating a PeekViewOptions object, simply add it with Peek.with(options)

Here is a list of all the possible options, along with the implementation:

PeekViewOptions options = new PeekViewOptions();
options.setBackgroundDim(1f);           // range: 0  - 1  (default is .6)
options.setHapticFeedback(false);       // default is true

// it may be a good idea to set set these through resources so that you can use different options based on screen size and orientation
options.setWidthPercent(.4f);           // range: .1 - .9 (default is .6)
options.setHeightPercent(.4f);          // range: .1 - .9 (default is .5)

// you can also set the size of the PeekView using absolute values, instead of percentages. 
// Setting these will override the corresponding percentage value.
// You should use this instead of setting the size of the view from the layout resources, as those get overridden.
options.setAbsoluteWidth(200);          // 200 DP
options.setAbsoluteHeight(200);         // 200 DP

// default is false. If you change this to true, it will ignore the width and height percentages you set.
options.setFullScreenPeek(true); 
// default is true. Unless you are going to animate things yourself, i recommend leaving this as true.
options.setFadeAnimation(false);

// PeekView has the ability to blur the background behind it, instead of just using a simple dark dim.
// If you set a blurred view, then it will invalidate whatever you set as your background dim.
// If you do this, please look at the installation steps for the blur effect, or the app will crash.
options.setBlurBackground(true);                            // default is true
options.setBlurOverlayColor(Color.parse("#99000000"));      // #99000000 default

Peek.into(...).with(options).applyTo(...);

Different lifecycle events

PeekView has a number of lifecycle events that you can choose to implement. In the above example, I just showed the use of SimpleOnPeek, which provides a callback so that you can initialize your layout after it has been inflated. Similar to what you would do after Activity.setContentView(...).

If you are doing any kind of custom animations, or network calls though, you may need something more powerful. SimpleOnPeek is just a wrapper of the OnPeek interface, which also contains callbacks for when the PeekView is displayed to the user on the screen (helpful if you wanted to do your own animations instead of use the default fade animation), as well as a callback for when the PeekView is dismissed from the screen (a good place to stop any networking activities).

Implementing these callbacks is straightforward and almost the same as you have done with the SimpleOnPeek:

Peek.into(..., new OnPeek() {
    @Override
    public void onInflated(View rootView) {
        // the normal inflation callback
    }

    @Override
    public void shown() {
        // the view is shown to the user
        // Could be a nice place for custom animations on the inflated view
    }

    @Override
    public void dismissed() {
        // the view is dismissed from the screen
        // It is destroyed an never reused, so any cleanup would go here
    }
}).applyTo(...);

Clearing the Peek for a View

Sometimes it may be necessary to not allow "peeking" in a View where it was previously allowed. Some situations that come to mind would be in a RecyclerView or ListView where content could be different. Because of the way these lists work, when the view gets recycled, it will retain the "peeking" ability of the original View, unless you clear it. Clearing a "peek" is really easy:

Peek.clear(View);

Currently, all this method does is set the TouchListener to null for the provided View. In the future though, there may be more advanced changes that will be made to this method, so, it is probably safer to use the Peek#clear method instead of just nulling the TouchListener yourself.

Contributing

Please fork this repository and contribute back using pull requests. Features can be requested using issues. All code, comments, and critiques are greatly appreciated.

Changelog

The full changelog for the library can be found here.

License

Copyright 2016 Luke Klinker

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

More Repositories

1

talon-twitter-holo

[Deprecated] The Holo version of my popular Android Talon for Twitter app, 100% open-source
Java
1,636
star
2

Android-TextView-LinkBuilder

Insanely easy way to define clickable links within a TextView.
Kotlin
1,590
star
3

talon-for-twitter-android

The most powerful and beautiful Twitter client available.
Java
1,192
star
4

Android-DragDismissActivity

A smooth, easy-to-implement, drag to dismiss Android Activity.
Java
679
star
5

Android-FloatingTutorialActivity

A light-weight, easy-to-implement, and easy-to-look-at way to do a tutorial pager or dialog on Android
Kotlin
351
star
6

wearable-reply

Simplify text input for Android Wear 2.0, by voice, keyboard, or canned response.
Java
120
star
7

FloatingWindowDemo

Creating Floating Window Popups
Java
103
star
8

launcher3

Gradle built Launcher3 from Nougat
Java
96
star
9

Android-BadgedImageView

Simple library for placing media type tags or text over an ImageView
Java
82
star
10

wedding-app

Seating chart for my wedding
Java
75
star
11

google-calendar-desktop

A simple Electron app to display Google Calendar
JavaScript
60
star
12

Android-GiphySearch

Super easy way to find awesome GIFs for your apps
Java
51
star
13

nougat-7.1-playground

Examples of launcher shortcuts, image keyboard, and round icon support
Java
49
star
14

Android-SimpleVideoView

Simplified replacement for Android's overly complex VideoView
Java
46
star
15

talon_theme

Example themeing for my Talon for Twitter Android app
23
star
16

help-site-template

A template to create a help site for an app, web service, or anything you need to direct users to.
JavaScript
17
star
17

slideover-messaging

SlideOver Messaging App
Java
14
star
18

android-chat

Push Notification Chat App for Android
Java
12
star
19

todoist-desktop

Another desktop implementation of Todoist, for Linux.
JavaScript
11
star
20

swift-calendar

Super simple calendar sample using Swift for iOS
Swift
8
star
21

iot_smoke_detector

An Internet of Things enabled smoke detector
Java
4
star
22

Android-EnlargedNumbers-Spannable

Demonstration of manipulating text styles/sizes within the same TextView.
Java
4
star
23

twitter-push

Push Notifications for Twitter through Chrome
Java
3
star
24

wedding-on-rails

Wedding website with Ruby on Rails
Ruby
1
star
25

klinker24

1
star