Skip to content

Commit

Permalink
Make the SVG sample a functional app.
Browse files Browse the repository at this point in the history
  • Loading branch information
TWiStErRob authored and sjudd committed Sep 2, 2014
1 parent e71f2b8 commit 3b3414f
Show file tree
Hide file tree
Showing 24 changed files with 490 additions and 218 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,17 @@ git submodule init && git submodule update
./gradlew jar
```

Note: Make sure your Android SDK has the Android Support Repository installed, and that your `$ANDROID_HOME` environment variable is pointing at the SDK.
Note: Make sure your Android SDK has the Android Support Repository installed, and that your `$ANDROID_HOME` environment variable is pointing at the SDK or add a `local.properties` file in the root project with a `sdk.dir=...` line.

Samples
-------
Follow the steps in the 'Build' section to setup the project and then:

```
gradlew :samples:flickr:run
gradlew :samples:giphy:run
gradlew :samples:svg:run
```

Development
-----------
Expand Down
5 changes: 5 additions & 0 deletions samples/flickr/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ android {
versionName '1.0.0'
}
}

task run(type: Exec, dependsOn: 'installDebug') {
description 'Installs the APK and runs the main activity: "gradlew :samples:???:run"'
commandLine "${android.sdkDirectory}/platform-tools/adb", 'shell', 'am', 'start', '-n', 'com.bumptech.glide.samples.flickr/.FlickrSearchActivity'
}
4 changes: 4 additions & 0 deletions samples/giphy/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ android {
}
}

task run(type: Exec, dependsOn: 'installDebug') {
description 'Installs the APK and runs the main activity: "gradlew :samples:???:run"'
commandLine "${android.sdkDirectory}/platform-tools/adb", 'shell', 'am', 'start', '-n', 'com.bumptech.glide.samples.giphy/.MainActivity'
}
20 changes: 9 additions & 11 deletions samples/svg/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,23 @@ repositories {

android {
compileSdkVersion 19
buildToolsVersion "19.1.0"
buildToolsVersion '19.1.0'

defaultConfig {
applicationId "com.bumptech.svgsample.app"
applicationId 'com.bumptech.svgsample.app'
minSdkVersion 10
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
versionName '1.0'
}
}

dependencies {
compile project(':library')
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:19.+'
compile 'com.caverock:androidsvg:1.2.1'
}

task run(type: Exec, dependsOn: 'installDebug') {
description 'Installs the APK and runs the main activity: "gradlew :samples:???:run"'
commandLine "${android.sdkDirectory}/platform-tools/adb", 'shell', 'am', 'start', '-n', 'com.bumptech.svgsample.app/.MainActivity'
}

This file was deleted.

11 changes: 8 additions & 3 deletions samples/svg/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bumptech.svgsample.app" >

<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:label="@string/app_name">
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

Expand Down
134 changes: 103 additions & 31 deletions samples/svg/src/main/java/com/bumptech/svgsample/app/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,56 +1,128 @@
package com.bumptech.svgsample.app;

import android.app.Activity;
import android.graphics.drawable.PictureDrawable;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.ResourceEncoder;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.model.StreamEncoder;
import com.bumptech.glide.load.resource.NullResourceEncoder;
import com.bumptech.glide.load.resource.file.FileToStreamDecoder;
import com.caverock.androidsvg.SVG;

import java.io.File;
import java.io.InputStream;

public class MainActivity extends ActionBarActivity {
/**
* Displays an SVG image loaded from an android raw resource.
*/
public class MainActivity extends Activity {
private static final String TAG = "SVGActivity";

ImageView imageViewRes;
ImageView imageViewNet;

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

ImageView imageView = (ImageView) findViewById(R.id.svg_image_view);

Glide.with(this)
.using(Glide.buildStreamModelLoader(Integer.class, this), InputStream.class)
.load(R.drawable.ic_launcher) // Some resourceId.
.as(Svg.class)
.transcode(new SvgDrawableTranscoder(), SvgDrawable.class)
.decoder(new SvgDecoder())
.encoder(new SvgEncoder())
.sourceEncoder(new StreamEncoder())
.cacheDecoder(new FileToStreamDecoder<Svg>(new SvgDecoder()))
.transform(new SvgTransformation())
.into(imageView);
imageViewRes = (ImageView) findViewById(R.id.svg_image_view1);
imageViewNet = (ImageView) findViewById(R.id.svg_image_view2);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
protected void onStart() {
super.onStart();
reload();
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
public void clearCache(View v) {
Log.w(TAG, "clearing cache");
Glide.clear(imageViewRes);
Glide.clear(imageViewNet);
Glide.get(this).clearMemory();
File cacheDir = Glide.getPhotoCacheDir(this);
if (cacheDir.isDirectory()) {
for (File child : cacheDir.listFiles()) {
if (!child.delete()) {
Log.w(TAG, "cannot delete: " + child);
}
}
}
reload();
}

public void cycleScaleType(View v) {
ImageView.ScaleType curr = imageViewRes.getScaleType();
Log.w(TAG, "cycle: current=" + curr);
ImageView.ScaleType[] all = ImageView.ScaleType.values();
int nextOrdinal = (curr.ordinal() + 1) % all.length;
ImageView.ScaleType next = all[nextOrdinal];
Log.w(TAG, "cycle: next=" + next);
imageViewRes.setScaleType(next);
imageViewNet.setScaleType(next);
reload();
}

return super.onOptionsItemSelected(item);
private void reload() {
Log.w(TAG, "reloading");
((TextView) findViewById(R.id.button)).setText(getString(R.string.scaleType, imageViewRes.getScaleType()));
loadRes();
loadNet();
}

@SuppressWarnings({ "cast", "rawtypes", "unchecked" })
private void loadRes() {
ResourceEncoder<SVG> resourceEncoder = NullResourceEncoder.get();
Glide.with(this)
.using(Glide.buildStreamModelLoader(Integer.class, this), InputStream.class)
.load(R.raw.android_toy_h)
.as(SVG.class)
.transcode(new SvgDrawableTranscoder(), PictureDrawable.class)
.diskCacheStrategy(DiskCacheStrategy.NONE)
// SVG cannot be serialized so it's not worth to cache it
// and the getResources() should be fast enough when acquiring the InputStream
.encoder(resourceEncoder)
// not used
.sourceEncoder(new StreamEncoder())
// not used
.cacheDecoder(new FileToStreamDecoder<SVG>(new SvgDecoder()))
// not used
.decoder(new SvgDecoder())
.placeholder(R.drawable.image_loading)
.error(R.drawable.image_error)
.animate(android.R.anim.fade_in)
.listener(new SvgSoftwareLayerSetter<Integer>())
.into(imageViewRes);
}

@SuppressWarnings({ "cast", "rawtypes", "unchecked" })
private void loadNet() {
ResourceEncoder<SVG> resourceEncoder = NullResourceEncoder.get();
Glide.with(this)
.using(Glide.buildStreamModelLoader(String.class, this), InputStream.class)
.load("http://www.clker.com/cliparts/u/Z/2/b/a/6/android-toy-h.svg")
.as(SVG.class)
.transcode(new SvgDrawableTranscoder(), PictureDrawable.class)
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
// SVG cannot be serialized so it's not worth to cache it
.sourceEncoder(new StreamEncoder())
// however loading from the network can be cached via StreamEncoder
.cacheDecoder(new FileToStreamDecoder<SVG>(new SvgDecoder()))
.decoder(new SvgDecoder())
.encoder(resourceEncoder)
// not used
.placeholder(R.drawable.image_loading)
.error(R.drawable.image_error)
.animate(android.R.anim.fade_in)
.listener(new SvgSoftwareLayerSetter<String>())
.into(imageViewNet);
}
}
19 changes: 0 additions & 19 deletions samples/svg/src/main/java/com/bumptech/svgsample/app/Svg.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,27 @@

import com.bumptech.glide.load.ResourceDecoder;
import com.bumptech.glide.load.engine.Resource;
import com.caverock.androidsvg.SVG;
import com.caverock.androidsvg.SVGParseException;

import java.io.IOException;
import java.io.InputStream;

public class SvgDecoder implements ResourceDecoder<InputStream,Svg> {

@Override
public Resource<Svg> decode(InputStream source, int width, int height) throws IOException {
return new SvgResource(Svg.fromStream(source));
/**
* Decodes an SVG internal representation from an {@link InputStream}.
*/
public class SvgDecoder implements ResourceDecoder<InputStream, SVG> {
public Resource<SVG> decode(InputStream source, int width, int height) throws IOException {
try {
SVG svg = SVG.getFromInputStream(source);
return new SvgResource(svg);
} catch (SVGParseException ex) {
throw new IOException("Cannot load SVG from stream", ex);
}
}

@Override
public String getId() {
return ""; // Or if you're planning on have multiple give it some unique id.
return "SvgDecoder.com.bumptech.svgsample.app";
}
}

This file was deleted.

Loading

0 comments on commit 3b3414f

Please sign in to comment.