Skip to content

Commit

Permalink
Add fullscreen activity to GIF sample.
Browse files Browse the repository at this point in the history
  • Loading branch information
sjudd committed Oct 19, 2014
1 parent 1579932 commit 159f8c3
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 11 deletions.
2 changes: 2 additions & 0 deletions samples/giphy/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:largeHeap="true"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
Expand All @@ -17,6 +18,7 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".FullscreenActivity" />
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* A java wrapper for Giphy's http api based on https://github.com/Giphy/GiphyAPI.
*/
public final class Api {
private static Api api = null;
private static volatile Api api = null;
private static final String BETA_KEY = "dc6zaTOxFJmzC";
private static final String BASE_URL = "http://api.giphy.com";
private static final String SEARCH_PATH = "/v1/gifs/search";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.bumptech.glide.samples.giphy;

import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.load.resource.transcode.BitmapToGlideDrawableTranscoder;
import com.google.gson.Gson;

/**
* An {@link android.app.Activity} for displaying full size original GIFs.
*/
public class FullscreenActivity extends Activity {
private static final String EXTRA_RESULT_JSON = "result_json";

public static Intent getIntent(Context context, Api.GifResult result) {
Intent intent = new Intent(context, FullscreenActivity.class);
intent.putExtra(EXTRA_RESULT_JSON, new Gson().toJson(result));
return intent;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fullscreen_activity);

String resultJson = getIntent().getStringExtra(EXTRA_RESULT_JSON);
final Api.GifResult result = new Gson().fromJson(resultJson, Api.GifResult.class);

ImageView gifView = (ImageView) findViewById(R.id.fullscreen_gif);

gifView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("giphy_url", result.images.original.url);
clipboard.setPrimaryClip(clip);
}
});

Glide.with(this)
.load(result.images.original.url)
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.thumbnail(Glide.with(this)
.load(result)
.asBitmap()
.transcode(new BitmapToGlideDrawableTranscoder(this), GlideDrawable.class)
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
)
.into(gifView);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.content.Context;
import android.text.TextUtils;

import com.bumptech.glide.load.model.GenericLoaderFactory;
import com.bumptech.glide.load.model.ModelLoader;
import com.bumptech.glide.load.model.ModelLoaderFactory;
Expand Down Expand Up @@ -55,6 +54,5 @@ protected String getUrl(Api.GifResult model, int width, int height) {

private static int getDifference(Api.GifImage gifImage, int width, int height) {
return Math.abs(width - gifImage.width) + Math.abs(height - gifImage.height);

}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.bumptech.glide.samples.giphy;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
Expand All @@ -12,6 +13,7 @@
import com.bumptech.glide.GenericRequestBuilder;
import com.bumptech.glide.Glide;
import com.bumptech.glide.ListPreloader;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

import java.io.InputStream;
import java.util.ArrayList;
Expand All @@ -24,15 +26,14 @@ public class MainActivity extends Activity implements Api.Monitor {
private static final String TAG = "GiphyActivity";

private GifAdapter adapter;
private DrawableRequestBuilder<Api.GifResult> fullRequest;
private DrawableRequestBuilder<Api.GifResult> gifItemRequest;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Glide.get(this).register(Api.GifResult.class, InputStream.class, new GiphyModelLoader.Factory());
Api.get().getTrending();

ImageView giphyLogoView = (ImageView) findViewById(R.id.giphy_logo_view);
Glide.with(this)
Expand All @@ -43,11 +44,12 @@ protected void onCreate(Bundle savedInstanceState) {
ListView gifList = (ListView) findViewById(R.id.gif_list);
GiphyPreloader preloader = new GiphyPreloader(2);

fullRequest = Glide.with(this)
gifItemRequest = Glide.with(this)
.from(Api.GifResult.class)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.fitCenter();

adapter = new GifAdapter(this, preloader, fullRequest);
adapter = new GifAdapter(this, preloader, gifItemRequest);
gifList.setAdapter(adapter);
gifList.setOnScrollListener(preloader);
}
Expand All @@ -56,6 +58,9 @@ protected void onCreate(Bundle savedInstanceState) {
protected void onStart() {
super.onStart();
Api.get().addMonitor(this);
if (adapter.getCount() == 0) {
Api.get().getTrending();
}
}

@Override
Expand Down Expand Up @@ -93,7 +98,7 @@ protected List<Api.GifResult> getItems(int start, int end) {

@Override
protected GenericRequestBuilder getRequestBuilder(Api.GifResult item) {
return fullRequest.load(item);
return gifItemRequest.load(item);
}
}

Expand Down Expand Up @@ -143,11 +148,18 @@ public View getView(int position, View convertView, ViewGroup parent) {
convertView = activity.getLayoutInflater().inflate(R.layout.gif_list_item, parent, false);
}

Api.GifResult result = results[position];
final Api.GifResult result = results[position];
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "load result: " + result);
}
final ImageView gifView = (ImageView) convertView.findViewById(R.id.gif_view);
gifView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent fullscreenIntent = FullscreenActivity.getIntent(activity, result);
activity.startActivity(fullscreenIntent);
}
});

requestBuilder
.load(result)
Expand Down
6 changes: 6 additions & 0 deletions samples/giphy/src/main/res/layout/fullscreen_activity.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fullscreen_gif"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
3 changes: 1 addition & 2 deletions samples/giphy/src/main/res/layout/gif_list_item.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>

<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gif_view"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_height="125dp"
android:contentDescription="@string/image_description" />

0 comments on commit 159f8c3

Please sign in to comment.