Skip to content
This repository has been archived by the owner on Aug 10, 2023. It is now read-only.

Commit

Permalink
Merge pull request #3 from qoqa/wip-2
Browse files Browse the repository at this point in the history
Remove useless SvgSoftwareLayerSetter #2
  • Loading branch information
Kevinrob authored Jan 8, 2018
2 parents 9b6bce9 + e74e46a commit ceb11fa
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 78 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ dependencies {
## Usage
```kotlin
GlideApp.with(this)
.asSvg()
.load(url)
.into(picto_view)
```
20 changes: 0 additions & 20 deletions library/src/main/java/ch/qoqa/glide/svg/GlideSvgExtension.kt

This file was deleted.

29 changes: 29 additions & 0 deletions library/src/main/java/ch/qoqa/glide/svg/SvgBitmapTranscoder.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package ch.qoqa.glide.svg

import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.drawable.PictureDrawable
import com.bumptech.glide.load.Options
import com.bumptech.glide.load.engine.Resource
import com.bumptech.glide.load.resource.SimpleResource
import com.bumptech.glide.load.resource.transcode.ResourceTranscoder
import com.caverock.androidsvg.SVG

class SvgBitmapTranscoder : ResourceTranscoder<SVG, Bitmap> {
override fun transcode(toTranscode: Resource<SVG>, options: Options): Resource<Bitmap> {
val svg = toTranscode.get()
val width = svg.documentWidth.toInt().takeIf { it > 0 }
?: (svg.documentViewBox.right - svg.documentViewBox.left).toInt()
val height = svg.documentHeight.toInt().takeIf { it > 0 }
?: (svg.documentViewBox.bottom - svg.documentViewBox.top).toInt()

val picture = svg.renderToPicture(width, height)
val drawable = PictureDrawable(picture)

val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
canvas.drawPicture(drawable.picture)

return SimpleResource(bitmap)
}
}
25 changes: 8 additions & 17 deletions library/src/main/java/ch/qoqa/glide/svg/SvgDrawableTranscoder.kt
Original file line number Diff line number Diff line change
@@ -1,29 +1,20 @@
package ch.qoqa.glide.svg

import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.drawable.PictureDrawable
import android.content.Context
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import com.bumptech.glide.load.Options
import com.bumptech.glide.load.engine.Resource
import com.bumptech.glide.load.resource.SimpleResource
import com.bumptech.glide.load.resource.transcode.ResourceTranscoder
import com.caverock.androidsvg.SVG

class SvgDrawableTranscoder : ResourceTranscoder<SVG, Bitmap> {
override fun transcode(toTranscode: Resource<SVG>, options: Options): Resource<Bitmap> {
val svg = toTranscode.get()
val width = svg.documentWidth.toInt().takeIf { it > 0 }
?: (svg.documentViewBox.right - svg.documentViewBox.left).toInt()
val height = svg.documentHeight.toInt().takeIf { it > 0 }
?: (svg.documentViewBox.bottom - svg.documentViewBox.top).toInt()
class SvgDrawableTranscoder(private val context: Context) : ResourceTranscoder<SVG, Drawable> {
private val bitmapTranscoder = SvgBitmapTranscoder()

val picture = svg.renderToPicture(width, height)
val drawable = PictureDrawable(picture)
override fun transcode(toTranscode: Resource<SVG>, options: Options): Resource<Drawable> {
val bitmap = bitmapTranscoder.transcode(toTranscode, options).get()

val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
canvas.drawPicture(drawable.picture)

return SimpleResource(bitmap)
return SimpleResource(BitmapDrawable(context.resources, bitmap))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ch.qoqa.glide.svg

import android.content.Context
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import com.bumptech.glide.Glide
import com.bumptech.glide.Registry
import com.bumptech.glide.annotation.GlideModule
Expand All @@ -12,7 +13,8 @@ import java.io.InputStream
@GlideModule
class SvgLibraryGlideModule : LibraryGlideModule() {
override fun registerComponents(context: Context, glide: Glide, registry: Registry) {
registry.register(SVG::class.java, Bitmap::class.java, SvgDrawableTranscoder())
registry.register(SVG::class.java, Drawable::class.java, SvgDrawableTranscoder(context))
.register(SVG::class.java, Bitmap::class.java, SvgBitmapTranscoder())
.append(InputStream::class.java, SVG::class.java, SvgDecoder())
}
}
29 changes: 0 additions & 29 deletions library/src/main/java/ch/qoqa/glide/svg/SvgSoftwareLayerSetter.kt

This file was deleted.

42 changes: 37 additions & 5 deletions sample/src/main/java/ch/qoqa/sample/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package ch.qoqa.sample

import android.annotation.SuppressLint
import android.graphics.Color
import android.graphics.PorterDuff
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageView
import kotlinx.android.synthetic.main.main_activity.*

class MainActivity : AppCompatActivity() {
Expand All @@ -13,11 +18,38 @@ class MainActivity : AppCompatActivity() {

setContentView(R.layout.main_activity)

sample_image_view.setColorFilter(Color.BLUE, PorterDuff.Mode.SRC_ATOP)
// TODO add example with bitmap
val images = listOf(
Images {
GlideApp.with(this)
.load("https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/android.svg?1")
.into(it)
},
Images {
it.setColorFilter(Color.BLUE, PorterDuff.Mode.SRC_ATOP)
GlideApp.with(this)
.load("https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/android.svg?2")
.into(it)
}
)

sample_images.adapter = object : BaseAdapter() {
override fun getItem(position: Int) = images[position]
override fun getItemId(position: Int) = position.toLong()
override fun getCount() = images.size

@SuppressLint("ViewHolder")
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View? {
val view = layoutInflater.inflate(R.layout.item_image, parent, false) ?: return null

getItem(position).block(view.findViewById(R.id.sample_image_view))

return view
}
}


GlideApp.with(this)
.asSvg()
.load("https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/android.svg")
.into(sample_image_view)
}

data class Images(val block: (ImageView) -> Unit)
}
6 changes: 6 additions & 0 deletions sample/src/main/res/layout/item_image.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/sample_image_view"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerInParent="true" />
10 changes: 5 additions & 5 deletions sample/src/main/res/layout/main_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/sample_image_view"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerInParent="true"/>

<ListView
android:id="@+id/sample_images"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>

0 comments on commit ceb11fa

Please sign in to comment.