通过一张现有的Bitmap,画出一张同样的但是大小使我们指定的Bitmap

需求:直接createBitmap的话不允许生成的bitmap的宽高大于原始的,因此需要特定方法来将一张Bitmap的大小进行调整

crossImage为一张现有的bitmap

Bitmap target = Bitmap.createBitmap(MIDDLE_LINE_WIDTH, MIDDLE_LINE_WIDTH, crossImage.getConfig());

Canvas temp_canvas = new Canvas(target);

temp_canvas.drawBitmap(crossImage, null, new Rect(0, 0, target.getWidth(), target.getHeight()), null);

此时的target就是一张指定大小,但是内容和crossImage一样的bitmap了。

//使用Bitmap加Matrix来缩放

public static Drawable resizeImage(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 new BitmapDrawable(resizedBitmap);
}

//使用BitmapFactory.Options的inSampleSize参数来缩放

public static Drawable resizeImage2(String path,

int width,int height)

{

BitmapFactory.Options options = new BitmapFactory.Options();

options.inJustDecodeBounds = true;//不加载bitmap到内存中

BitmapFactory.decodeFile(path,options);

int outWidth = options.outWidth;

int outHeight = options.outHeight;

options.inDither = false;

options.inPreferredConfig = Bitmap.Config.ARGB_8888;

options.inSampleSize = 1;

    if (outWidth != 0 && outHeight != 0 && width != 0 && height != 0)
{
int sampleSize=(outWidth/width+outHeight/height)/2;
Log.d(tag, "sampleSize = " + sampleSize);
options.inSampleSize = sampleSize;
} options.inJustDecodeBounds = false;
return new BitmapDrawable(BitmapFactory.decodeFile(path, options));
}

根据现有Bitmap生成相同图案指定大小的新Bitmap的更多相关文章

  1. iOS二维码、条形码生成(可指定大小、颜色)

    一.前言: iOS7.0之后可以利用系统原生 API 生成二维码, iOS8.0之后可以生成条形码, 系统默认生成的颜色是黑色. 在这里, 利用以下方法可以生成指定大小.指定颜色的二维码和条形码, 还 ...

  2. PHP JS HTML ASP页面跳转代码 延时跳转代码 返回到上一界面并刷新 JS弹出指定大小的新窗口

    1.PHP延时跳转代码 //跳转到浏览界面 header("Refresh:1;url=machine_list.php"); //不延时 <?php header(&quo ...

  3. java将list分为指定大小的新集合

    上代码: import java.util.ArrayList; import java.util.List; public class JayCommonUtil { /** * 按指定大小,分隔集 ...

  4. 打开指定大小的新窗口和window.open参数

    用法: <SCRIPT LANGUAGE="javascript">   window.open ('要打开的路径', '窗口名称', '参数列表');</SCR ...

  5. dd 生成指定大小文件

    d命令可以轻易实现创建指定大小的文件,如 dd if=/dev/zero of=test bs=1M count=1000 会生成一个1000M的test文件,文件内容为全0(因从/dev/zero中 ...

  6. PHP 生成指定大小随机图片

    PHP 生成指定大小随机图片 <?php $image_width = 100; $image_height = 100; $image_str = ''; if (isset($_GET['w ...

  7. Java、Linux、Win 快速生成指定大小的空文件

    Linux dd 命令: dd if=/dev/zero of=<fileName> bs=<一次复制的大小> count=<复制的次数> 生成 50 MB 的空文 ...

  8. linux生成指定大小的文件(转)

    # dd if=/dev/zero of=50M.file bs=1M count=50在当前目录下生成一个50M的文件 虚拟块设备文件更通用的名称是硬盘镜像文件(Hard Disk Image),但 ...

  9. 自定义控件,上图下字的Button,图片任意指定大小

    最近处在安卓培训期,把自己的所学写成博客和大家分享一下,今天学的是这个自定义控件,上图下字的Button安卓自带,但是苦于无法设置图片大小(可以在代码修改),今天自己做了一个,首先看一下效果图,比较实 ...

随机推荐

  1. Win32 Sdk 连接Access数据库

    /************************************************************* *** MyWinClass.cpp 创建窗口模板 *** vs2017+ ...

  2. XCode Interface Builder开发——2

    XCode Interface Builder开发--2 简单的练手项目--仿苹果自备的计算器 简介 制作一个简易功能的计算器并非难事,但是其中要考虑的不同情况却仍有许多,稍不留神就会踩坑. 例如: ...

  3. [tgpl]go匿名函数

    [tgpl]go匿名函数 0. 定义 匿名函数顾名思义是没有名字的函数, Named functions can be declared only at the package level, but ...

  4. React:redux+router4搭建应用骨架

    可能是短期内关于react的对后一篇笔记.假设读者对redux和router4有基本了解. 缘由: 现在网上很多关于react+redux的文章都是沿用传统的文件组织形式,即: |--componen ...

  5. Codeforces1141E(E题)Superhero Battle

    A superhero fights with a monster. The battle consists of rounds, each of which lasts exactly nn min ...

  6. mysql操作之二:fetchone与获取lastrowid

    import mySQLdb conn = mySQLdb.connect(host='127.0.0.1',user='root',passwd='123456')cur = conn.cursor ...

  7. ajax提交可以上传文件的form表单

    var formData = new FormData($( "#fm")[0]);       $.ajax({            url: 'webnavigationcw ...

  8. PAT-1080 Graduate Admission (结构体排序)

    1080. Graduate Admission It is said that in 2013, there were about 100 graduate schools ready to pro ...

  9. Java Serializable(序列化)的总结

    1.序列化是干什么的? 简单说就是为了保存在内存中的各种对象的状态(也就是实例变量,不是方法),并且可以把保存的对象状态再读出来.虽然你可以用你自己的各种各样的方法来保存object states,但 ...

  10. 基于 abp vNext 和 .NET Core 开发博客项目 - 定时任务最佳实战(二)

    上一篇(https://www.cnblogs.com/meowv/p/12971041.html)使用HtmlAgilityPack抓取壁纸数据成功将图片存入数据库,本篇继续来完成一个全网各大平台的 ...