根据缩略的宽高比例和设定的最大宽高设置图片的宽高 长图会截取中间一段
来源:互联网 发布:微商版微信是什么软件 编辑:程序博客网 时间:2024/06/04 19:34
/* *
* 根据缩略的宽高比例和设定的最大宽高设置图片的宽高 长图会截取中间一段
*/
public
static
synchronized
Bitmap readBitmap(String key,
int
width,
int
height) {
File bitmapFile =
null
;
Bitmap bitmap =
null
;
boolean
toLongOrWidth =
false
;
boolean
widthbigger =
false
;
try
{
bitmapFile = getFile(key);
}
catch
(FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if
(bitmapFile !=
null
) {
BitmapFactory.Options options =
new
Options();
options.inJustDecodeBounds =
true
;
try
{
BitmapFactory.decodeStream(
new
FileInputStream(bitmapFile),
null
, options);
int
outheight = options.outHeight;
int
outwidth = options.outWidth;
float
rate;
if
(outheight > outwidth) {
rate = (
float
) outheight / (
float
) outwidth;
if
(rate >
2.0
) {
toLongOrWidth =
true
;
}
height = (
int
) (width * rate);
widthbigger =
false
;
}
else
{
rate = (
float
) outwidth / (
float
) outheight;
if
(rate >
2.0
) {
toLongOrWidth =
true
;
}
width = (
int
) (height * rate);
widthbigger =
true
;
}
int
size = calculateInSampleSize(options, width, height);
options.inSampleSize = size;
options.inJustDecodeBounds =
false
;
bitmap = BitmapFactory.decodeStream(
new
FileInputStream(
bitmapFile),
null
, options);
Bitmap dst = Bitmap.createScaledBitmap(bitmap, width, height,
false
);
if
(toLongOrWidth) {
if
(widthbigger) {
int
oldwidth = width;
width = (
int
) ((
float
) height * (
float
)
1.77
);
dst = Bitmap.createBitmap(dst, (oldwidth - width) /
2
,
0
, width, height);
}
else
{
int
oldheight = height;
height = (
int
) ((
float
) width * (
float
)
1.77
);
dst = Bitmap.createBitmap(dst,
0
,
(oldheight - height) /
2
, width, height);
}
Bitmap newb = Bitmap.createBitmap(dst.getWidth(),
dst.getHeight(), Config.ARGB_8888);
Canvas canvasTmp =
new
Canvas(newb);
canvasTmp.drawColor(Color.TRANSPARENT);
Paint p =
new
Paint();
Typeface font = Typeface.create(
"宋体"
, Typeface.BOLD);
p.setAntiAlias(
true
);
// 设置画笔为无锯齿
p.setColor(Color.WHITE);
p.setTypeface(font);
p.setTextSize(ImageUtils.dp2px(HoHoApplication.mAppContext,
(
float
)
12
));
canvasTmp.drawBitmap(dst,
0
,
0
, p);
Paint p2 =
new
Paint();
p2.setColor(Color.parseColor(
"#77000000"
));
Rect r =
new
Rect(
0
, dst.getHeight()
- ImageUtils.dp2px(HoHoApplication.mAppContext,
(
float
)
24
), dst.getWidth(),
dst.getHeight());
canvasTmp.drawRect(r, p2);
canvasTmp.drawText(
"长图"
,
dst.getWidth()
/
2
- ImageUtils.dp2px(
HoHoApplication.mAppContext,
(
float
)
12
),
dst.getHeight()
- ImageUtils.dp2px(
HoHoApplication.mAppContext,
(
float
)
6
), p);
canvasTmp.save(Canvas.ALL_SAVE_FLAG);
canvasTmp.restore();
dst = newb;
}
if
(bitmap != dst) {
// 如果没有缩放,那么不回收
bitmap.recycle();
// 释放Bitmap的native像素数组
}
return
dst;
}
catch
(FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return
bitmap;
}
0 0
- 根据缩略的宽高比例和设定的最大宽高设置图片的宽高 长图会截取中间一段
- 根据图片的宽高比例设置imageView 的size
- 根据屏幕宽高按指定比例设置控件的宽高
- 根据图片比例设置控件的宽高,避免图片拉伸
- flex module 宽高的比例设置
- 动态设置View的宽高、宽高比例(设置ViewPager的高度,宽高比例为2:1)
- 控制图片比例、等比例控制图片最大宽高。
- 。net 根据高宽比例压缩图片,并给简单的文字水印……
- android将给定图片维持宽高比缩放后,截取正中间的正方形部分
- android将给定图片维持宽高比缩放后,截取正中间的正方形部分
- 一个非常好用的图片等比例缩放到指定最大高宽的函数
- html根据宽的百分比设置高
- fullscreen图片全屏并保持宽高比例的背景图
- 等比例显示图片的大小,宽高自适应
- 自定义按照图片宽高比例缩放的RadioLayout
- listview的条目上的图片按照宽高比例进行缩放设置
- 自定义宽高比例的布局控件
- 等比例宽高的ImageView
- MaterialDesin
- Android如何查看手机网卡信息和ip信息
- Android使用MediaPlayer播放音乐文件
- hibernate 映射组件 和 使用类集合,映射其他的表
- 自适应辛普森模板
- 根据缩略的宽高比例和设定的最大宽高设置图片的宽高 长图会截取中间一段
- Intellij Idea导入eclipse项目和依赖处理
- 设置vpn
- jbox读取请稍候 设置与关闭
- ExtJS弹窗刷新父页面
- BareTail工具查看Mysql实时执行的Sql语句
- C++基础知识的总结(主要针对面试)
- angular错误解决
- Android studio工程中增加自定义资源编译目录