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>

android图片的缩放、圆角处理的更多相关文章

  1. Android 图片的缩放与旋转

    本文实现Android中的图片的缩放效果 首先设计布局: <LinearLayout xmlns:android="http://schemas.android.com/apk/res ...

  2. Android图片的缩放效果

    一.概述 Android 图片要实现:手势滑动,双击变大,多点触控的效果. 其实是有一定难度的,我们需要用Matrix ,GestureDetector 等等需要完成一个复杂的逻辑才能实现,然而今天我 ...

  3. android图片拖动缩放

    这篇图片拖拽缩放也是我在项目中用到的,今天整理一下,将源码奉献给大家,希望对大家以后碰到相似的问题有帮助.android 大图片拖拽缩放 这篇就不做过多介绍了,直接上源码: public class ...

  4. Android图片旋转,缩放,位移,倾斜,对称完整示例(一)——imageView.setImageMatrix(matrix)和Matrix

    MainActivity如下: import android.os.Bundle; import android.view.MotionEvent; import android.view.View; ...

  5. Android图片采样缩放

    为什么要对Android中的图片进行采样缩放呢? 是为了更加高效的加载Bitmap.假设通过imageView来显示图片,很多时候ImageView并没有图片的原始尺寸那么大,这时候把整张图片加载进来 ...

  6. Android图片处理--缩放

    PS:在开发中我们会遇到一些图片处理问题,比如说缓存图片了.限制图片大小了.查看图片了等.上一篇文章介绍了图片的全景效果查看,今天介绍一个图片缩放,我们如果有时间的话,可以自己写一个属于自己的库,里面 ...

  7. Android图片旋转,缩放,位移,倾斜,对称完整演示样例(一)——imageView.setImageMatrix(matrix)和Matrix

    MainActivity例如以下: import android.os.Bundle; import android.view.MotionEvent; import android.view.Vie ...

  8. [android] 图片的缩放

    界面布局,线性布局,竖直排列,两个ImageView 获取到两个ImageView对象 调用BitmapFactory.decodeResource(res,id)方法,获取Bitmap对象 参数:r ...

  9. Android图片上传,可以选择多张图片,缩放预览,拍照上传等

    仿照微信,朋友圈分享图片功能 .可以进行图片的多张选择,拍照添加图片,以及进行图片的预览,预览时可以进行缩放,并且可以删除选中状态的图片 .很不错的源码,大家有需要可以下载看看 . 微信 微信 微信 ...

随机推荐

  1. 刷机,twrp,安装xposed

    首先明白几个名词: recovery模式,类似于pc端的PE系统,每个手机都有自带的rec,但不好用,最好自己刷一个,现在市面最好用的是twrp fastboot模式,比recovery更底层,进入f ...

  2. [LeetCode] 227. 基本计算器 II

    题目链接: https://leetcode-cn.com/problems/basic-calculator-ii 难度:中等 通过率:33.2% 题目描述: 实现一个基本的计算器来计算一个简单的字 ...

  3. HDU-4717 The Moving Points(凸函数求极值)

    The Moving Points Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  4. 2018icpc宁夏邀请赛_L_Continuous Intervals

    题意 给定一个序列,定义连续区间为区间的数排序后,任意两个相邻的数之差不超过1. 分析 假设区间最大值为\(max\),最小值为\(min\),不同数个数为\(cnt\),那么问题转化为求满足\(ma ...

  5. 05.AutoMapper 之配置验证(Configuration Validation)

    https://www.jianshu.com/p/5901a5d1ef15 配置验证(Configuration Validation) 手写映射配置代码虽然繁琐,但具有可测试的优点.AutoMap ...

  6. Solr IK分词器配置

    下载地址:https://search.maven.org/search?q=com.github.magese 分词器配置: 参考:https://www.cnblogs.com/mengjinlu ...

  7. Big Data(七)MapReduce计算框架(PPT截图)

    一.为什么叫MapReduce? Map是以一条记录为单位映射 Reduce是分组计算

  8. 牛客假日团队赛5J 护城河 bzoj 1670: [Usaco2006 Oct]Building the Moat护城河的挖掘 (凸包的周长)

    链接:https://ac.nowcoder.com/acm/contest/984/J 来源:牛客网 护城河 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言6 ...

  9. python snippets

    1.Find memory used by an object import sys 2.Combine a list of strings into a single string strings ...

  10. outlook 使用临时邮箱 使用旧数据

    控制面板-->邮件32位 显示配置文件 删除再添加 具体可参考 https://blog.csdn.net/liuyukuan/article/details/80043840 偷懒,图片从网上 ...