android自定义带倒影的ImageView控件

简介:

http://xiaolifan.iteye.com/blog/1258520

今天给大家带来的是一个带倒影的ImageView控件,实现方法很简单,注释里面写的很详细,部分代码来自网络,我稍加了修改: 

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.widget.ImageView;

public class ReflectImageView extends ImageView {
	
	private Bitmap originalBitmap;

	public ReflectImageView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public ReflectImageView(Context context) {
		this(context, null, 0);
	}

	public ReflectImageView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		DoReflection(((BitmapDrawable)getDrawable()).getBitmap());
	}
	
	@Override
	public void setImageBitmap(Bitmap bm) {
		DoReflection(bm);
		
	}
	
	/**显示倒影效果的setImageBitmap函数*/
	public void setImageBitmap(Bitmap bm,boolean isFlected) {
		if (isFlected) {
			super.setImageBitmap(bm);
		}
	}
	
	@Override
	public void setImageResource(int resId) {
		
		originalBitmap = BitmapFactory.decodeResource(getResources(), resId);
		DoReflection(originalBitmap);
	}
	
	private void DoReflection(Bitmap originalImage) {
        // 原始图片和反射图片中间的间距
        final int reflectionGap = 4;
        int width = originalImage.getWidth();
        int height = originalImage.getHeight();
        // 反转
        Matrix matrix = new Matrix();
        // 第一个参数为1表示x方向上以原比例为准保持不变,正数表示方向不变。
        // 第二个参数为-1表示y方向上以原比例为准保持不变,负数表示方向取反。
        matrix.preScale(1, -0.75f);
        // reflectionImage就是下面透明的那部分,可以设置它的高度为原始的3/4,这样效果会更好些
        Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0, 0, width, height, matrix, false);
        // 创建一个新的bitmap,高度为原来的两倍
        Bitmap bitmap4Reflection = Bitmap.createBitmap(width, 2 * height, Config.ARGB_8888);
        // 其宽*高 = width * (height + height * 3 / 4)
        Canvas canvasRef = new Canvas(bitmap4Reflection);
     // defaultPaint不能为null,否则会有空指针异常。
        Paint deafaultPaint = new Paint();
        deafaultPaint.setAntiAlias(true);
        // 先画原始的图片
        canvasRef.drawBitmap(originalImage, 0, 0, deafaultPaint);
        // 画间距
        canvasRef.drawRect(0, height, width, height + reflectionGap, deafaultPaint);
        // 画被反转以后的图片
        canvasRef.drawBitmap(reflectionImage, 0, height + reflectionGap, null);
        // 创建一个渐变的蒙版放在下面被反转的图片上面
        Paint paint = new Paint();
        LinearGradient shader = new LinearGradient(200, originalImage.getHeight(), 10, bitmap4Reflection.getHeight()
                        + reflectionGap, Color.argb(100, 0, 0, 0), Color.argb(100, 0, 0, 0), TileMode.CLAMP);
        paint.setShader(shader);
        // Set the Transfer mode to be porter duff and destination in
        paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
        // 将蒙板画上
        canvasRef.drawRect(0, height, width, bitmap4Reflection.getHeight() + reflectionGap, paint);
        // 调用ImageView中的setImageBitmap
        this.setImageBitmap(bitmap4Reflection,true);
}
	
}


相关文章
|
24天前
|
XML Java Android开发
Android实现自定义进度条(源码+解析)
Android实现自定义进度条(源码+解析)
51 1
|
4月前
|
XML Android开发 数据安全/隐私保护
Android 自定义开源库 EasyView
Android 自定义开源库 EasyView
|
3天前
|
移动开发 Java Unix
Android系统 自动加载自定义JAR文件
Android系统 自动加载自定义JAR文件
21 1
|
3天前
|
Shell Android开发 开发者
Android系统 自定义动态修改init.custom.rc
Android系统 自定义动态修改init.custom.rc
22 0
|
3天前
|
存储 安全 Android开发
Android系统 自定义系统和应用权限
Android系统 自定义系统和应用权限
18 0
|
4月前
|
XML API Android开发
Android 自定义View 之 圆环进度条
Android 自定义View 之 圆环进度条
|
18天前
|
XML Java Android开发
Android之UI基础控件
Android之UI基础控件
|
28天前
|
Android开发
Android 开发 pickerview 自定义选择器
Android 开发 pickerview 自定义选择器
12 0
|
1月前
|
Android开发
[Android]RadioButton控件
[Android]RadioButton控件
12 0
|
3月前
|
Android开发
分享88个Android控件源代码总有一个是你想要的
分享88个Android控件源代码总有一个是你想要的
23 0