Reflection效果

倒影效果,简化版

package com.nico;

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

public class TestActivity extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		// 获取图片的bitmap对象
		Bitmap bmp = BitmapFactory
				.decodeResource(getResources(), R.drawable.qm);
		// 调用生成带倒影的bitmap
		Bitmap b = getFBitmap(bmp);
		ImageView img = (ImageView) findViewById(R.id.img);
		// 生成imageview
		img.setImageBitmap(b);

	}

	public Bitmap getFBitmap(Bitmap bmp) {

		// 画布
		Canvas canvas = new Canvas();
		// 矩阵
		Matrix matrix = canvas.getMatrix();
		// 缩放
		matrix.preScale(1, -1);
		// 生成带倒影的bitmap打底图
		Bitmap allbitmap = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight()
				+ bmp.getHeight() / 2, Config.ARGB_8888);
		// 设置为画布背景
		canvas.setBitmap(allbitmap);
		Paint dp = new Paint();
		// 将初始图像画上去
		canvas.drawBitmap(bmp, 0, 0, dp);
		Paint p = new Paint();
		// 绘制下半部分的图像
		canvas.drawRect(0, bmp.getHeight(), bmp.getWidth(),
				bmp.getHeight() / 2, p);

		// 绘制下边部分的图像
		Bitmap flenbmp = Bitmap.createBitmap(bmp, 0, bmp.getHeight() / 2, bmp
				.getWidth(), bmp.getHeight() / 2, matrix, false);

		//将btmap 绘制到画布上
		canvas.drawBitmap(flenbmp, 0, bmp.getHeight(), null);
		Paint paint = new Paint();
		//渐变的效果,不是很清楚
		LinearGradient shader = new LinearGradient(0, bmp.getHeight(), 0,
				allbitmap.getHeight(), 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
		paint.setShader(shader);
		paint.setXfermode(new PorterDuffXfermode((PorterDuff.Mode.DST_IN)));
		//绘制下面的图片的框,并使用到设置好参数的paint
		canvas.drawRect(0, bmp.getHeight(), bmp.getWidth(), allbitmap.getHeight() , paint);
		return allbitmap;

	}
}

 

 

你可能感兴趣的:(android,OS)