第一种是通过canvas画出来的效果:

  1. public void first(View v) {
  2. // 防止出现Immutable bitmap passed to Canvas constructor错误
  3. Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(),
  4. R.drawable.apple).copy(Bitmap.Config.ARGB_8888, true);
  5. Bitmap bitmap2 = ((BitmapDrawable) getResources().getDrawable(
  6. R.drawable.go)).getBitmap();
  7. Bitmap newBitmap = null;
  8. newBitmap = Bitmap.createBitmap(bitmap1);
  9. Canvas canvas = new Canvas(newBitmap);
  10. Paint paint = new Paint();
  11. int w = bitmap1.getWidth();
  12. int h = bitmap1.getHeight();
  13. int w_2 = bitmap2.getWidth();
  14. int h_2 = bitmap2.getHeight();
  15. paint.setColor(Color.GRAY);
  16. paint.setAlpha(125);
  17. canvas.drawRect(0, 0, bitmap1.getWidth(), bitmap1.getHeight(), paint);
  18. paint = new Paint();
  19. canvas.drawBitmap(bitmap2, Math.abs(w - w_2) / 2,
  20. Math.abs(h - h_2) / 2, paint);
  21. canvas.save(Canvas.ALL_SAVE_FLAG);
  22. // 存储新合成的图片
  23. canvas.restore();
  24. image.setImageBitmap(newBitmap);
  25. }
public void first(View v) {

        // 防止出现Immutable bitmap passed to Canvas constructor错误
Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(),
R.drawable.apple).copy(Bitmap.Config.ARGB_8888, true);
Bitmap bitmap2 = ((BitmapDrawable) getResources().getDrawable(
R.drawable.go)).getBitmap(); Bitmap newBitmap = null; newBitmap = Bitmap.createBitmap(bitmap1);
Canvas canvas = new Canvas(newBitmap);
Paint paint = new Paint(); int w = bitmap1.getWidth();
int h = bitmap1.getHeight(); int w_2 = bitmap2.getWidth();
int h_2 = bitmap2.getHeight(); paint.setColor(Color.GRAY);
paint.setAlpha(125);
canvas.drawRect(0, 0, bitmap1.getWidth(), bitmap1.getHeight(), paint); paint = new Paint();
canvas.drawBitmap(bitmap2, Math.abs(w - w_2) / 2,
Math.abs(h - h_2) / 2, paint);
canvas.save(Canvas.ALL_SAVE_FLAG);
// 存储新合成的图片
canvas.restore(); image.setImageBitmap(newBitmap);
}

Canvas canvas = new Canvas(newBitmap); 当以newBitmap创建Canvas时,所以操作都已经在该图上实现了。

该例子可以想做是播放器开始播放的效果,计算好中间位置,先覆盖一层透明灰色的正方形,然后在中间画上自己播放的按钮。

第二种是使用系统的LayerDrawable类,该类主要用来控制多个图片的结合:

  1. public void second(View v) {
  2. Bitmap bitmap1 = ((BitmapDrawable) getResources().getDrawable(
  3. R.drawable.apple)).getBitmap();
  4. Bitmap bitmap2 = ((BitmapDrawable) getResources().getDrawable(
  5. R.drawable.go)).getBitmap();
  6. Drawable[] array = new Drawable[2];
  7. array[0] = new BitmapDrawable(bitmap1);
  8. array[1] = new BitmapDrawable(bitmap2);
  9. LayerDrawable la = new LayerDrawable(array);
  10. // 其中第一个参数为层的索引号,后面的四个参数分别为left、top、right和bottom
  11. la.setLayerInset(0, 0, 0, 0, 0);
  12. la.setLayerInset(1, 20, 20, 20, 20);
  13. image.setImageDrawable(la);
  14. }
public void second(View v) {

        Bitmap bitmap1 = ((BitmapDrawable) getResources().getDrawable(
R.drawable.apple)).getBitmap();
Bitmap bitmap2 = ((BitmapDrawable) getResources().getDrawable(
R.drawable.go)).getBitmap(); Drawable[] array = new Drawable[2];
array[0] = new BitmapDrawable(bitmap1);
array[1] = new BitmapDrawable(bitmap2);
LayerDrawable la = new LayerDrawable(array);
// 其中第一个参数为层的索引号,后面的四个参数分别为left、top、right和bottom
la.setLayerInset(0, 0, 0, 0, 0);
la.setLayerInset(1, 20, 20, 20, 20);
image.setImageDrawable(la);
}

关联数组array,控制每一层的位置

装载自:http://blog.csdn.net/gh102/article/details/6719624

注意:上面防止出现Immutable bitmap passed to Canvas constructor错误

原因是如果不用copy的方法,直接引用会对资源文件进行修改,而android是不允许在代码里修改res文件里的图片

android 图片叠加效果——两种方法的简介与内容 ,带解决Immutable bitmap passed to Canvas constructor错误的更多相关文章

  1. android 图片叠加效果——两种方法

    效果图: 第一种: 第二种: 第一种是通过canvas画出来的效果: public void first(View v) { // 防止出现Immutable bitmap passed to Can ...

  2. UIImage加载图片的两种方法区别

    Apple官方的文档为生成一个UIImage对象提供了两种方法加载图片: 1. imageNamed,其参数为图片的名字: 2. imageWithContentsOfFile,其参数也是图片文件的路 ...

  3. AE 将地图导出为图片的两种方法

    在ArcGIS的开发中,我们经常需要将当前地图打印(或是转出)到图片文件中.将Map或Layout中的图象转出有两种方法,一种为通过IActiveView的OutPut函数,另外一种是通过IExpor ...

  4. input上传图片(file),预览图片的两种方法。blob与base64编码

    上传图片时一般都需要预览,我一般用这两种方法来实现.base64编码可以直接上传到后台,后台解析下,得到的文件就会比较小了,省去了压缩图片这一步了. //获取对象input file 的图片地址,放进 ...

  5. android 一题多改系列——android 打电话实现两种方法

    在android开发中,用户能够拨打电话是最基本的需求.俗话说“条条大路通罗马”,实现拨打电话的方式有多种,今天,就提供最常用两种. 首先,拨打电话,对于用户来说,是一个耗费的操作,因此,需要一定权限 ...

  6. VC下加载JPG/GIF/PNG图片的两种方法

    转载自:http://blog.sina.com.cn/s/blog_6582aa410100huil.html 仅管VC有提供相应的API和类来操作bmp位图.图标和(增强)元文件,但却不支持jpg ...

  7. Android从Camera中获取图片的两种方法

    方法一: 此方法会由Camera直接产生照片回传给应用程序,但是返回的是压缩图片,显示不清晰 ? 1 2 3 4 5 6 try {      Intent cameraIntent = new In ...

  8. python 读取并显示图片的两种方法

    在 python 中除了用 opencv,也可以用 matplotlib 和 PIL 这两个库操作图片.本人偏爱 matpoltlib,因为它的语法更像 matlab. 一.matplotlib 1. ...

  9. python实现读取并显示图片的两种方法

    https://www.cnblogs.com/lantingg/p/9259840.html 在 python 中除了用 opencv,也可以用 matplotlib 和 PIL 这两个库操作图片. ...

随机推荐

  1. 【Maven】pom.xml(1)

    在pom.xml加入: <build> <finalName>oauth2</finalName> <resources> <resource&g ...

  2. maven之setting.xml的配置详解

    文件存放位置 全局配置: ${M2_HOME}/conf/settings.xml 用户配置: ${user.home}/.m2/settings.xml note:用户配置优先于全局配置.${use ...

  3. 【前端】CentOS 7 系列教程之四: 配置 git 服务器自动部署

    转载请注明出处:http://www.cnblogs.com/shamoyuu/p/linux_4.html 安装pm2守护进程,备用 npm install -g pm2 创建/srv/www文件夹 ...

  4. zabbix忘记登录密码重置方法

    1.忘记zabbix之后,先用mysql查看一下: [root@centos7-106 ~]# mysql -uroot -p -e "select * from zabbix.users\ ...

  5. android调用第三方库——第二篇——编写库android程序直接调用第三方库libhello.so (转载)

    转自:http://blog.csdn.net/jiuyueguang/article/details/9449737 版权声明:本文为博主原创文章,未经博主允许不得转载. 0:前言 1:本文主要作为 ...

  6. ARM汇编中ldr伪指令和ldr指令(转载)

    转自:http://blog.csdn.net/ce123_zhouwei/article/details/7182756 ARM是RISC结构,数据从内存到CPU之间的移动只能通过L/S指令来完成, ...

  7. 洛谷 - P2762 - 太空飞行计划问题 - 最小割

    https://www.luogu.org/problemnew/solution/P2762 最小割对应的点,在最后一次更新中dinic的bfs会把他的dep重置掉.所以可以根据这个性质复原最小割. ...

  8. HDU 2063 过山车+poj 1469

    //这是一个非常简单的匹配.其实满感觉这种算法讲道理是可以想到. //但是我们这种弱就只能先学了匈牙利算法,然后随便嗨这种题目了.没事结果都一样. //这就是匹配算法的DFS形式,有一个BFS形式的, ...

  9. ubuntu 直接用软件的名字启动非apt安装的软件

    方法一: 可以在.bashrc文件中加入 alias命令,把软件的名字就等于软件执行文件的绝对路径 方法二: 在/usr/bin 目录下为执行文件创建软链接(未尝试)不过应该可以 软件自启动的方法 在 ...

  10. hdu1102 Constructing Roads 基础最小生成树

    //克鲁斯卡尔(最小生成树) #include<cstdio> #include<iostream> #include<algorithm> using names ...