android图片的缩放、圆角处理
生活随笔
收集整理的這篇文章主要介紹了
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" /> <solidandroid:color="#000"/><padding android:top="5dp"android:bottom="5dp"/></shape>?
?
?
?
?
轉載于:https://www.cnblogs.com/bobodeboke/p/3182819.html
總結
以上是生活随笔為你收集整理的android图片的缩放、圆角处理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java.io.FileNotFound
- 下一篇: 网页设计表格单元格线条及边框设置