ImageView imgMode = new ImageView(context);
Bitmap bm1 = BitmapFactory.decodeResource(getResources(),R.drawable.snow);
Bitmap bm2 = BitmapFactory.decodeResource(getResources(),R.drawable.cross);
Drawable[] array = new Drawable[2];
array[0] = new BitmapDrawable(bm1);
array[1] = new BitmapDrawable(bm2);
LayerDrawable ld = new LayerDrawable(array);
imgMode.setImageDrawable(ld);
如上代码,可以用LayerDrawable重叠图片,但是做出来的效果是图片会被调整大小
在SDK中有如下解释:
All drawable items are scaled to fit the size of the containing View, by default. Thus, placing your images in a layer list at different positions might increase the size of the View and some images scale as appropriate. To avoid scaling items in the list, use a <bitmap> element inside the <item> element to specify the drawable and define the gravity to something that does not scale, such as "center". For example, the following <item> defines an item that scales to fit its container View:
<item android:drawable="@drawable/image" />
To avoid scaling, the following example uses a <bitmap> element with centered gravity:
<item>
  <bitmap android:src="@drawable/image"
          android:gravity="center" />
</item>其中介绍说用xml文件来写布局,其中用bitmap作为item部署的话,就可以让LayerDrawable的大小来适应bitmap的大小,但是
由于在项目风格上,都没有用xml文件来写布局,直接是在java代码里来写布局的
在代码方面目前没找到实现该效果的方法
求达人帮助 如何让LayerDrawable适应里面Image的大小 而不是让调整Image的大小去适应LayerDrawable
如果有其他方法实现重叠效果,能否提供例子呢 如使用canvas