`
onewayonelife
  • 浏览: 259761 次
  • 性别: Icon_minigender_1
  • 来自: 太原
社区版块
存档分类
最新评论

Android 图片倒影

 
阅读更多

MainActivity

package org.wp.activity;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.PorterDuffXfermode;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;
import android.graphics.Shader.TileMode;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.widget.ImageView;

/**
 * ========================================== 
 * Matrix
 * ========================================== 
 * The Matrix class holds a 3x3 matrix for transforming coordinates. 
 * Matrix does not have a constructor, 
 * so it must be explicitly initialized using either reset() 
 * - to construct an identity matrix, 
 * or one of the set..() functions 
 * (e.g. setTranslate, setRotate, etc.).
 * 
 * Matrix 中文里叫矩阵,高等数学里有介绍
 * 在图像处理方面,主要是用于平面的缩放、平移、旋转等操作。
 * Matrix的操作,总共分为translate(平移),rotate(旋转),
 * scale(缩放)和skew(倾斜)四种,
 * 每一种变换在Android的API里都提供了set, post和pre三种操作方式
 * 除了translate,其他三种操作都可以指定中心点。 
 * 
 * ========================================== 
 * createBitmap
 * ==========================================
 * public static Bitmap createBitmap (Bitmap source, int x, int y, 
 * 					int width, int height, Matrix m, boolean filter) 
 * Since: API Level 1 Returns an immutable bitmap from subset of the 
 * 		  source bitmap, transformed by the optional matrix. 
 *        It is initialized with the same density as the original bitmap.
 * Parameters
 * source  The bitmap we are subsetting 
 * x  The x coordinate of the first pixel in source 
 * y  The y coordinate of the first pixel in source 
 * width  The number of pixels in each row 
 * height  The number of rows 
 * m  Optional matrix to be applied to the pixels 
 * filter  true if the source should be filtered. 
 *         Only applies if the matrix contains more than 
 *         just translation. 
 * Returns
 * A bitmap that represents the specified subset of source
 * Throws
 * IllegalArgumentException  
 * if the x, y, width, height values are outside of the 
 * dimensions of the source bitmap. 
 * 
 * source 源 bitmap对象
 * x 源坐标x位置
 * y 源坐标y位置
 * width 宽度
 * height 高度
 * m 接受的maxtrix对象,如果没有可以设置 为null
 * filter 该参数仅对maxtrix包含了超过一个翻转才有效
 * 
 * ==========================================
 * LinearGradient
 * ==========================================
 * public LinearGradient (float x0, float y0, float x1, float y1, i
 * 				nt color0, int color1, Shader.TileMode tile) 
 * Since: API Level 1 Create a shader that draws a linear gradient along a line.
 * Parameters
 * x0  The x-coordinate for the start of the gradient line 
 * y0  The y-coordinate for the start of the gradient line 
 * x1  The x-coordinate for the end of the gradient line 
 * y1  The y-coordinate for the end of the gradient line 
 * color0  The color at the start of the gradient line. 
 * color1  The color at the end of the gradient line. 
 * tile  The Shader tiling mode  
 * 
 * 在android.graphics中我们可以找到有关Gradient字样的类,
 * 比如LinearGradient 线性渐变、RadialGradient径向渐变  和 角度渐变SweepGradient 三种,
 * 他们的基类为android.graphics.Shader。
 * 
 * LinearGradient线性渐变
 * 在android平台中提供了两种重载方式来实例化该类分别为,
 * 他们的不同之处为参数中第一种方法可以用颜色数组,和位置来实现更细腻的过渡效果,
 * 比如颜色采样int[] colors数组中存放20种颜色,则渐变将会逐一处理。
 * 而第二种方法参数仅为起初颜色color0和最终颜色color1。
 * LinearGradient(float x0, float y0, float x1, float y1, 
 * 		int[] colors, float[] positions, Shader.TileMode tile) 
 * LinearGradient(float x0, float y0, float x1, float y1, 
 * 		int color0, int color1, Shader.TileMode tile) 
 * 
 * 参数一为渐变起初点坐标x位置,参数二为y轴位置,
 * 参数三和四分辨对应渐变终点,最后参数为平铺方式,这里设置为镜像
 * 
 * 刚才已经讲到Gradient是基于Shader类,
 * 所以我们通过Paint的setShader方法来设置这个渐变
 * p.setShader(lg);
 * 
 * ==========================================
 * setXfermode
 * ==========================================
 * Xfermode
 * 可以通过修改Paint的Xfermode来影响在
 * Canvas已有的图像上面绘制新的颜色的方式 。
 * 
 * 在正常的情况下,在已有的图像上绘图将会在其上面添加一层新的形状。
 * 如果新的Paint是完全不透明的,那么它将完全遮挡住下面的Paint;
 * 如果它是部分透明的,那么它将会被染上下面的颜色。
 * 
 * 下面的Xfermode子类可以改变这种行为:
 * 
 * AvoidXfermode  指定了一个颜色和容差,
 * 				      强制Paint避免在它上面绘图(或者只在它上面绘图)。
 * PixelXorXfermode  当覆盖已有的颜色时,应用一个简单的像素XOR操作。
 * 
 * PorterDuffXfermode  这是一个非常强大的转换模式,使用它,
 *                     可以使用图像合成的16条Porter-Duff规则的任意
 *                     一条来控制Paint如何与已有的Canvas图像进行交互。
 * 
 * 16条Porter-Duff规则
 * 1.PorterDuff.Mode.CLEAR
 * 2.PorterDuff.Mode.SRC
 * 3.PorterDuff.Mode.DST
 * 4.PorterDuff.Mode.SRC_OVER
 * 5.PorterDuff.Mode.DST_OVER
 * 6.PorterDuff.Mode.SRC_IN
 * 7.PorterDuff.Mode.DST_IN
 * 8.PorterDuff.Mode.SRC_OUT
 * 9.PorterDuff.Mode.DST_OUT
 * 10.PorterDuff.Mode.SRC_ATOP
 * 11.PorterDuff.Mode.DST_ATOP
 * 12.PorterDuff.Mode.XOR
 * 13.PorterDuff.Mode.DARKEN
 * 14.PorterDuff.Mode.LIGHTEN
 * 15.PorterDuff.Mode.MULTIPLY
 * 16.PorterDuff.Mode.SCREEN 
 * 
 * @author wp
 */
public class MainActivity extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		ImageView myImageView = (ImageView) this.findViewById(R.id.myImageView);
		Bitmap bitmap = ((BitmapDrawable) getResources().getDrawable(
				R.drawable.qianqian)).getBitmap();
		myImageView.setImageBitmap(createReflectedImage(bitmap));
	}

	private Bitmap createReflectedImage(Bitmap originalBitmap) {
		// 图片与倒影间隔距离
		final int reflectionGap = 4;
		
		// 图片的宽度
		int width = originalBitmap.getWidth();
		// 图片的高度
		int height = originalBitmap.getHeight();
		
		Matrix matrix = new Matrix();
		// 图片缩放,x轴变为原来的1倍,y轴为-1倍,实现图片的反转
		matrix.preScale(1, -1);
		// 创建反转后的图片Bitmap对象,图片高是原图的一半。
		Bitmap reflectionBitmap = Bitmap.createBitmap(originalBitmap, 0,
				height / 2, width, height / 2, matrix, false);
		// 创建标准的Bitmap对象,宽和原图一致,高是原图的1.5倍。
		Bitmap withReflectionBitmap = Bitmap.createBitmap(width, (height
				+ height / 2 + reflectionGap), Config.ARGB_8888);

		// 构造函数传入Bitmap对象,为了在图片上画图
		Canvas canvas = new Canvas(withReflectionBitmap);
		// 画原始图片
		canvas.drawBitmap(originalBitmap, 0, 0, null);

		// 画间隔矩形
		Paint defaultPaint = new Paint();
		canvas.drawRect(0, height, width, height + reflectionGap, defaultPaint);

		// 画倒影图片
		canvas.drawBitmap(reflectionBitmap, 0, height + reflectionGap, null);

		// 实现倒影效果
		Paint paint = new Paint();
		LinearGradient shader = new LinearGradient(0, originalBitmap.getHeight(), 
				0, withReflectionBitmap.getHeight(), 0x70ffffff, 0x00ffffff,
				TileMode.MIRROR);
		paint.setShader(shader);
		paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));

		// 覆盖效果
		canvas.drawRect(0, height, width, withReflectionBitmap.getHeight(), paint);

		return withReflectionBitmap;
	}
}

 

 

 

分享到:
评论
2 楼 mjbb 2012-03-11  
能不能多写些关于图形编程的文章啊,万分感谢
1 楼 mjbb 2012-03-11  
好文章啊,对于我这种入门级的人真好

相关推荐

    Android图片倒影+圆角图片

    首先,我们来讨论如何在Android中实现图片的倒影效果。倒影效果通常在UI设计中用于创造视觉上的平衡感,它可以通过Java代码或者使用第三方库来实现。基本思路是先将原始图片翻转,然后与原图拼接在一起。在Java中,...

    Android 图片倒影效果源码.rar

    在Android开发中,图片倒影效果是一种常见的视觉设计,它能为用户界面增添美观和动态感。本资源“Android 图片倒影效果源码.rar”包含了一种实现该效果的方法,下面将详细介绍如何在Android应用中创建图片倒影以及...

    Android图片倒影效果源码.zip

    本资源“Android图片倒影效果源码.zip”提供了一种实现Android平台上图片倒影效果的方法。以下是对这个源码实现的详细解析: 1. **图片倒影原理** 图片倒影通常是通过复制原始图片,然后翻转复制的部分,再将其...

    Android 图片倒影效果源码.zip

    这个压缩包“Android 图片倒影效果源码.zip”很可能包含了一个实现图片倒影功能的示例项目,我们可以从这个源码中学习如何在Android应用中创建图片倒影。 首先,创建图片倒影的基本原理是复制原始图片的下半部分,...

    Android程序研发源码Android 图片倒影效果源码.rar

    在Android应用开发中,创建图片倒影效果是一个常见的需求,特别是在设计美观的用户界面时。这份"Android程序研发源码Android 图片倒影效果源码.rar"提供的代码示例可以帮助开发者实现这一功能。源码中的核心是`Image...

    Android:图片倒影效果

    本话题主要关注如何实现“Android:图片倒影效果”,这通常涉及到自定义组件和图形渲染技术。在Android中,我们可以利用自定义`Gallery`控件来实现图片的平滑切换,并结合倒影效果为用户提供更为生动的交互体验。 `...

    Android 图片倒影效果源码.zip项目安卓应用源码下载

    Android 图片倒影效果源码.zip项目安卓应用源码下载Android 图片倒影效果源码.zip项目安卓应用源码下载 1.适合学生毕业设计研究参考 2.适合个人学习研究参考 3.适合公司开发项目技术参考

    android实现图片的倒影效果

    以上就是一个基本的Android图片倒影效果的实现过程。当然,你可以根据需求调整细节,比如改变分隔线的颜色、透明度,或者调整倒影的翻转程度。这个过程涉及到Bitmap操作、Canvas绘图以及对位图配置的理解,是Android...

    [Android应用源码]-图片倒影效果源码

    在Android开发中,实现图片倒影效果是一种常见的视觉设计需求,可以增强用户界面的美观性和动态感。这个压缩包文件提供了实现图片倒影效果的源码示例,可以帮助开发者理解和学习如何在Android应用中创建此类效果。...

    Android 图片倒影效果源码.zip源码资源下载

    在Android开发中,图片倒影效果是一种常见的视觉设计,它能为用户界面增添动态感和艺术性。这个源码资源提供了实现图片倒影的详细代码,适用于那些希望在自己的应用中添加类似效果的开发者。本资源主要基于Java编程...

    Android倒影效果

    实现Android图片倒影效果主要分为以下几个步骤: 1. **创建自定义View**:首先,我们需要创建一个自定义的View类,例如名为`ReflectView`。在这个类中,我们将处理倒影的逻辑。继承自`View`或`ImageView`,并重写...

    Android 图片倒影效果源码-IT计算机-毕业设计.zip

    在Android应用开发中,创建图片倒影效果是一种常见的视觉增强技术,可以提升用户界面的美观度。本项目是一个专门针对Android平台的图片倒影效果实现的源码示例,非常适合计算机专业的学生作为毕业设计或者论文研究...

    android 圆角图片+图片倒影

    在Android开发中,创建圆角图片和图片倒影是常见的需求,这主要涉及到图像处理和视图绘制的技术。本文将详细讲解如何在Android平台上实现这些功能。 首先,让我们来看看如何生成圆角图片。在Android中,我们可以...

    Android实现图片的倒影效果

    总结来说,Android实现图片倒影效果的核心步骤包括:创建新的Bitmap,通过Canvas和Matrix实现图片的翻转,利用LinearGradient创建渐变效果,最后将倒影与原始图片合并。这些知识点是Android图形处理和UI设计的重要...

Global site tag (gtag.js) - Google Analytics