Android Glide加载网络的svg图片

  1. 添加依赖

implementation ‘com.github.bumptech.glide:glide:4.11.0’
annotationProcessor ‘com.github.bumptech.glide:compiler:4.11.0’
implementation ‘com.caverock:androidsvg:1.2.1’

  1. 新建SvgDecoder.java
public class SvgDecoder implements ResourceDecoder<InputStream, SVG> {

    @Override
    public boolean handles(@NonNull InputStream source, @NonNull Options options) {
        // TODO: Can we tell?
        return true;
    }

    public Resource<SVG> decode(
            @NonNull InputStream source, int width, int height, @NonNull Options options)
            throws IOException {
        try {
            SVG svg = SVG.getFromInputStream(source);
            if (width != SIZE_ORIGINAL) {
                svg.setDocumentWidth(width);
            }
            if (height != SIZE_ORIGINAL) {
                svg.setDocumentHeight(height);
            }
            return new SimpleResource<>(svg);
        } catch (SVGParseException ex) {
            throw new IOException("Cannot load SVG from stream", ex);
        }
    }
}
  1. 新建SvgDrawableTranscoder.java
public class SvgDrawableTranscoder implements ResourceTranscoder<SVG, PictureDrawable> {
  @Nullable
  @Override
  public Resource<PictureDrawable> transcode(
      @NonNull Resource<SVG> toTranscode, @NonNull Options options) {
    SVG svg = toTranscode.get();
    Picture picture = svg.renderToPicture();
    PictureDrawable drawable = new PictureDrawable(picture);
    return new SimpleResource<>(drawable);
  }
}
  1. 新建SvgModule.java
@GlideModule
public class SvgModule extends AppGlideModule {
  @Override
  public void registerComponents(
      @NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) {
    registry
        .register(SVG.class, PictureDrawable.class, new SvgDrawableTranscoder())
        .append(InputStream.class, SVG.class, new SvgDecoder());
  }

  // Disable manifest parsing to avoid adding similar modules twice.
  @Override
  public boolean isManifestParsingEnabled() {
    return false;
  }
}

  1. 新建SvgSoftwareLayerSetter.java
public class SvgSoftwareLayerSetter implements RequestListener<PictureDrawable> {

  @Override
  public boolean onLoadFailed(
      GlideException e, Object model, Target<PictureDrawable> target, boolean isFirstResource) {
    ImageView view = ((ImageViewTarget<?>) target).getView();
    view.setLayerType(ImageView.LAYER_TYPE_NONE, null);
    return false;
  }

  @Override
  public boolean onResourceReady(
      PictureDrawable resource,
      Object model,
      Target<PictureDrawable> target,
      DataSource dataSource,
      boolean isFirstResource) {
    ImageView view = ((ImageViewTarget<?>) target).getView();
    view.setLayerType(ImageView.LAYER_TYPE_SOFTWARE, null);
    return false;
  }
}
  1. activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.appcompat.widget.AppCompatImageView
        android:id="@+id/imageView"
        android:layout_width="56dp"
        android:layout_height="56dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
  1. MainActivity.class
public class MainActivity extends AppCompatActivity {

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


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

        Glide.with(this).as(PictureDrawable.class)
                .listener(new SvgSoftwareLayerSetter())
                .load("http://192.168.101.58:8080/demo/demo.svg").into(imageView);
    }
}

Android 9.0 http网络请求需要在AndroidManifest.xml中添加networkSecurityConfig

    <application
        android:networkSecurityConfig="@xml/network_security_config">
    </application>

在res目录下新建xml/network_security_config.xml文件

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true" />
</network-security-config>
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 中使用 Glide 加载网络视频,你可以按照以下步骤进行操作: 1. 首先,在你的 Android 项目中添加 Glide 的依赖。你可以在项目的 `build.gradle` 文件中的 `dependencies` 块中添加以下代码: ```groovy implementation 'com.github.bumptech.glide:glide:4.12.0' annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0' ``` 2. 确保你已经在 AndroidManifest.xml 文件中添加了网络权限: ```xml <uses-permission android:name="android.permission.INTERNET" /> ``` 3. 在你需要加载网络视频的地方,使用 Glide 的 `VideoViewTarget` 类来加载视频。首先,导入 Glide 相关的类: ```java import com.bumptech.glide.Glide; import com.bumptech.glide.request.RequestOptions; import com.bumptech.glide.request.target.Target; import com.bumptech.glide.request.target.ViewTarget; import com.bumptech.glide.request.transition.Transition; import com.bumptech.glide.request.transition.TransitionFactory; ``` 4. 然后,使用以下代码加载视频: ```java String videoUrl = "Your video URL"; Glide.with(context) .load(videoUrl) .apply(RequestOptions.noTransformation()) .into(new ViewTarget<View, Drawable>(yourVideoView) { @Override public void onResourceReady(@NonNull Drawable resource, Transition<? super Drawable> transition) { if (resource instanceof GifDrawable) { GifDrawable gifDrawable = (GifDrawable) resource; gifDrawable.setLoopCount(GifDrawable.LOOP_FOREVER); gifDrawable.start(); } else if (resource instanceof BitmapDrawable) { BitmapDrawable bitmapDrawable = (BitmapDrawable) resource; Bitmap bitmap = bitmapDrawable.getBitmap(); // Do something with the bitmap } } }); ``` 在上面的代码中,将 "Your video URL" 替换为你要加载网络视频的 URL,同时将 `yourVideoView` 替换为要显示视频的 `VideoView` 或 `SurfaceView`。 这样,使用 Glide 加载网络视频就完成了。请注意,Glide 也可以加载其他类型的图片资源和动画资源。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值