How to Add PanoramaImageView in Android

Advertisement
Today i am going to tell you a new Custom view which can blow your mind. This is using Gyroscope sensor.
An imageView can auto scroll with device rotating.

ScreenShots


horizontal sample


vertical sample
recyclerview sample

Include PanoramaImageView to Your Project

With gradle:

dependencies {
   compile 'com.gjiazhe:PanoramaImageView:1.0'
}

Use PanoramaImageView in Layout File Just Like ImageView

<com.gjiazhe.panoramaimageview.PanoramaImageView
    android:id="@+id/panorama_image_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/img"
    app:piv_enablePanoramaMode="true"
    app:piv_show_scrollbar="true"
    app:piv_invertScrollDirection="false" />

Description of Attributes

AttributesFormatDefaultDescription
piv_enablePanoramaModebooleantrueEnable panorama effect or not.
piv_show_scrollbarbooleantrueShow scrollbar or not.
piv_invertScrollDirectionbooleanfalseInvert the scroll direction or not
You can also check : Add Boom Menu For Android

All the attributes can also be set in java code:
panoramaImageView.setEnablePanoramaMode(true);
panoramaImageView.setEnableScrollbar(true);
panoramaImageView.setInvertScrollDirection(false);

Register the GyroscopeObserver

In Activity or Fragment using PanoramaImageView, you should register the GyroscopeObserver in onResume() and remember to unregister it in onPause().
public class MyActivity extends AppCompatActivity {

    private GyroscopeObserver gyroscopeObserver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Initialize GyroscopeObserver.
        gyroscopeObserver = new GyroscopeObserver();
        // Set the maximum radian the device should rotate to show image's bounds.
        // It should be set between 0 and π/2.
        // The default value is π/9.
        gyroscopeObserver.setMaxRotateRadian(Math.PI/9);

        PanoramaImageView panoramaImageView = (PanoramaImageView) findViewById(R.id.panorama_image_view);
        // Set GyroscopeObserver for PanoramaImageView.
        panoramaImageView.setGyroscopeObserver(gyroscopeObserver);
    }

    @Override
    protected void onResume() {
        super.onResume();
        // Register GyroscopeObserver.
        gyroscopeObserver.register(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        // Unregister GyroscopeObserver.
        gyroscopeObserver.unregister();
    }
}

Set OnPanoramaScrollListener to observe scroll state

Check this : Add Android Fingerprint Authentication in Android

If you want to get callback when the image scrolls, set an OnPanoramaScrollListener for PanoramaImageView.
panoramaImageView.setOnPanoramaScrollListener(new PanoramaImageView.OnPanoramaScrollListener() {
    @Override
    public void onScrolled(PanoramaImageView view, float offsetProgress) {
        // Do something here.
        // The offsetProgress range from -1 to 1, indicating the image scrolls
        // from left(top) to right(bottom).
    }
});
Embed this post on your website Embed