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

2016-02-19 09:34 4 1 收藏

生活已是百般艰难,为何不努力一点。下面图老师就给大家分享Android 图像处理(类型转换,比例缩放,倒影,圆角)的小例子,希望可以让热爱学习的朋友们体会到设计的小小的乐趣。

【 tulaoshi.com - 编程语言 】

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.获得圆角图片的方法

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

代码如下:

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;   
    }

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

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;
}

来源:https://www.tulaoshi.com/n/20160219/1591285.html

延伸阅读
(效果如上图所示) 其实很简单: 比方说上面的容器是一个ListView 代码如下: ListView android:id="@+id/listView_devices" android:layout_width="fill_parent" android:layout_height="fill_parent" SPAN style="COLOR: #ff0000" android:background="@android:drawable/dialog_frame"/SPAN android:cacheColorHint="@color/tran...
标签: 浏览器
IE如何保持设定好的缩放比例 1、开启 Internet Explroer,按工具,选择Internet 选项。 2、在Internet 选项窗口中切换到高级选项卡,取消勾选对于新的窗口和选项卡,重设缩放级别,再单击确认保存设置即可。
在Android中使用ImageView显示图片的时候发现图片显示不正,方向偏了或者倒过来了。 解决这个问题很自然想到的分两步走: 1、自动识别图像方向,计算旋转角度; 2、对图像进行旋转并显示。 一、识别图像方向 首先在这里提一个概念EXIF(Exchangeable Image File Format,可交换图像文件),具体解释参见Wiki。 简而言之,Exif是一个标准,...
标签: Web开发
早上起来就在想这个问题,一直到公司也没想出个法,真愁。 对于我这种JS的白痴来说,有点费劲。 试了N久后:width:expression(this.width 100 && this.width this.height ? 100 : true); height: expression(this.height 100 ? 100 : true); 支持IE6 FF 和IE7
标签: Web开发
按比例缩小或者放大到某个尺寸,对于标准浏览器(如Firefox),或者最新都IE7浏览器, 直接使用max-width,max-height;或者min-width,min-height的CSS属性即可。如: img{max-width:100px;max-height:100px;} img{min-width:100px;min-height:100px;} 对于IE6及其以下版本的浏览器,则可以利用其支持的expression属性,在css co...

经验教程

92

收藏

68
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部