Skip to content

Instantly share code, notes, and snippets.

@CoffeeCode
Created May 26, 2014 20:00
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 CoffeeCode/acaa096470a71a29ebe2 to your computer and use it in GitHub Desktop.
Save CoffeeCode/acaa096470a71a29ebe2 to your computer and use it in GitHub Desktop.
public class Viewer extends Fragment {
private static final String TAG = "Viewer";
//Fragment Static strings
public static final String IMAGE_RESOURCE_ID = "iconResourceID";
public static final String ITEM_NAME = "itemName";
//Static strings for Bundel and etc.
public static final String PREFERENCE_LAST_IMAGE = "lastImage";
public static final String BUNDEL_SELECTED_IMAGE = "selectedImage";
protected String imagePath;
protected Button filter1; //Laplace intrinsics
protected Button filter2; //Gauss intrinsics
protected Button filter3; //SharpenMask
protected Button filter4; //Laplace Renderscript
protected TextView textViewExif;
protected TextView textViewInfo1;
protected TextView textViewInfo2;
protected RenderScript renderScript;
protected SubsamplingScaleImageView imageView;
protected Context context;
public static Viewer init(String imagePath) {
Viewer viewerFragment = new Viewer();
Bundle args = new Bundle();
args.putString("imagePath", imagePath);
viewerFragment.setArguments(args);
return viewerFragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
imagePath = getArguments().getString("imagePath");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_viewer, container, false);
context = super.getActivity();
renderScript = RenderScript.create(context);
loadUi(view);
try {
imageView.setImageFile(imagePath);
Log.d(TAG, "setting imagePath in onCreateView : " + imagePath);
textViewExif.setText(imagePath);
} catch (IOException e) {
e.printStackTrace();
}
initializeInheritance(view); //nur für klassen die diese vererben
toggleHideyBar();
return view;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.viewer, menu);
super.onCreateOptionsMenu(menu,inflater);
}
protected void initializeInheritance(View view){
}
protected void showMetadata(ImageData imageData){
textViewExif.setText(imageData.getExposureTime() + " " +
imageData.getfNumber() + " " +
imageData.getFilmSpeed() + " " +
imageData.getFocalLength());
textViewInfo1.setText(String.valueOf(imageData.getLaplaceSum()));
textViewInfo2.setText(String.valueOf(imageData.getMaxLaplaceValue()));
}
protected void loadUi(View view){
setHasOptionsMenu(true);
imageView = (SubsamplingScaleImageView) view.findViewById(R.id.previewImage);
imageView = (SubsamplingScaleImageView) view.findViewById(R.id.previewImage);
textViewExif = (TextView) view.findViewById(R.id.textViewExif);
textViewInfo1 = (TextView) view.findViewById(R.id.textViewInfo1);
textViewInfo2 = (TextView) view.findViewById(R.id.textViewInfo2);
filter1 = (Button) view.findViewById(R.id.BtnFilter1);
filter1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Bitmap image = BitmapFactory.decodeFile(imagePath);
image = image.createScaledBitmap(image, image.getWidth()/2, image.getHeight()/2, true);
final Bitmap filteredImage = BitmapEffects.getLapaceFilter(renderScript).apply(image);
setImageBitmap(filteredImage);
}
});
filter2 = (Button) view.findViewById(R.id.BtnFilter2);
filter2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Bitmap image = BitmapFactory.decodeFile(imagePath);
final Bitmap filteredImage = BitmapEffects.getBlurryEffect(5, renderScript).apply(image);
setImageBitmap(filteredImage);
}
});
filter3 = (Button) view.findViewById(R.id.BtnFilter3);
filter3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d(TAG, "imagePath : " + imagePath);
File imageFile = new File(imagePath);
File filteredImageFile = BitmapEffects.getSharpFinderEffect(renderScript, context).applyFile(imageFile);
setImageFile(filteredImageFile);
}
});
filter4 = (Button) view.findViewById(R.id.BtnFilter4);
filter4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
File imageFile = new File(imagePath);
File filteredImageFile = BitmapEffects.getConvolution(renderScript).applyFile(imageFile);
setImageFile(filteredImageFile);
}
});
}
@Override
public void onDetach() {
super.onDetach();
toggleHideyBar();
}
/**
* Detects and toggles immersive mode (also known as "hidey bar" mode).
*/
public void toggleHideyBar() {
// The UI options currently enabled are represented by a bitfield.
// getSystemUiVisibility() gives us that bitfield.
int uiOptions = super.getActivity().getWindow().getDecorView().getSystemUiVisibility();
int newUiOptions = uiOptions;
boolean isImmersiveModeEnabled =
((uiOptions | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY) == uiOptions);
if (isImmersiveModeEnabled) {
Log.i(TAG, "Turning immersive mode mode off. ");
} else {
Log.i(TAG, "Turning immersive mode mode on.");
}
// Navigation bar hiding: Backwards compatible to ICS.
if (Build.VERSION.SDK_INT >= 14) {
newUiOptions ^= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
}
// Status bar hiding: Backwards compatible to Jellybean
if (Build.VERSION.SDK_INT >= 16) {
newUiOptions ^= View.SYSTEM_UI_FLAG_FULLSCREEN;
}
// Immersive mode: Backward compatible to KitKat.
// Note that this flag doesn't do anything by itself, it only augments the behavior
// of HIDE_NAVIGATION and FLAG_FULLSCREEN. For the purposes of this sample
// all three flags are being toggled together.
// Note that there are two immersive mode UI flags, one of which is referred to as "sticky".
// Sticky immersive mode differs in that it makes the navigation and status bars
// semi-transparent, and the UI flag does not get cleared when the user interacts with
// the screen.
if (Build.VERSION.SDK_INT >= 18) {
newUiOptions ^= View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
}
super.getActivity().getWindow().getDecorView().setSystemUiVisibility(newUiOptions);
}
protected void setImageFile(File filteredImageFile){
try {
imageView.setImageFile(filteredImageFile.getAbsolutePath());
} catch (IOException e) { e.printStackTrace(); }
}
protected void setImageBitmap(Bitmap filteredImageBitmap){
try {
imageView.setImageBitmap(filteredImageBitmap);
} catch (IOException e) { e.printStackTrace(); }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment