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. redis 字符串 数据类型

    1 字符串 设置:   set    key  value   获取:  get  key    删除:  del   key getrange   key    0 3    截取字符串内容    ...

  2. 多线程之小米商店APP爬虫

    #今日目标 **多线程之小米商店APP爬虫** 爬取小米商店所有社交APP ``` import requests import time from threading import Thread f ...

  3. allure 这么高大上的测试报告环境,5 分钟搞定

    allure 的测试报告是老板喜欢的样子.如果能用上 allure,干嘛还选择其他的测试报告类型呢?python 的 pytest 单元测试框架有 allure 的插件,可以很方便的在 python ...

  4. C# 方法,属性,字段

    以前是学C++的,初次学微软的C#头都大了.什么字段,常量,属性,方法......微软把别人的东西拿来糅合在C#里,弄成了一个“大杂烩”.其实,说到底,“字段”不就是“变量”吗,所谓的“方法”不就是“ ...

  5. 13.AutoMapper 之映射前后(Before and After Map Action)

    https://www.jianshu.com/p/1ff732094f21 映射前后(Before and After Map Action) 你可能偶尔需要在映射发生前后执行自定义逻辑.这应该很少 ...

  6. 12.AutoMapper 之Null替换(NullSubstitution)

    https://www.jianshu.com/p/3f86f237d1db Null 替换(Null Substitution) Null 替换允许当源类型成员在成员链任何位置为Null时给目标成员 ...

  7. luogu P4548 [CTSC2006]歌唱王国

    传送门 这题\(\mathrm{YMD}\)去年就讲了,然而我今年才做(捂脸) 考虑生成函数,设\(f_i\)表示最终串长为\(i\)的概率,其概率生成函数为\(F(x)=\sum f_ix^i\), ...

  8. openstack docker build error

    1. _proto_tcp = socket.getprotobyname('tcp') OSError: protocol not found you should have a /etc/prot ...

  9. (转)linux chattr lsattr 命令

    转http://www.ha97.com/5172.html PS:有时候你发现用root权限都不能修改某个文件,大部分原因是曾经用chattr命令锁定该文件了.chattr命令的作用很大,其中一些功 ...

  10. 一、Vue CLI

    一.Vue CLI https://cli.vuejs.org/zh/guide/installation.html 介绍: 二.安装 # 安装 Vue Cli npm install -g @vue ...