Android 图片合成:添加蒙板效果 不规则相框 透明度渐变效果的实现
先贴一张效果图,这是一张手机截屏:
左上方的风景图:背景图片
右上方的人物图:前景图片
左边心型透明图:相框图片
右边心型黑色图:蒙板图片
功能:把前景图应用蒙板,添加相框效果,合成到后景图上面:
结果就是下面的那张图片了。
还有一种是透明度渐变的,效果图如下:
因为只有透明度渐变,没有相框。但实现上基本一样。
下面是实现过程,直接贴代码吧,其中写了比较详细的注释。只有一个文件,如下:
- package com.example.androiddemo;
- import android.os.Bundle;
- import android.os.Environment;
- import android.app.Activity;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Canvas;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.ImageView;
- public class MainActivity extends Activity {
- private static final String TAG = "liuzw";
- private ImageView picBGView;
- private ImageView pictureView;
- private ImageView maskView;
- private ImageView frameView;
- private ImageView resultView;
- private Button startProcess;
- private Bitmap picBitmap;
- private Bitmap maskBitmap;
- private Bitmap frameBitmap;
- private Bitmap resultBitmap;
- private Bitmap fengjingBitmap;
- private Bitmap composedBitmap;
- private final int WITHOUT = -1;
- private static final int FRAME = 0;
- private static final int MASK = 1;
- // private int[] resIds = new int[]{ //斜框锯齿
- // R.drawable.pip_6_frame,
- // R.drawable.pip_6_frame_mask,
- // };
- // private int[] resIds = new int[]{ //胶条
- // R.drawable.pip_1_frame,
- // R.drawable.pip_1_frame_mask,
- // };
- private int[] resIds = new int[]{ //渐变
- WITHOUT,
- R.drawable.pip_2_frame_mask,
- };
- // private int[] resIds = new int[]{ //心形
- // R.drawable.pip_3_frame,
- // R.drawable.pip_3_frame_mask,
- // };
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- picBGView = (ImageView) findViewById(R.id.pic_bg);
- picBGView.setImageResource(R.drawable.fengjing);
- pictureView = (ImageView) findViewById(R.id.pic);
- pictureView.setImageResource(R.drawable.pip_test);
- maskView = (ImageView) findViewById(R.id.mask);
- maskView.setImageResource(resIds[MASK]);
- frameView = (ImageView) findViewById(R.id.frame);
- frameView.setImageResource(resIds[FRAME]);
- startProcess = (Button) findViewById(R.id.btnStart);
- startProcess.setOnClickListener(mListener);
- resultView = (ImageView) findViewById(R.id.showResult);
- }
- /**
- * 获得前置照片
- */
- private void getFrontPicture(){
- //蒙板的Bitmap
- if(maskBitmap == null || maskBitmap.isRecycled() && resIds[MASK] != WITHOUT){
- maskBitmap = BitmapFactory.decodeResource(this.getResources(), resIds[MASK]);
- }
- if(maskBitmap == null) return;
- //前置的原图,并将其缩放到跟蒙板大小一直
- if(picBitmap == null || picBitmap.isRecycled()){
- picBitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.pip_test);
- picBitmap = Bitmap.createScaledBitmap(picBitmap, maskBitmap.getWidth(), maskBitmap.getHeight(), false);
- }
- //相框的Bitmap
- if(frameBitmap == null || frameBitmap.isRecycled() && resIds[FRAME] != WITHOUT){
- frameBitmap = BitmapFactory.decodeResource(this.getResources(), resIds[FRAME]);
- }
- int w = maskBitmap.getWidth();
- int h = maskBitmap.getHeight();
- int edgeColor = maskBitmap.getPixel(1, 1);
- int centerColor = maskBitmap.getPixel(w/2, h/2);
- Log.d(TAG, "edgeColor = " + Integer.toHexString(edgeColor) + ", centerColor = " + Integer.toHexString(centerColor));
- if(resultBitmap == null){
- resultBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
- }
- //这是背景的风景图
- if(fengjingBitmap == null){
- fengjingBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.fengjing);
- }
- //前置相片添加蒙板效果
- int[] picPixels = new int[w*h];
- int[] maskPixels = new int[w*h];
- picBitmap.getPixels(picPixels, 0, w, 0, 0, w, h);
- maskBitmap.getPixels(maskPixels, 0, w, 0, 0, w, h);
- for(int i = 0; i < maskPixels.length; i++){
- if(maskPixels[i] == 0xff000000){
- picPixels[i] = 0;
- }else if(maskPixels[i] == 0){
- //donothing
- }else{
- //把mask的a通道应用与picBitmap
- maskPixels[i] &= 0xff000000;
- maskPixels[i] = 0xff000000 - maskPixels[i];
- picPixels[i] &= 0x00ffffff;
- picPixels[i] |= maskPixels[i];
- }
- }
- //生成前置图片添加蒙板后的bitmap:resultBitmap
- resultBitmap.setPixels(picPixels, 0, w, 0, 0, w, h);
- }
- /**
- * 图片合成
- */
- private void compose(){
- if(fengjingBitmap == null || fengjingBitmap.isRecycled()){
- Log.e(TAG, "compose ERROR: fengjingBitmap is not valuable");
- return;
- }
- composedBitmap = Bitmap.createBitmap(fengjingBitmap.getWidth(), fengjingBitmap.getHeight(), Bitmap.Config.ARGB_8888);
- if(composedBitmap == null || composedBitmap.isRecycled()){
- Log.e(TAG, "compose ERROR: composedBitmap is not valuable");
- return;
- }
- if(resultBitmap == null || resultBitmap.isRecycled()){
- Log.e(TAG, "compose ERROR: resultBitmap is not valuable");
- return;
- }
- Canvas cv = new Canvas(composedBitmap);
- cv.drawBitmap(fengjingBitmap, 0, 0, null);
- cv.drawBitmap(resultBitmap, 100, 100, null);
- if(frameBitmap != null && !frameBitmap.isRecycled()){
- cv.drawBitmap(frameBitmap, 100, 100, null);
- }
- cv.save(Canvas.ALL_SAVE_FLAG);
- cv.restore();
- resultView.setImageBitmap(composedBitmap);
- }
- @Override
- protected void onDestroy() {
- // TODO Auto-generated method stub
- super.onDestroy();
- //释放资源
- resultView.setImageBitmap(null);
- if(picBitmap != null && !picBitmap.isRecycled()){
- picBitmap.recycle();
- picBitmap = null;
- }
- if(maskBitmap != null && !maskBitmap.isRecycled()){
- maskBitmap.recycle();
- maskBitmap = null;
- }
- if(frameBitmap != null && !frameBitmap.isRecycled()){
- frameBitmap.recycle();
- frameBitmap = null;
- }
- if(resultBitmap != null && !resultBitmap.isRecycled()){
- resultBitmap.recycle();
- resultBitmap = null;
- }
- if(fengjingBitmap != null && !fengjingBitmap.isRecycled()){
- fengjingBitmap.recycle();
- fengjingBitmap = null;
- }
- if(composedBitmap != null && !composedBitmap.isRecycled()){
- composedBitmap.recycle();
- composedBitmap = null;
- }
- }
- private OnClickListener mListener = new OnClickListener(){
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- switch(v.getId()){
- case R.id.btnStart:
- getFrontPicture();
- compose();
- break;
- }
- }
- };
- }
为了完整和方便参考,把布局文件也贴一下,如下:
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="#ffffffff"
- tools:context=".MainActivity" >
- <LinearLayout
- android:id="@+id/views1"
- android:layout_width="match_parent"
- android:layout_height="150dip"
- android:orientation="horizontal" >
- <ImageView
- android:id="@+id/pic_bg"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1.0" />
- <ImageView
- android:id="@+id/pic"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1.0" />
- </LinearLayout>
- <LinearLayout
- android:id="@+id/views2"
- android:layout_below="@+id/views1"
- android:layout_width="match_parent"
- android:layout_height="150dip"
- android:orientation="horizontal" >
- <ImageView
- android:id="@+id/frame"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1.0" />
- <ImageView
- android:id="@+id/mask"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1.0" />
- </LinearLayout>
- <Button
- android:id="@+id/btnStart"
- android:layout_below="@+id/views2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Start" />
- <ImageView
- android:id="@+id/showResult"
- android:layout_below="@+id/btnStart"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"/>
- </RelativeLayout>
Android 图片合成:添加蒙板效果 不规则相框 透明度渐变效果的实现的更多相关文章
- 【Anroid界面实现】WindowManager类使用具体解释——用户首次打开APP的使用教学蒙板效果实现
转载请注明出处:http://blog.csdn.net/zhaokaiqiang1992 在上一篇的文章中,我们介绍了怎样实现桌面悬浮窗体,在这个效果的实现过程中.最重要的一个类就是WindowMa ...
- android 图片特效处理之怀旧效果
图片特效处理系列将介绍图片的像素点的特效处理,这些物资注重的是原理.也就是说只要你知道这些算法不管是C++,VB,C#,Java都可以做出相同的特效.下面将介绍图片怀旧效果的算法.算法如下: 上面公式 ...
- android图片特效处理之怀旧效果
图片特效处理系列将介绍图片的像素点的特效处理,这些物资注重的是原理.也就是说只要你知道这些算法不管是C++,VB,C#,Java都可以做出相同的特效.下面将介绍图片怀旧效果的算法.算法如下: 上面公式 ...
- android 图片特效处理之 光晕效果
这篇将讲到图片特效处理的图片光晕效果.跟前面一样是对像素点进行处理,本篇实现的思路可参见android图像处理系列之九--图片特效处理之二-模糊效果和android图像处理系列之十三--图片特效处理之 ...
- android 图片特效处理之光晕效果
这篇将讲到图片特效处理的图片光晕效果.跟前面一样是对像素点进行处理,本篇实现的思路可参见android图像处理系列之九--图片特效处理之二-模糊效果和android图像处理系列之十三--图片特效处理之 ...
- android图片特效处理之光晕效果
这篇将讲到图片特效处理的图片光晕效果.跟前面一样是对像素点进行处理,本篇实现的思路可参见android图像处理系列之九--图片特效处理之二-模糊效果和android图像处理系列之十三--图片特效处理之 ...
- android 图片合成
package com.ebensz.eink.demo; import java.io.File; import java.io.FileOutputStream; import android.a ...
- WPF蒙板弹窗
由于界面设计需要,要给弹窗添加蒙板效果,在百度和google搜索了半天,竟然没有一个满意的方案,最后只能自己想办法实现了一个,原理还是比较简单的,现在分享给大家. 先看一下效果.. 原理其实 ...
- SVG 2D入门9 - 蒙板
SVG支持的蒙板 SVG支持多种蒙板特效,使用这些特性,我们可以做出很多很炫的效果.至于中文中把mask叫做"蒙板"还是"遮罩"就不去区分了,这里都叫做蒙板吧. ...
随机推荐
- js深入研究之牛逼的类封装设计
<script type="text/javascript"> var Book = function(newIsbn, newTitle, newAuthor) { ...
- cf494A Treasure
A. Treasure time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...
- ASP.NET文件组成(转载于Owen的BLOG)
一.扩展名: .aspx:窗体文件,为前台程序. .cs文件:类文件,主要为后台数据处理,供所有的.aspx文件的后台应用. .asmx文件:用于创建从其他应用程序使用的web服务的类. .css文件 ...
- myBatis学习(9):一级缓存和二级缓存
正如大多数持久层框架一样,MyBatis同样提供了一级缓存和二级缓存的支持 1. MyBatis一级缓存基于PerpetualCache的HashMap本地缓存,其存储作用域为 Session,默认情 ...
- iOS 使用pods报错问题 pod --version
错误信息如下 find_spec_for_exe': can't find gem cocoapods (>= 0.a) (Gem::GemNotFoundException) from /Us ...
- css中的列表样式
在网页设计中,我们经常将某些具有相似功能的标签放在同一组中,这时我们经常会用到列表标签(无序列表ul,有序列表ol),在列表标签中对列表样式的设计可以使我们的页面得到一定程度的美化. 在css中对列表 ...
- 知方可补不足~CSS中的几个伪元素
对于一个很好的编辑器VS来说,它对于编程语句的自动提示功能是很强大的,有时,我们根本不需要看相关API,而直接看VS给我们的提示就可以完成一个新技术的学习了. 今天我们来说几个CSS中的伪元素,它们在 ...
- BUG出现的地方真的令我这个测试新人想象不到
今天上班,仍然在等待下一阶段项目的研发完成. 没有正式测试任务的我,作为新手肯定要趁着这个时间好好学习了,偶尔再拿出公司已经上线发布的APP来到处看看. 就在这偶尔的情况下让我发现了一个在正式测试时根 ...
- 【转载】CocoaPods安装和使用教程
转自:http://code4app.com/article/cocoapods-install-usage 目录 CocoaPods是什么? 如何下载和安装CocoaPods? 如何使用CocoaP ...
- Struts2上传文件
jsp: <form action="file_upload.action" method="post" enctype="multipart/ ...