Skip to content

Commit cc552d1

Browse files
committed
展示svg图片
1 parent 432b233 commit cc552d1

File tree

10 files changed

+262
-1
lines changed

10 files changed

+262
-1
lines changed

app/build.gradle

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,24 @@ dependencies {
5656
implementation viewDependencies["pickerview"]
5757

5858
implementation utilsDependencies["multidex"]
59-
annotationProcessor compilerDependencies["butterknife-compiler"]
59+
implementation 'androidx.appcompat:appcompat:1.1.0'
60+
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
6061
implementation("com.alibaba.android:vlayout:${vlayout}@aar") {
6162
transitive = true
6263
}
6364
implementation tencentDependencies["crashreport_upgrade"]
6465
// implementation 'com.tencent.tinker:tinker-android-lib:1.9.9'
66+
6567
implementation imageDependencies["matisse"]
68+
implementation imageDependencies["androidsvg"]
6669

6770
implementation project(':imageloader')
6871
implementation project(':quicklib')
6972
implementation project(':paylib')
7073
implementation project(':widget')
74+
75+
annotationProcessor compilerDependencies["glide-compiler"]
76+
annotationProcessor compilerDependencies["butterknife-compiler"]
7177
}
7278

7379
//apply from: "tinker_config.gradle"

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
android:supportsRtl="true"
3333
android:theme="@style/AppTheme"
3434
tools:ignore="GoogleAppIndexingWarning">
35+
<activity android:name=".ui.ShowSvgActivity"></activity>
3536
<!-- FileProvider文件共享 -->
3637
<provider
3738
android:name="androidx.core.content.FileProvider"

app/src/main/java/com/sdwfqin/quickseed/ui/MainActivity.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public class MainActivity extends BaseActivity {
3939
"跑马灯Demo",
4040
"Camerax",
4141
"VLayoutDemo",
42+
"展示SVG图片",
4243
};
4344

4445
@Override
@@ -124,6 +125,9 @@ public void right() {
124125
case 8:
125126
startActivity(new Intent(mContext, VLayoutSampleActivity.class));
126127
break;
128+
case 9:
129+
startActivity(new Intent(mContext, ShowSvgActivity.class));
130+
break;
127131
default:
128132
}
129133
});
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package com.sdwfqin.quickseed.ui;
2+
3+
import android.graphics.drawable.PictureDrawable;
4+
import android.widget.ImageView;
5+
6+
import com.bumptech.glide.RequestBuilder;
7+
import com.sdwfqin.quicklib.base.BaseActivity;
8+
import com.sdwfqin.quickseed.R;
9+
import com.sdwfqin.quickseed.utils.svg.GlideApp;
10+
import com.sdwfqin.quickseed.utils.svg.SvgSoftwareLayerSetter;
11+
12+
import butterknife.BindView;
13+
14+
import static com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions.withCrossFade;
15+
16+
/**
17+
* 展示Svg图片
18+
* <p>
19+
*
20+
* @author 张钦
21+
* @date 2019-10-29
22+
*/
23+
public class ShowSvgActivity extends BaseActivity {
24+
25+
@BindView(R.id.img)
26+
ImageView mImg;
27+
28+
private String mImgRes =
29+
"<?xml version=\"1.0\" standalone=\"no\"?>\n" +
30+
"<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n" +
31+
"<svg version=\"1.1\" id=\"图层_1\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" x=\"0px\" y=\"0px\" viewBox=\"0 0 800 442.7\" style=\"enable-background:new 0 0 800 442.7;\" xml:space=\"preserve\">\n" +
32+
"<defs>\n" +
33+
"<style type=\"text/css\">\n" +
34+
"\t.st0{fill:#FF0000;stroke:#000000;stroke-miterlimit:10;}\n" +
35+
"\t.st1{fill:#FFFFFF;stroke:#000000;stroke-miterlimit:10;}\n" +
36+
"\t.st2{font-family:'AdobeSongStd-Light-GBpc-EUC-H';}\n" +
37+
"\t.st3{font-size:14px;}\n" +
38+
"</style>\n" +
39+
"</defs>\n" +
40+
"<g fill=\"#000000\">\n" +
41+
"\t<rect x=\"21.6\" y=\"29.3\" class=\"st0\" width=\"126.9\" height=\"53.8\"/>\n" +
42+
"\t<rect x=\"148.4\" y=\"29.3\" class=\"st1\" width=\"126.9\" height=\"53.8\"/>\n" +
43+
"\t<rect x=\"274.5\" y=\"29.3\" class=\"st1\" width=\"126.9\" height=\"53.8\"/>\n" +
44+
"\t<rect x=\"480.6\" y=\"29.3\" class=\"st1\" width=\"126.9\" height=\"53.8\"/>\n" +
45+
"\t<rect x=\"633.9\" y=\"29.3\" class=\"st1\" width=\"126.9\" height=\"53.8\"/>\n" +
46+
"\t<rect x=\"21.6\" y=\"112.5\" class=\"st1\" width=\"199.2\" height=\"53.8\"/>\n" +
47+
"\t<rect x=\"21.6\" y=\"166.3\" class=\"st1\" width=\"199.2\" height=\"53.8\"/>\n" +
48+
"\t<rect x=\"21.6\" y=\"220.1\" class=\"st1\" width=\"199.2\" height=\"53.7\"/>\n" +
49+
"\t<rect x=\"21.6\" y=\"273.8\" class=\"st1\" width=\"199.2\" height=\"53.8\"/>\n" +
50+
"\t<rect x=\"244.3\" y=\"112.5\" class=\"st1\" width=\"141.2\" height=\"215\"/>\n" +
51+
"\t<rect x=\"409.1\" y=\"112.5\" class=\"st1\" width=\"199.2\" height=\"96.3\"/>\n" +
52+
"\t<rect x=\"409.1\" y=\"231.3\" class=\"st1\" width=\"199.2\" height=\"96.3\"/>\n" +
53+
"\t<rect x=\"633.1\" y=\"112.9\" class=\"st1\" width=\"126.9\" height=\"95.9\"/>\n" +
54+
"\t<rect x=\"633.1\" y=\"231.7\" class=\"st1\" width=\"126.9\" height=\"95.9\"/>\n" +
55+
"\t<rect x=\"21.6\" y=\"359.5\" class=\"st1\" width=\"199.2\" height=\"53.8\"/>\n" +
56+
"\t<rect x=\"244.4\" y=\"359.5\" class=\"st1\" width=\"343.7\" height=\"53.8\"/>\n" +
57+
"</g>\n" +
58+
"<g id=\"文字图层\">\n" +
59+
"\t<text transform=\"matrix(1 0 0 1 653.7477 62.8554)\" class=\"st2 st3\">危险化学品仓库</text>\n" +
60+
"\t<text transform=\"matrix(1 0 0 1 86.2301 148.7154)\" class=\"st2 st3\">生产车间一</text>\n" +
61+
"\t<text transform=\"matrix(1 0 0 1 86.2301 201.7154)\" class=\"st2 st3\">生产车间二</text>\n" +
62+
"\t<text transform=\"matrix(1 0 0 1 86.2301 254.7154)\" class=\"st2 st3\">生产车间三</text>\n" +
63+
"\t<text transform=\"matrix(1 0 0 1 86.2301 308.7154)\" class=\"st2 st3\">生产车间四</text>\n" +
64+
"\t<text transform=\"matrix(1 0 0 1 279.8901 221.3546)\" class=\"st2 st3\">物料存放区</text>\n" +
65+
"\t<text transform=\"matrix(1 0 0 1 100.2301 391.7154)\" class=\"st2 st3\">门卫室</text>\n" +
66+
"\t<text transform=\"matrix(1 0 0 1 402.1901 391.715)\" class=\"st2 st3\">花坛</text>\n" +
67+
"\t<text transform=\"matrix(1 0 0 1 480.6501 166.2996)\" class=\"st2 st3\">研发大楼</text>\n" +
68+
"\t<text transform=\"matrix(1 0 0 1 487.6502 279.6246)\" class=\"st2 st3\">办公楼</text>\n" +
69+
"\t<text transform=\"matrix(1 0 0 1 675.5451 166.2996)\" class=\"st2 st3\">停车场</text>\n" +
70+
"\t<text transform=\"matrix(1 0 0 1 682.5451 284.7154)\" class=\"st2 st3\">食堂</text>\n" +
71+
"\t<text transform=\"matrix(1 0 0 1 65.7729 63.0264)\" class=\"st2 st3\">配电房</text>\n" +
72+
"\t<text transform=\"matrix(1 0 0 1 188.7477 63.4633)\" class=\"st2 st3\">空压机房</text>\n" +
73+
"\t<text transform=\"matrix(1 0 0 1 307.7477 65.4304)\" class=\"st2 st3\">维修车间</text>\n" +
74+
"\t<text transform=\"matrix(1 0 0 1 518.7477 63.7154)\" class=\"st2 st3\">成品仓库</text>\n" +
75+
"</g>\n" +
76+
"</svg>";
77+
78+
@Override
79+
protected int getLayout() {
80+
return R.layout.activity_show_svg;
81+
}
82+
83+
@Override
84+
protected void initEventAndData() {
85+
86+
mTopBar.setTitle("展示SVG图片");
87+
mTopBar.addLeftBackImageButton()
88+
.setOnClickListener(v -> finish());
89+
90+
RequestBuilder<PictureDrawable> requestBuilder =
91+
GlideApp.with(this)
92+
.as(PictureDrawable.class)
93+
.transition(withCrossFade())
94+
.listener(new SvgSoftwareLayerSetter());
95+
requestBuilder
96+
.load(mImgRes.getBytes())
97+
.into(mImg);
98+
99+
}
100+
101+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.sdwfqin.quickseed.utils.svg;
2+
3+
import androidx.annotation.NonNull;
4+
5+
import com.bumptech.glide.load.Options;
6+
import com.bumptech.glide.load.ResourceDecoder;
7+
import com.bumptech.glide.load.engine.Resource;
8+
import com.bumptech.glide.load.resource.SimpleResource;
9+
import com.caverock.androidsvg.SVG;
10+
import com.caverock.androidsvg.SVGParseException;
11+
12+
import java.io.IOException;
13+
import java.io.InputStream;
14+
15+
/**
16+
* Decodes an SVG internal representation from an {@link InputStream}.
17+
*/
18+
public class SvgDecoder implements ResourceDecoder<InputStream, SVG> {
19+
20+
@Override
21+
public boolean handles(@NonNull InputStream source, @NonNull Options options) {
22+
// TODO: Can we tell?
23+
return true;
24+
}
25+
26+
public Resource<SVG> decode(
27+
@NonNull InputStream source, int width, int height, @NonNull Options options)
28+
throws IOException {
29+
try {
30+
SVG svg = SVG.getFromInputStream(source);
31+
return new SimpleResource<>(svg);
32+
} catch (SVGParseException ex) {
33+
throw new IOException("Cannot load SVG from stream", ex);
34+
}
35+
}
36+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.sdwfqin.quickseed.utils.svg;
2+
3+
import android.graphics.Picture;
4+
import android.graphics.drawable.PictureDrawable;
5+
6+
import androidx.annotation.NonNull;
7+
import androidx.annotation.Nullable;
8+
9+
import com.bumptech.glide.load.Options;
10+
import com.bumptech.glide.load.engine.Resource;
11+
import com.bumptech.glide.load.resource.SimpleResource;
12+
import com.bumptech.glide.load.resource.transcode.ResourceTranscoder;
13+
import com.caverock.androidsvg.SVG;
14+
15+
/**
16+
* Convert the {@link SVG}'s internal representation to an Android-compatible one ({@link Picture}).
17+
*/
18+
public class SvgDrawableTranscoder implements ResourceTranscoder<SVG, PictureDrawable> {
19+
@Nullable
20+
@Override
21+
public Resource<PictureDrawable> transcode(
22+
@NonNull Resource<SVG> toTranscode, @NonNull Options options) {
23+
SVG svg = toTranscode.get();
24+
Picture picture = svg.renderToPicture();
25+
PictureDrawable drawable = new PictureDrawable(picture);
26+
return new SimpleResource<>(drawable);
27+
}
28+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.sdwfqin.quickseed.utils.svg;
2+
3+
import android.content.Context;
4+
import android.graphics.drawable.PictureDrawable;
5+
6+
import androidx.annotation.NonNull;
7+
8+
import com.bumptech.glide.Glide;
9+
import com.bumptech.glide.Registry;
10+
import com.bumptech.glide.annotation.GlideModule;
11+
import com.bumptech.glide.module.AppGlideModule;
12+
import com.caverock.androidsvg.SVG;
13+
14+
import java.io.InputStream;
15+
16+
@GlideModule
17+
public class SvgModule extends AppGlideModule {
18+
@Override
19+
public void registerComponents(@NonNull Context context, @NonNull Glide glide,
20+
@NonNull Registry registry) {
21+
registry.register(SVG.class, PictureDrawable.class, new SvgDrawableTranscoder())
22+
.append(InputStream.class, SVG.class, new SvgDecoder());
23+
}
24+
25+
// Disable manifest parsing to avoid adding similar modules twice.
26+
@Override
27+
public boolean isManifestParsingEnabled() {
28+
return false;
29+
}
30+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.sdwfqin.quickseed.utils.svg;
2+
3+
import android.graphics.drawable.PictureDrawable;
4+
import android.widget.ImageView;
5+
6+
import com.bumptech.glide.load.DataSource;
7+
import com.bumptech.glide.load.engine.GlideException;
8+
import com.bumptech.glide.request.RequestListener;
9+
import com.bumptech.glide.request.target.ImageViewTarget;
10+
import com.bumptech.glide.request.target.Target;
11+
12+
/**
13+
* Listener which updates the {@link ImageView} to be software rendered, because {@link
14+
* com.caverock.androidsvg.SVG SVG}/{@link android.graphics.Picture Picture} can't render on a
15+
* hardware backed {@link android.graphics.Canvas Canvas}.
16+
*/
17+
public class SvgSoftwareLayerSetter implements RequestListener<PictureDrawable> {
18+
19+
@Override
20+
public boolean onLoadFailed(
21+
GlideException e, Object model, Target<PictureDrawable> target, boolean isFirstResource) {
22+
ImageView view = ((ImageViewTarget<?>) target).getView();
23+
view.setLayerType(ImageView.LAYER_TYPE_NONE, null);
24+
return false;
25+
}
26+
27+
@Override
28+
public boolean onResourceReady(
29+
PictureDrawable resource,
30+
Object model,
31+
Target<PictureDrawable> target,
32+
DataSource dataSource,
33+
boolean isFirstResource) {
34+
ImageView view = ((ImageViewTarget<?>) target).getView();
35+
view.setLayerType(ImageView.LAYER_TYPE_SOFTWARE, null);
36+
return false;
37+
}
38+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
android:layout_width="match_parent"
5+
android:layout_height="match_parent"
6+
android:background="@color/white"
7+
android:orientation="vertical"
8+
tools:context=".ui.ShowSvgActivity">
9+
10+
<ImageView
11+
android:id="@+id/img"
12+
android:layout_width="match_parent"
13+
android:layout_height="300dp"
14+
android:scaleType="centerCrop"/>
15+
16+
</androidx.appcompat.widget.LinearLayoutCompat>

config.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ ext {
5757
"photoview" : "com.github.chrisbanes:PhotoView:2.3.0",
5858
// matisse
5959
"matisse" : "com.zhihu.android:matisse:0.5.3-beta2",
60+
"androidsvg" : "com.caverock:androidsvg-aar:1.4",
6061
]
6162

6263
utilsDependencies = [

0 commit comments

Comments
 (0)