设为首页 加入收藏

TOP

在Android中2D中实现对图片的倒影(一)
2014-11-24 03:24:58 来源: 作者: 【 】 浏览:3
Tags:Android 实现 图片 倒影

package com.joe.bitmap;


public class BitmapUtils {
private static final String TAG = "BitmapUtils";


/**
* Set the pixels's alpha. alpha's value decrease by the bit's height
* @param pixels
* the array which contains pixels.
* @param srcWidth
* the bitmap's width
* @param srcHeight
* the bitmap's height
* @param alphastart
* the first pixels's alpha
* @param percent
* the line's fouction's slope (y = mx+b;)
* such as: alphastart=0x2fffffff;(alpha's value 0x2f)
* m = 0x2f/(height * percent) (if the shadow fill full of the
* bitmap,k =1. otherwise the shadow only in a half of the bitmap
* percent = 0.5) alpha = m*height + alphaLagest
* The percent max value are 1!
* @param b
* the line's fouction's const (y = mx+b)
*/
public static void shadowFromPixels(int[] pixels, int srcWidth,
int srcHeight, int alphastart, float percent) {
int b = alphastart;
//if need display a part of (such as 1/2,1/3...) bitmap
int alpahLine = (int) (srcHeight * percent);

float temp = (alphastart >>> 24);
percent = ((temp / (srcHeight * percent)));
int alpha = 0;

Log.d(TAG, "shadowByPixels: percent=" + percent + "\tb=" + b
+ "\t temp=" + temp + "\tsrcHeight=" + srcHeight);

for (int i = 0; i < srcHeight; i++) {
if(i < alpahLine){
alpha = b - (((int) (i * percent)) << 24);
}else{
alpha = 0x00ffffff;
}
for (int j = 0; j < srcWidth; j++) {
pixels[j + i * srcWidth] = pixels[j + i * srcWidth] & alpha;
}
}
}


/**
* Set the pixels's alpha. alpha's value decrease by the bit's height
*
* @param pixels the array which contains pixels.
* @param srcWidth the bitmap's width
* @param srcHeight the bitmap's height
* @param alphastart the first pixels's alpha
*/
public static void shadowFromPixels(int[] pixels, int srcWidth,
int srcHeight, int alphastart) {
int b = alphastart;
float temp = (alphastart >>> 24);
float percent = (temp / srcHeight) ;
int alpha = 0;
Log.d(TAG, "shadowByPixels: percent=" + percent + "\tb=" + b
+ "\t temp=" + temp + "\tsrcHeight=" + srcHeight);
for (int i = 0; i < srcHeight; i++) {
alpha = b -(((int) (i * percent)) << 24);
for (int j = 0; j < srcWidth; j++) {
pixels[j + i * srcWidth] = pixels[j + i * srcWidth] & alpha;
}
}
}

//我的这个 算法有点问题:
我只考虑到 图片中每一行的alpha值是相同的,大多数图片是这样,但是有些图片不是哦.
这里是把每一行的alpha值,重新设置成了一个值,这是不能通用的.
通用的做法是把 这行的alpha值取出来,乘以一个比例
当前行号 = a,(0~a总)
需要显示总的行号 = a总



这个比例= ((a总-a)/a总) * 当前行的alpha值.


//这个为,封装的对外的接口.
//实现倒影首先 实现对图片的倒转...通过矩阵来实现.因为如果A点在图片的最左上角(0,0)
//倒转后 则坐标变为(0,height) 而最左下角由(0,height) 变为(0,0) 倒转关系为 (x,height-y)
// 把图片中的像素取出来,每行像素相同,不同的行根据height递增而alpha递减.(如果像素是RGB_8888编码方式的话
// 0x88ffffff 88就表示alpha值ff为完全不透明,00为完全透明 )
// y = mx+b
//y为某行的alpha值. m为height b为初始alpha值.
public static Bitmap shadowFromBitmap(Bitmap src,int alphastart, float percent){
int width = src.getWidth();
int height = (int) (src.getHeight());
float f[] = {1.0F,0.0F,0.0F,0.0F,-1.0F,height,0.0F,0.0F,1.0F};
//这里实现倒转
Matrix matrix = new Matrix();

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Android 通用获取Ip的方法 下一篇Android 图片透明度处理代码

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·你必须要弄懂的多线 (2025-12-25 04:22:35)
·如何在 Java 中实现 (2025-12-25 04:22:32)
·Java【多线程】单例 (2025-12-25 04:22:29)
·C++中智能指针的性能 (2025-12-25 03:49:29)
·如何用智能指针实现c (2025-12-25 03:49:27)