Skip to content

Instantly share code, notes, and snippets.

@aminelaadhari
Forked from amlcurran/LightboxActivity.java
Created February 18, 2014 22:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aminelaadhari/9081564 to your computer and use it in GitHub Desktop.
Save aminelaadhari/9081564 to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lightboxView"
android:background="@android:color/white" />
<?xml version="1.0" encoding="utf-8"?>
<uk.co.senab.photoview.PhotoView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lightboxView"
android:background="@android:color/white" />
/**
* Copyright 2013 Alex Curran
*
* 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.
*
*/
package com.espian.formulae.utils;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import com.espian.formulae.lib.R;
/**
* An {@link Activity} which produces a "Lightbox"-type UI, where the entire screen bar the
* image is dimmed. Start the Activity with an {@link Intent} which has an extra with
* the key LightboxActivity.SOURCE_VALUE, which is either a Bitmap or resource id.
*
* The system is flexible should you wish to supply it with data that isn't a Bitmap or
* resource id. Overriding LightboxActivity and implementing getBitmapFromResource(..)
* allows you to supply your own logic for retrieving an image.
*
* You can optionally override shouldDimUi(), which allows the system chrome to be
* put in a low-profile mode. This is disabled by default.
*
* @author Alex Curran
*/
public class LightboxActivity extends Activity {
public static final int SOURCE_INVALID = -1;
public static final int SOURCE_RESOURCE = 0;
public static final int SOURCE_BITMAP = 2;
public static final int SOURCE_OTHER = 3;
public static final String SOURCE_TYPE = "source_type";
public static final String SOURCE_VALUE = "source_value";
public void onCreate(Bundle saved) {
super.onCreate(saved);
setContentView(R.layout.act_lightbox);
ImageView lightbox = (ImageView) findViewById(R.id.lightboxView);
Object source = getIntent().getExtras().get(SOURCE_VALUE);
int sourceType = getIntent().getIntExtra(SOURCE_TYPE, SOURCE_INVALID);
// If the developer hasn't supplied a source type, try to guess it
// with some pretty crude heuristics...
if (sourceType == SOURCE_INVALID) {
if (source == null) throw new NullPointerException("LightboxActivity has no source.");
if (source instanceof Bitmap) sourceType = SOURCE_BITMAP;
if (source instanceof Integer) sourceType = SOURCE_RESOURCE;
}
// Else continue on with figuring out what our source is
switch (sourceType) {
case SOURCE_INVALID:
throw new IllegalArgumentException("LightboxActivity not supplied with a valid source.");
case SOURCE_BITMAP:
lightbox.setImageBitmap((Bitmap) source);
break;
case SOURCE_RESOURCE:
lightbox.setImageResource((Integer) source);
break;
case SOURCE_OTHER:
lightbox.setImageBitmap(getBitmapFromSource(source));
break;
}
if (shouldDimUi()) {
lightbox.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
}
}
/**
* Used in conjuction with SOURCE_OTHER. Must be overridden in a subclass
* @param sourceValue The object extra from the intent with key "source_value"
* @return A manually generated Bitmap
*/
public Bitmap getBitmapFromSource(Object sourceValue) {
return null;
}
/**
* Determine whether the System UI should be dimmed whilst this Activity is
* shown. Override the default value in a subclass should you wish to change it
* @return
*/
public boolean shouldDimUi() {
return false;
}
}
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.Lightbox" parent="android:Theme" >
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">true</item>
</style>
</resources>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment