Android 如何使用glide将相对布局的背景设置为gif
Android how to set background of relative layout as gif with glide
为了测试,我下载了下面的图片并将我的可绘制文件夹放在我的 ImageView
中,当我使用 glide 时,我可以在 activity 的中心看到它。
但是当我尝试 setBackground
时,它要么变成静态图像,要么出错
setContentView(R.layout.activity_main);
ImageView ivImg = new ImageView(this);//to hold
RelativeLayout r = (RelativeLayout) findViewById(R.id.middleRlayout);//the layout of activityt
ivImg = (ImageView) findViewById(R.id.middle);//middle is id of img
Glide.with(this)
.load(R.drawable.giphy)
.asGif()
.crossFade()
.placeholder(R.drawable.giphy);
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) {
//noinspection deprecation
r.setBackgroundDrawable(R.drawable.giphy);
} else {
r.setBackground(ivImg);
}
我针对 Mauker 的回答编辑了我的代码:
RelativeLayout r = (RelativeLayout) findViewById(R.id.middler);
ImageView ivImg = new ImageView(this);
ivImg = (ImageView) findViewById(R.id.middle);
GlideDrawableImageViewTarget imageViewTarget = new GlideDrawableImageViewTarget(ivImg);
Glide.with(this)
.load(R.drawable.giphy)
.crossFade()
.placeholder(R.drawable.giphy)
.into(imageViewTarget);
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) {
//noinspection deprecation
r.setBackgroundDrawable(R.drawable.giphy);
} else {
r.setBackground(imageViewTarget);
}
我的负载和占位符相同?并且 setbackground 不接受作为参数。
尝试使用 Glide 的 GlideDrawableImageViewTarget
。
看起来像这样:
ImageView ivImg = findViewById(R.id.imageView);
GlideDrawableImageViewTarget ivTarget = new GlideDrawableImageViewTarget(ivImg);
Glide.with(this)
.load(R.drawable.giphy) // The image you want to load
.crossFade()
.placeholder(R.raw.sample_gif) // The placeholder GIF.
.into(ivTarget);
编辑: 好的,我误读了你的问题,发现你想设置 RelativeLayout
背景。我的第一个想法是...您不能将动画 GIF 设置为背景,因为 Drawable 表示 "still image"。您可以在 Glide 的问题跟踪器上查看 this question for more information on that matter. You can also check this related issue 以获取更多信息。
第二次编辑: 解决问题的一种可能方法是将上面的代码与下面的布局结合起来:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<!-- Use this ImageView to load your background image-->
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/gray"/>
<!-- Place the rest of your UI elements here, on this inner RelativeLayout. Like the sample below -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<TextView
android:id="@+id/tv_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My sample text"
android:textColor="#ffffff"
android:layout_centerHorizontal="true" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me!"
android:layout_below="@+id/tv_text"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</RelativeLayout>
您的屏幕将如下所示:
为了测试,我下载了下面的图片并将我的可绘制文件夹放在我的 ImageView
中,当我使用 glide 时,我可以在 activity 的中心看到它。
但是当我尝试 setBackground
时,它要么变成静态图像,要么出错
setContentView(R.layout.activity_main);
ImageView ivImg = new ImageView(this);//to hold
RelativeLayout r = (RelativeLayout) findViewById(R.id.middleRlayout);//the layout of activityt
ivImg = (ImageView) findViewById(R.id.middle);//middle is id of img
Glide.with(this)
.load(R.drawable.giphy)
.asGif()
.crossFade()
.placeholder(R.drawable.giphy);
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) {
//noinspection deprecation
r.setBackgroundDrawable(R.drawable.giphy);
} else {
r.setBackground(ivImg);
}
我针对 Mauker 的回答编辑了我的代码:
RelativeLayout r = (RelativeLayout) findViewById(R.id.middler);
ImageView ivImg = new ImageView(this);
ivImg = (ImageView) findViewById(R.id.middle);
GlideDrawableImageViewTarget imageViewTarget = new GlideDrawableImageViewTarget(ivImg);
Glide.with(this)
.load(R.drawable.giphy)
.crossFade()
.placeholder(R.drawable.giphy)
.into(imageViewTarget);
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) {
//noinspection deprecation
r.setBackgroundDrawable(R.drawable.giphy);
} else {
r.setBackground(imageViewTarget);
}
我的负载和占位符相同?并且 setbackground 不接受作为参数。
尝试使用 Glide 的 GlideDrawableImageViewTarget
。
看起来像这样:
ImageView ivImg = findViewById(R.id.imageView);
GlideDrawableImageViewTarget ivTarget = new GlideDrawableImageViewTarget(ivImg);
Glide.with(this)
.load(R.drawable.giphy) // The image you want to load
.crossFade()
.placeholder(R.raw.sample_gif) // The placeholder GIF.
.into(ivTarget);
编辑: 好的,我误读了你的问题,发现你想设置 RelativeLayout
背景。我的第一个想法是...您不能将动画 GIF 设置为背景,因为 Drawable 表示 "still image"。您可以在 Glide 的问题跟踪器上查看 this question for more information on that matter. You can also check this related issue 以获取更多信息。
第二次编辑: 解决问题的一种可能方法是将上面的代码与下面的布局结合起来:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<!-- Use this ImageView to load your background image-->
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/gray"/>
<!-- Place the rest of your UI elements here, on this inner RelativeLayout. Like the sample below -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<TextView
android:id="@+id/tv_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My sample text"
android:textColor="#ffffff"
android:layout_centerHorizontal="true" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me!"
android:layout_below="@+id/tv_text"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</RelativeLayout>
您的屏幕将如下所示: