当前位置:  编程技术>移动开发

Android 图像处理(类型转换,比例缩放,倒影,圆角)的小例子

    来源: 互联网  发布时间:2014-10-16

    本文导语:  1.放大缩小图片 代码如下:public static Bitmap zoomBitmap(Bitmap bitmap,int w,int h){            int width = bitmap.getWidth();            int height = bitmap.getHeight();            Matrix matrix = new Matrix();            float scaleWidht = ((floa...

1.放大缩小图片

代码如下:

public static Bitmap zoomBitmap(Bitmap bitmap,int w,int h){   
        int width = bitmap.getWidth();   
        int height = bitmap.getHeight();   
        Matrix matrix = new Matrix();   
        float scaleWidht = ((float)w / width);   
        float scaleHeight = ((float)h / height);   
        matrix.postScale(scaleWidht, scaleHeight);   
        Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);   
        return newbmp;   
    }


2.获得圆角图片的方法

代码如下:

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap,float roundPx){   

        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);   
        Canvas canvas = new Canvas(output);   

        final int color = 0xff424242;   
        final Paint paint = new Paint();   
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());   
        final RectF rectF = new RectF(rect);   

        paint.setAntiAlias(true);   
        canvas.drawARGB(0, 0, 0, 0);   
        paint.setColor(color);   
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);   

        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));   
        canvas.drawBitmap(bitmap, rect, rect, paint);   

        return output;   
    }


3.获得带倒影的图片方法

代码如下:

public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap){   
       final int reflectionGap = 4;   
       int width = bitmap.getWidth();   
       int height = bitmap.getHeight();   

       Matrix matrix = new Matrix();   
       matrix.preScale(1, -1);   

       Bitmap reflectionImage = Bitmap.createBitmap(bitmap,0, height/2, width, height/2, matrix, false);   

       Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height/2), Config.ARGB_8888);   

       Canvas canvas = new Canvas(bitmapWithReflection);   
       canvas.drawBitmap(bitmap, 0, 0, null);   
       Paint deafalutPaint = new Paint();   
       canvas.drawRect(0, height,width,height + reflectionGap, deafalutPaint);   

       canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);   

       Paint paint = new Paint();   
       LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,
     bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);   
        paint.setShader(shader);   
        // Set the Transfer mode to be porter duff and destination in   
        paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));   
        // Draw a rectangle using the paint with our linear gradient   
        canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()   
                + reflectionGap, paint);   

        return bitmapWithReflection;   
    }

4.将Drawable转化为Bitmap

代码如下:

public static Bitmap drawableToBitmap(Drawable drawable){
      int width = drawable.getIntrinsicWidth();
      int height = drawable.getIntrinsicHeight();
      Bitmap bitmap = Bitmap.createBitmap(width, height,
      drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888: Bitmap.Config.RGB_565);
      Canvas canvas = new Canvas(bitmap);
      drawable.setBounds(0,0,width,height);
      drawable.draw(canvas);
      return bitmap;
}


    
 
 
 
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • Android 倒影算法的实现代码
  • android显示TextView文字的倒影效果实现代码
  • Android 轻松实现图片倒影效果实例代码
  • 申请Android Map 的API Key(v2)的最新申请方式(SHA1密钥)
  • Android瀑布流实例 android_waterfall
  • Android开发需要的几点注意事项总结
  • Android系统自带样式 (android:theme)
  • android 4.0 托管进程介绍及优先级和回收机制
  • Android网络共享软件 Android Wifi Tether
  • Android访问与手机通讯相关类的介绍
  • Android 图标库 Android GraphView
  • Android及andriod无线网络Wifi开发的几点注意事项
  • 轻量级Android开发工具 Android Tools
  • Android 2.3 下StrictMode介绍
  • Android 开发环境 Android Studio
  • IDEA的Android开发插件 idea-android
  • Android手机事件提醒 Android Notifier
  • XBMC的Android客户端 android-xbmcremote
  • Android小游戏 Android Shapes
  • Android电池监控 Android Battery Dog
  • android开发:“android:WindowTitle”没有对应项no resource
  • Android 上类似IOS 的开关控件。 Android ToggleButton
  • Android 将 android view 的位置设为右下角的解决方法
  • Android 2D游戏引擎 Android Angle


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3