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>
android图片的缩放、圆角处理的更多相关文章
- Android 图片的缩放与旋转
本文实现Android中的图片的缩放效果 首先设计布局: <LinearLayout xmlns:android="http://schemas.android.com/apk/res ...
- Android图片的缩放效果
一.概述 Android 图片要实现:手势滑动,双击变大,多点触控的效果. 其实是有一定难度的,我们需要用Matrix ,GestureDetector 等等需要完成一个复杂的逻辑才能实现,然而今天我 ...
- android图片拖动缩放
这篇图片拖拽缩放也是我在项目中用到的,今天整理一下,将源码奉献给大家,希望对大家以后碰到相似的问题有帮助.android 大图片拖拽缩放 这篇就不做过多介绍了,直接上源码: public class ...
- Android图片旋转,缩放,位移,倾斜,对称完整示例(一)——imageView.setImageMatrix(matrix)和Matrix
MainActivity如下: import android.os.Bundle; import android.view.MotionEvent; import android.view.View; ...
- Android图片采样缩放
为什么要对Android中的图片进行采样缩放呢? 是为了更加高效的加载Bitmap.假设通过imageView来显示图片,很多时候ImageView并没有图片的原始尺寸那么大,这时候把整张图片加载进来 ...
- Android图片处理--缩放
PS:在开发中我们会遇到一些图片处理问题,比如说缓存图片了.限制图片大小了.查看图片了等.上一篇文章介绍了图片的全景效果查看,今天介绍一个图片缩放,我们如果有时间的话,可以自己写一个属于自己的库,里面 ...
- Android图片旋转,缩放,位移,倾斜,对称完整演示样例(一)——imageView.setImageMatrix(matrix)和Matrix
MainActivity例如以下: import android.os.Bundle; import android.view.MotionEvent; import android.view.Vie ...
- [android] 图片的缩放
界面布局,线性布局,竖直排列,两个ImageView 获取到两个ImageView对象 调用BitmapFactory.decodeResource(res,id)方法,获取Bitmap对象 参数:r ...
- Android图片上传,可以选择多张图片,缩放预览,拍照上传等
仿照微信,朋友圈分享图片功能 .可以进行图片的多张选择,拍照添加图片,以及进行图片的预览,预览时可以进行缩放,并且可以删除选中状态的图片 .很不错的源码,大家有需要可以下载看看 . 微信 微信 微信 ...
随机推荐
- Hive开发中使用变量的两种方法
在使用hive开发数据分析代码时,经常会遇到需要改变运行参数的情况,比如select语句中对日期字段值的设定,可能不同时间想要看不同日期的数据,这就需要能动态改变日期的值.如果开发量较大.参数多的话, ...
- python变量的内存管理
python变量的内存管理 一.变量存在了哪里? 先让我们来看一段代码: height = 100 # 定义变量 # print(100) # print会自动帮你创建一个变量100,打印完之后,马上 ...
- 139. 回文子串的最大长度(回文树/二分,前缀,后缀和,Hash)
题目链接 : https://www.acwing.com/problem/content/141/ #include <bits/stdc++.h> using namespace st ...
- RabbitMQ入门教程(四):工作队列(Work Queues)
原文:RabbitMQ入门教程(四):工作队列(Work Queues) 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https:/ ...
- 07.AutoMapper 之列表和数组(Lists and Arrays)
https://www.jianshu.com/p/419a3b7f12d5 列表和数组(Lists and Arrays) AutoMapper只需要配置元素类型的映射配置,不需要针对列表和数组进行 ...
- WebView获取title更改
[self.titleLabel setText:[self.webVIew stringByEvaluatingJavaScriptFromString:@"document.title& ...
- 程序员称为高手的10条心得(摘自http://www.jizhuomi.com/software/394.html)
在这个世界上,有数百万的人热衷于软件开发,他们有很多名字,如:软件工程师(Software Engineer),程序员(Programmer),编码人(Coder),开发人员(Developer).经 ...
- Centos7安装RocketMQ4.4
网上的教程坑贼鸡儿多 一.安装maven RocketMQ依赖maven打包,所以先要在虚拟机中安装maven,我使用的是v3.3.9. 1:进入指定目录下载maven 包 cd /usr/local ...
- mariadb主从架构
mariadb主从架构(异步)和集群 一般应用的场所是网站,主的机器是可以写可以读,从的机器可以读,也可以写,但不会同步.只有主的机器增删改,从的机器才会同步. 主从至少三个线程:dump.I/O t ...
- php函数nl2br的反函数br2nl 将html中的br换行符转换为文本输入中的换行符
下面这几个方法将能够帮你解决这个问题. PHP版将html中的<br />换行符转换为文本框中的换行符: 代码如下: function br2nl($text){ return preg_ ...