Android生成倒影效果

网上同类的例子很多,但给出的解释不多,而且没有通用性。
我在借鉴网友例子的基础上,给出了一个更具通用性的实现。

首先明确一些基本条件:
  * 原图与倒影的宽度是一样的;
  * 二者在垂直方向上可以有一点距离(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));
              }

至此,你可以欣赏你的作品啦。



转自http://blog.sina.com.cn/s/blog_3e3fcadd0101avgn.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值