在Android中2D中实现对图片的倒影
在Android中2D中实现对图片的倒影
package com.joe.bitmap;
import Android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.Bitmap.Config;
import android.util.Log;
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();
matrix.setValues(f);
Bitmap bitmap = Bitmap.createBitmap(src, 0, 0,width, height, matrix, false);
height = (int) (height*percent);
int[] pixels = new int[width*height];
bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
shadowFromPixels(pixels, width, height, alphastart);
bitmap =Bitmap.createBitmap(pixels, width, height, Config.ARGB_8888);
return bitmap;
}
}
评论暂时关闭