zoukankan      html  css  js  c++  java
  • android图片的缩放、圆角处理

    android中图片缩放方法有三种:1,bitmapFactory;2,bitmap+metrix;3,thumbUtil

    方法一:bitmapFactory:

    public static Bitmap resizeBitmapByFactory(String path, int w, int h) {
            BitmapFactory.Options option = new BitmapFactory.Options();
            option.inJustDecodeBounds = true;
            Bitmap bitmap = BitmapFactory.decodeFile(path, option);
            int outWidth = option.outWidth;
            int outHeight = option.outHeight;
            option.inDither = false;
            option.inPreferredConfig = Bitmap.Config.ARGB_8888;
    
            if (outWidth != 0 && outHeight != 0 && w != 0 && h != 0) {
                int sampleSize = (outWidth / w + outHeight / h) / 2;
                option.inSampleSize = sampleSize;
            }
    
            option.inJustDecodeBounds = false;
            return BitmapFactory.decodeFile(path, option);
    
        }

    方法二:bitmap+metrix

    public static Bitmap resizeBitmapByMetrix(Bitmap bitmap, int w, int h) {
            Bitmap BitmapOrg = bitmap;
            int width = BitmapOrg.getWidth();
            int height = BitmapOrg.getHeight();
            int newWidth = w;
            int newHeight = h;
    
            float scaleWidth = ((float) newWidth) / width;
            float scaleHeight = ((float) newHeight) / height;
    
            Matrix matrix = new Matrix();
            matrix.postScale(scaleWidth, scaleHeight);
            // if you want to rotate the Bitmap
            // matrix.postRotate(45);
            Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0, 0, width,
                    height, matrix, true);
            return resizedBitmap;
        }

    方法三,使用系统自带的thumbutil:

    public static Bitmap resizeBitmapByUtil(Bitmap bitmap, int w, int h) {
            return ThumbnailUtils.extractThumbnail(bitmap, w, h);
        }

    三个方法消耗的时间为:72,9,13不过怀疑方法一消耗的时间有可能是由于从文件中加载图片所致。这点待定

    二、实现图片的圆角效果

    public static Bitmap toRoundCornerBitmap(Bitmap bitmap, int cornerPixel) {
            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);
    
            final float roundPx = cornerPixel;
    
            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;
        }

    三、实现图片缩放,背景为圆角图片,则这个背景可以用图形资源文件来实现。

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" 
        android:shape="rectangle">
        <gradient  
            android:angle="90"  
            android:endColor="#00000000"  
            android:startColor="#00000000" />  
      
        <corners  
            android:bottomLeftRadius="12dp"  
            android:bottomRightRadius="12dp"  
            android:topLeftRadius="12dp"  
            android:topRightRadius="12dp" />  
      
        <stroke  
            android:width="1dip"  
            android:color="#eee" />  
        <solid
            android:color="#000"/>
        <padding 
            android:top="5dp"
            android:bottom="5dp"/>
        
    
    </shape>
  • 相关阅读:
    MySQL-EXPLAIN执行计划字段解释
    MySQL-EXPLAIN执行计划Extra解释
    HTTP与HTTPS的区别
    【面试】Java中sleep和wait的区别
    Nginx之前后端分离(入门)
    玩程序 之 一 . 字符串处理工具(可通过C#脚本扩展)
    C#实现下载功能,可用于软件自动更新
    对c#剪切板Clipboard占用的问题一点解决方法
    C# 制作 仪表
    C#使用自定义字体(从文件获取)
  • 原文地址:https://www.cnblogs.com/bobodeboke/p/3182819.html
Copyright © 2011-2022 走看看