日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android图片的缩放、圆角处理

發(fā)布時間:2025/3/15 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android图片的缩放、圆角处理 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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

方法三,使用系統(tǒng)自帶的thumbutil:

?

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

三個方法消耗的時間為:72,9,13不過懷疑方法一消耗的時間有可能是由于從文件中加載圖片所致。這點待定

二、實現(xiàn)圖片的圓角效果

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

三、實現(xiàn)圖片縮放,背景為圓角圖片,則這個背景可以用圖形資源文件來實現(xiàn)。

<?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>

?

?

?

?

?

轉(zhuǎn)載于:https://www.cnblogs.com/bobodeboke/p/3182819.html

總結(jié)

以上是生活随笔為你收集整理的android图片的缩放、圆角处理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。