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

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

问题描述

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

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);

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

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

推荐答案

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

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);

如果您在执行此操作时处于活动中,您甚至不需要调用 context.getResources(),只需调用 getResources().

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

然后您将获取所有图层并创建您的 LayerDrawable,就像您已经创建的那样:

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);

(注意 layer3 将在 layer2 之上,layer2 将在 layer1 之上).

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

拥有 LayerDrawable 后,您可以使用 view.setBackgoundDrawable(drawable)(在 API 16 及更高版本上)或 view.setBackground(drawable)(在 API 16 之前).这篇博文展示了如何检查设备版本并在您支持 16 之前的设备时调用适当的方法.

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.

如果您想相对于彼此定位图层,那么您还需要使用 setLayerInset() 就像您在代码中一样,但我建议您在获取图层后尝试-要显示的列表.

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天全站免登陆