以编程方式创建层列表 [英] programmatically create layer-list

查看:67
本文介绍了以编程方式创建层列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以编程方式创建一个将大小调整后的位图作为项目的图层列表.从我所看到的BitmapDrawable已被弃用.新的构造函数需要以下参数-公共BitmapDrawable(资源res,Bitmap位图).我从下面的一个非常基本的示例开始.

    BitmapDrawable background = new BitmapDrawable();
    background.setBounds(10,10,10,10);
    Drawable[] layers = {background};
    LayerDrawable splash_test = new LayerDrawable(layers);
    splash_test.setLayerInset(0, 0, 0, 0, 0);

如何正确使用新的BitmapDrawable构造函数以及如何将可绘制资源链接到背景对象.

解决方案

您提到要从几个位图制作一个图层列表.您所拥有的基本上是正确的,您所要做的就是获取每个位图对象并将其转换为BitmapDrawable.为此,您可以使用:

BitmapDrawable layer1 = new BitmapDrawable(context.getResources(), bitmap1);

如果您在进行某项活动时,甚至不需要呼叫context.getResources(),只需呼叫getResources().

然后,将您的所有图层都创建好并创建LayerDrawable,就像您已经是这样:

Drawable[] layers = {layer1, layer2, layer3};
LayerDrawable splash_test = new LayerDrawable(layers);

(请注意,第3层将在第2层之上,而第2层将在第1层之上).

一旦有了LayerDrawable,就可以使用view.setBackgoundDrawable(drawable)(在API 16和更高版本上)或view.setBackground(drawable)(在API 16之前)上在视图的背景上进行设置. 这篇文章显示了如何检查设备版本并在支持16个之前的设备的情况下调用适当的方法.

如果要相对放置图层,则还需要像在代码中一样使用setLayerInset(),但是我建议您在显示图层列表后尝试使用. >

I am trying to programmatically create a layer-list with resized bitmaps as items. From what I have seen BitmapDrawable has been deprecated. The new constructor requires the following parameters - public BitmapDrawable (Resources res, Bitmap bitmap). I have started out with a very basic example below.

    BitmapDrawable background = new BitmapDrawable();
    background.setBounds(10,10,10,10);
    Drawable[] layers = {background};
    LayerDrawable splash_test = new LayerDrawable(layers);
    splash_test.setLayerInset(0, 0, 0, 0, 0);

How would I correctly use the new BitmapDrawable constructor and how do I link a drawable resource to the background object.

解决方案

You mentioned that you want to make a layer list from a couple of bitmaps. What you have is largely correct, all you need to do is take each bitmap object and turn it into a BitmapDrawable. To do this you can use:

BitmapDrawable layer1 = new BitmapDrawable(context.getResources(), bitmap1);

If you are in an activity when you do this you don't even need to call context.getResources(), just getResources().

Then you will take all your layers and create your LayerDrawable, much like you already are:

Drawable[] layers = {layer1, layer2, layer3};
LayerDrawable splash_test = new LayerDrawable(layers);

(note that layer3 will be above layer2 and layer2 will be above layer1).

Once you have the LayerDrawable you can set it on the background of your view using view.setBackgoundDrawable(drawable) (on API 16 and greater) or view.setBackground(drawable) (on pre API 16). This post shows how to check the device version and call the appropriate method if you are supporting pre 16 devices.

If you want to position the layers relative to each other then you will also need to use setLayerInset() as you have in your code, but I would recommend that you try that after getting your layer-list to display.

这篇关于以编程方式创建层列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆