-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make the SVG sample a functional app.
- Loading branch information
1 parent
e71f2b8
commit 3b3414f
Showing
24 changed files
with
490 additions
and
218 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 0 additions & 13 deletions
13
samples/svg/src/androidTest/java/com/bumptech/svgsample/app/ApplicationTest.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 103 additions & 31 deletions
134
samples/svg/src/main/java/com/bumptech/svgsample/app/MainActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
19
samples/svg/src/main/java/com/bumptech/svgsample/app/Svg.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 0 additions & 38 deletions
38
samples/svg/src/main/java/com/bumptech/svgsample/app/SvgDrawable.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.