gpt4 book ai didi

android - 以编程方式将图像加载到图层列表中

转载 作者:行者123 更新时间:2023-11-29 17:17:32 30 4
gpt4 key购买 nike

我正在尝试以编程方式将图像加载到图层列表中。我找到了一些解决方案,但不知何故它不起作用。这是我所做的:

layer_list.xml:(设置为RelativeLayout的背景)

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/red" />
<item
android:id="@+id/replace"
android:drawable="@drawable/placeholder" />
<item
android:bottom="-250dp"
android:left="0dp"
android:right="-250dp"
android:top="250dp">
<rotate
android:fromDegrees="-10"
android:pivotX="0%"
android:pivotY="100%">
<shape android:shape="rectangle">
<solid android:color="@color/white" />
</shape>
</rotate>
</item>
</layer-list>

将位图加载到图层列表的代码:

Drawable drawable = new BitmapDrawable(getResources(), bitmap);
LayerDrawable layerDrawable = (LayerDrawable) ResourcesCompat.getDrawable(getResources(), R.drawable.layer_list, null);
layerDrawable.setDrawableByLayerId(R.id.replace, drawable);

但是占位符图像永远不会被新位图替换。我在这里做错了什么?

编辑:这是我加载图像的方式:

    private void loadImageIntoLayerList(Context context, String url) {
// To get image using Fresco
ImageRequest imageRequest = ImageRequestBuilder
.newBuilderWithSource(Uri.parse(url))
.setProgressiveRenderingEnabled(true)
.build();

ImagePipeline imagePipeline = Fresco.getImagePipeline();
DataSource<CloseableReference<CloseableImage>> dataSource = imagePipeline
.fetchDecodedImage(imageRequest, context);

BaseBitmapDataSubscriber dataSubscriber = new BaseBitmapDataSubscriber() {
@Override
public void onNewResultImpl(@Nullable Bitmap bitmap) {
// You can use the bitmap in only limited ways
// No need to do any cleanup.
if (bitmap != null) {
Drawable drawable = new BitmapDrawable(getResources(), bmp);
LayerDrawable layerDrawable = (LayerDrawable) ResourcesCompat.getDrawable
(getResources(), R.drawable.layer_list, null);
if (layerDrawable != null) {
layerDrawable.setDrawableByLayerId(R.id.cover_image, drawable);
header.setBackground(layerDrawable);
}
}
}

@Override
public void onFailureImpl(DataSource dataSource) {
// No cleanup required here.
}
};
dataSource.subscribe(dataSubscriber, CallerThreadExecutor.getInstance());

最佳答案

在末尾添加以下行:

myRelativeLayout.setBackground(layerDrawable);

看起来你需要在更改可绘制对象后再次设置背景。

这是工作代码:

public class TestActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
final RelativeLayout RL = (RelativeLayout) findViewById(R.id.RL);
Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Drawable drawable = ResourcesCompat.getDrawable(getResources(), R.drawable.image7, null);
LayerDrawable layerDrawable = (LayerDrawable) ResourcesCompat.getDrawable(getResources(), R.drawable.layer_list, null);
layerDrawable.setDrawableByLayerId(R.id.replace, drawable);
RL.setBackground(layerDrawable);

}
});
}
}

layer_list.xml 和你的一样,只是drawable不同。 activity_test.xml 只是一个中间有一个按钮的 RelativeLayout。

关于android - 以编程方式将图像加载到图层列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38159263/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com