网上同类的例子很多,但给出的解释不多,而且没有通用性。
我在借鉴网友例子的基础上,给出了一个更具通用性的实现。
首先明确一些基本条件:
* 原图与倒影的宽度是一样的;
* 二者在垂直方向上可以有一点距离(1-2dp);
* 二者的高度一般是不一样的,且倒影高度应比较小;
* 二者在水平方向上的压缩比要相同,否则二者不对称;
* 倒影在垂直方向上是渐变的,即越向下越暗;
基于此,上下相邻2个ImageView的宽度要一致,且scaleType都应设为 fitXY 。
我们有如下的布局(原图高度:倒影高度 = 3:1):
android:id="@+id/screen0"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:id="@+id/image0"
android:src="@drawable/main_icon"
android:scaleType="fitXY"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="2dp"
android:layout_weight="1"
/>
android:id="@+id/image0_reflection"
android:src="@drawable/main_icon"
android:scaleType="fitXY"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="2dp"
android:layout_weight="3"
/>
以下是计算倒影的过程以及所用到的函数(感谢网友的贡献):
staticpublic Bitmap setAlpha(Bitmap sourceImg, int alphaPercent){
try {
int[] argb = new int[sourceImg.getWidth() *sourceImg.getHeight()];
sourceImg.getPixels(argb, 0, sourceImg.getWidth(), 0,0,sourceImg.getWidth(),sourceImg.getHeight());
alphaPercent = alphaPercent * 255 /100;
double round = (double)alphaPercent/(double)(argb.length);
for (int i = 0; i < argb.length; i++){
if((alphaPercent - i*round) > 10) {
argb[i] = ((int)(alphaPercent-i*round) << 24) | (argb[i]& 0x00FFFFFF);
continue;
} else{
argb[i] = (10 << 24) | (argb[i] & 0x00FFFFFF);
continue;
}
}
sourceImg = Bitmap.createBitmap(argb, sourceImg.getWidth(),sourceImg.getHeight(),Config.ARGB_8888);
return sourceImg;
} catch (Exception e) {
// do nothing!
}
return null;
}
publicstatic Bitmap setShadow(Bitmap bitmap, int heightPercent) {
try {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Matrix matrix = new Matrix();
matrix.preScale(1, -1); // upside down
int newWidth = width;
int newHeight = (int)((float)height * heightPercent /100.0f);
int fromX = 0;
int fromY = height - newHeight;
//
Bitmap shadowImage = Bitmap.createBitmap(bitmap, fromX, fromY,newWidth, newHeight, matrix, false);
return shadowImage;
} catch (Exception e) {
// do nothing.
}
return null;
}
以下是利用上面的函数来计算倒影。这里把倒影与原图的高度比与透明度(alpha)都设成常数了,不过你很容易把它们改成参数:
static finalint heightPercent = 40;
static finalint alphaPercent = 80;
publicstatic Bitmap getReflection(Bitmap bitmap) {
if(bitmap ==null)
returnnull;
Bitmap newBitmap = setShadow(bitmap, heightPercent);
if(newBitmap != null)
newBitmap = setAlpha(newBitmap, alphaPercent);
return newBitmap;
}
下面是一个小函数,用来从asset中获得一个Bitmap对象:
staticpublic Bitmap getBitmapFromAssetFile(Context context, StringassetFileName) {
if(context== null || TextUtils.isEmpty(assetFileName))
returnnull;
Bitmapbitmap = null;
try {
InputStreamis = context.getAssets().open(assetFileName);
bitmap =BitmapFactory.decodeStream(is);
} catch(Exception e) {
e.printStackTrace();
bitmap =null;
}
returnbitmap;
}
好了,你可以利用上面的函数来显示一个图片的倒影了(基于上面的布局):
ImageView
image0 =(ImageView)findViewById(R.id.image0);
ImageView
image0_reflection =(ImageView)findViewById(R.id.image0_reflection);
bitmap0 = getBitmapFromAssetFile(this, "lovely_doggy.jpg");
if(bitmap0 != null) {
image0.setImageBitmap(bitmap0);
image0_reflection.setImageBitmap(getReflection(bitmap0));
}
我在借鉴网友例子的基础上,给出了一个更具通用性的实现。
首先明确一些基本条件:
* 二者的高度一般是不一样的,且倒影高度应比较小;
基于此,上下相邻2个ImageView的宽度要一致,且scaleType都应设为 fitXY 。
我们有如下的布局(原图高度:倒影高度 = 3:1):
以下是计算倒影的过程以及所用到的函数(感谢网友的贡献):
以下是利用上面的函数来计算倒影。这里把倒影与原图的高度比与透明度(alpha)都设成常数了,不过你很容易把它们改成参数:
下面是一个小函数,用来从asset中获得一个Bitmap对象:
好了,你可以利用上面的函数来显示一个图片的倒影了(基于上面的布局):
至此,你可以欣赏你的作品啦。
转自http://blog.sina.com.cn/s/blog_3e3fcadd0101avgn.html