{"id":721,"date":"2023-02-26T15:27:55","date_gmt":"2023-02-26T07:27:55","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=721"},"modified":"2023-04-29T17:14:27","modified_gmt":"2023-04-29T09:14:27","slug":"android-use-glide-to-load-view-background-image","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/26\/android-use-glide-to-load-view-background-image\/","title":{"rendered":"Android\u4f7f\u7528Glide\u52a0\u8f7dView\u80cc\u666f\u56fe\u7247"},"content":{"rendered":"<p>\u4e00\u822c\u5e38\u7528Glide\u52a0\u8f7d\u56fe\u7247\u5230ImageView\u4e0a\uff0c\u5982\u4f55\u52a0\u8f7d\u5230View\u53caLayout\u7684\u80cc\u666f\u4e0a\u5462\uff1f<\/p>\n<h2>Glide 4 \u53ca\u4ee5\u4e0a<\/h2>\n<p><!-- more --><\/p>\n<pre><code class=\"language-java\">public static void loadViewBackground(String url, final View view) {\n    SimpleTarget&lt;Drawable&gt; simpleTarget = new SimpleTarget&lt;Drawable&gt;() {\n        @Override\n        public void onResourceReady(@NonNull Drawable resource, Transition&lt;? super Drawable&gt; transition) {\n            if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.JELLY_BEAN) {\n                view.setBackground(resource);\n            }\n        }\n    };\n    GlideApp.with(view.getContext()).load(url).into(simpleTarget);\n}<\/code><\/pre>\n<h2>\u5e38\u7528\u65b9\u5f0f<\/h2>\n<p>\u7b2c\u4e00\u79cd\u65b9\u5f0f\uff1a<\/p>\n<pre><code class=\"language-java\">Glide.with(context)\n    .load(url)\n    .asBitmap()\n    .into(new SimpleTarget&lt;Bitmap&gt;(180, 180) { \/\/\u56fe\u7247\u5bbd\u9ad8\n        @Override\n        public void onResourceReady(Bitmap resource, GlideAnimation&lt;? super Bitmap&gt; glideAnimation) {\n        Drawable drawable = new BitmapDrawable(resource);\n            if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.JELLY_BEAN) {\n                layout.setBackground(drawable); \/\/\u8bbe\u7f6e\u80cc\u666f\n            }\n        }\n    });<\/code><\/pre>\n<p>\u8be5\u65b9\u5f0f\u6309\u7167\u8bbe\u7f6e\u7684\u5bbd\u9ad8\u53bb\u622a\u53d6\u56fe\u7247\uff0c\u518d\u53bb\u52a0\u8f7d\u5230\u63a7\u4ef6\u80cc\u666f\u4e0a\uff0c\u6709\u65f6\u4f1a\u5bfc\u81f4\u56fe\u7247\u663e\u793a\u4e0d\u5168<\/p>\n<p>\u7b2c\u4e8c\u79cd\u65b9\u5f0f\uff1a<\/p>\n<pre><code class=\"language-java\">Glide.with(context)\n    .load(url)\n    .into(new ViewTarget&lt;View, GlideDrawable&gt;(layout) { \/\/\u62ec\u53f7\u91cc\u4e3a\u9700\u8981\u52a0\u8f7d\u7684\u63a7\u4ef6\n        @Override\n        public void onResourceReady(GlideDrawable resource, GlideAnimation&lt;? super GlideDrawable&gt; glideAnimation) {\n            this.view.setBackground(resource.getCurrent());\n        }\n    });<\/code><\/pre>\n<p>\u8be5\u65b9\u5f0f\u5219\u5b8c\u7f8e\u52a0\u8f7d\u56fe\u7247<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u822c\u5e38\u7528Glide\u52a0\u8f7d\u56fe\u7247\u5230ImageView\u4e0a\uff0c\u5982\u4f55\u52a0\u8f7d\u5230View\u53caLayout\u7684\u80cc\u666f\u4e0a\u5462\uff1f Glide 4 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[206],"tags":[209],"class_list":["post-721","post","type-post","status-publish","format-standard","hentry","category-android-image-loader","tag-glide"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/721","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=721"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/721\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=721"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=721"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=721"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}