本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处!

写完上一篇后,总认为介绍的知识点不多,仅仅是一种在UI线程解析载入图片。两种在子线程解析,在UI线程载入图片,就这个差别。

关于图片和ImageView,事实上有很多其它能够介绍的。比方在解析图片前获得图片的宽高。用来做图片适配。比方等比例缩小图片,以减小内存占用;比方图片旋转效果等等。

第一个问题和第二个问题:

BitmapFactory.Options options = new BitmapFactory.Options();
/*
* If set to true, the decoder will return null (no bitmap), but the
* out... fields will still be set, allowing the caller to query the
* bitmap without having to allocate the memory for its pixels.
*/
options.inJustDecodeBounds = true;
String url=Environment.getExternalStorageDirectory().getPath() +"/test.jpg";//sdcard/test.jpg
BitmapFactory.decodeFile(url,options);
int outWidth=options.outWidth;//获得图片的宽
int outHeight=options.outHeight;//获得图片的高
int width=100;//放图片组件的宽
int height=100;//放图片组件的高
double shink=outHeight*outWidth/width/height;//缩小的比例
options.inSampleSize=(int) Math.sqrt(shink);//inSampleSize设置,则缩小比例即它的平方,如2则比例为1/4。

Bitmap bitmap=BitmapFactory.decodeFile(url, options);//此值就是我们所须要的值

下面是官方提供的方法:
    public static int computeSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) {
        int initialSize = computeInitialSampleSize(options, minSideLength, maxNumOfPixels);
        int roundedSize;
        if (initialSize <= 8) {
            roundedSize = 1;
            while (roundedSize < initialSize) {
                roundedSize <<= 1;
            }
        } else {
            roundedSize = (initialSize + 7) / 8 * 8;
        }
        return roundedSize;
    }

private static int computeInitialSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) {
        double w = options.outWidth;
        double h = options.outHeight;
        int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
        int upperBound = (minSideLength == -1) ?

128 : (int) Math.min(Math.floor(w / minSideLength), Math.floor(h / minSideLength));
        if (upperBound < lowerBound) {
            // return the larger one when there is no overlapping zone.
            return lowerBound;
        }
        if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
            return 1;
        } else if (minSideLength == -1) {
            return lowerBound;
        } else {
            return upperBound;
        }
    }

第三个问题:Matrix来翻转、剪切图片之后再讲。
matrix通过设置postRotate来进行翻转。不管45度还是90度
Matrix matrix = new Matrix();
matrix.postRotate(45);
int x=0;//The x coordinate of the first pixel in source
int y=0;// The y coordinate of the first pixel in source
int width=bitmap.getWidth();//The number of pixels in each row
int height=bitmap.getHeight();// The number of rows
Bitmap finalBitmap = Bitmap.createBitmap(bitmap, x, y, width ,height, matrix, true);
mImg.setImageBitmap(finalBitmap);
剪切主要改变x,y,width和height,来获得对应尺寸和图形的Bitmap
matrix.postRotate的结果例如以下图
matrix.postScale(1,-1);倒立的结果例如以下图

第四个问题:Bitmap.Config几种类型间的差别

A:alpha R:red G:green B:blue

ARGB_8888 32位,8byte每一个像素 透明度为8

ARGB_4444 16位。4byte每一个像素 透明度为4

ALPHA_8     8位,1byte每一个像素 透明度为8 没有颜色

RGB_565    16位,2byte每一个像素 无透明度

第五个问题:canvas绘图

clipRect:要求画制的区域,有left、right、top、bottom4个參数(相对父布局而言,scrollTo同理)

drawArc:画弧线

drawRect:画长方形

drawCircle:画圆

drawLine:画线

通过clipRect等对画布进行设置,能够有效防止在看不见的时候还绘制UI。降低图片渲染的次数
src设置图片内容。不会拉伸,能够通过设置alpha来显示透明度;使用scaleType来对其缩放。比方设置crop_start,则须要在配置文件写fit_xy保证图片宽和高被正确计算,在代码中设置crop_start才有效
background设置图片背景,会拉伸
adjustViewBounds用来设置图片大小,又想设置宽高比的场景

本文来自http://blog.csdn.net/liuxian13183/ 。引用必须注明出处。

写完上一篇后。总认为介绍的知识点不多,仅仅是一种在UI线程解析载入图片,两种在子线程解析。在UI线程载入图片,就这个差别。

关于图片和ImageView。事实上有很多其它能够介绍的,比方在解析图片前获得图片的宽高,用来做图片适配;比方等比例缩小图片,以减小内存占用;比方图片旋转效果等等。

第一个问题和第二个问题:

BitmapFactory.Options options = new BitmapFactory.Options();
/*
* If set to true, the decoder will return null (no bitmap), but the
* out... fields will still be set, allowing the caller to query the
* bitmap without having to allocate the memory for its pixels.
*/
options.inJustDecodeBounds = true;
String url=Environment.getExternalStorageDirectory().getPath() +"/test.jpg";//sdcard/test.jpg
BitmapFactory.decodeFile(url,options);
int outWidth=options.outWidth;//获得图片的宽
int outHeight=options.outHeight;//获得图片的高
int width=100;//放图片组件的宽
int height=100;//放图片组件的高
double shink=outHeight*outWidth/width/height;//缩小的比例
options.inSampleSize=(int) Math.sqrt(shink);//inSampleSize设置。则缩小比例即它的平方,如2则比例为1/4。

Bitmap bitmap=BitmapFactory.decodeFile(url, options);//此值就是我们所须要的值

下面是官方提供的方法:
    public static int computeSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) {
        int initialSize = computeInitialSampleSize(options, minSideLength, maxNumOfPixels);
        int roundedSize;
        if (initialSize <= 8) {
            roundedSize = 1;
            while (roundedSize < initialSize) {
                roundedSize <<= 1;
            }
        } else {
            roundedSize = (initialSize + 7) / 8 * 8;
        }
        return roundedSize;
    }

private static int computeInitialSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) {
        double w = options.outWidth;
        double h = options.outHeight;
        int lowerBound = (maxNumOfPixels == -1) ?

1 : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
        int upperBound = (minSideLength == -1) ?

128 : (int) Math.min(Math.floor(w / minSideLength), Math.floor(h / minSideLength));
        if (upperBound < lowerBound) {
            // return the larger one when there is no overlapping zone.
            return lowerBound;
        }
        if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
            return 1;
        } else if (minSideLength == -1) {
            return lowerBound;
        } else {
            return upperBound;
        }
    }

第三个问题:Matrix来翻转、剪切图片之后再讲。
matrix通过设置postRotate来进行翻转,不管45度还是90度
Matrix matrix = new Matrix();
matrix.postRotate(45);
int x=0;//The x coordinate of the first pixel in source
int y=0;// The y coordinate of the first pixel in source
int width=bitmap.getWidth();//The number of pixels in each row
int height=bitmap.getHeight();// The number of rows
Bitmap finalBitmap = Bitmap.createBitmap(bitmap, x, y, width ,height, matrix, true);
mImg.setImageBitmap(finalBitmap);
剪切主要改变x,y,width和height。来获得对应尺寸和图形的Bitmap
matrix.postRotate的结果例如以下图
matrix.postScale(1,-1);倒立的结果例如以下图

第四个问题:Bitmap.Config几种类型间的差别

A:alpha R:red G:green B:blue

ARGB_8888 32位,8byte每一个像素 透明度为8

ARGB_4444 16位。4byte每一个像素 透明度为4

ALPHA_8     8位,1byte每一个像素 透明度为8 没有颜色

RGB_565    16位,2byte每一个像素 无透明度

第五个问题:canvas绘图

clipRect:要求画制的区域,有left、right、top、bottom4个參数

drawArc:画弧线

drawRect:画长方形

drawCircle:画圆

drawLine:画线

本文来自http://blog.csdn.net/liuxian13183/ 。引用必须注明出处!

写完上一篇后,总认为介绍的知识点不多,仅仅是一种在UI线程解析载入图片,两种在子线程解析,在UI线程载入图片,就这个差别。

关于图片和ImageView,事实上有很多其它能够介绍的,比方在解析图片前获得图片的宽高,用来做图片适配。比方等比例缩小图片,以减小内存占用;比方图片旋转效果等等。

第一个问题和第二个问题:

BitmapFactory.Options options = new BitmapFactory.Options();
/*
* If set to true, the decoder will return null (no bitmap), but the
* out... fields will still be set, allowing the caller to query the
* bitmap without having to allocate the memory for its pixels.
*/
options.inJustDecodeBounds = true;
String url=Environment.getExternalStorageDirectory().getPath() +"/test.jpg";//sdcard/test.jpg
BitmapFactory.decodeFile(url,options);
int outWidth=options.outWidth;//获得图片的宽
int outHeight=options.outHeight;//获得图片的高
int width=100;//放图片组件的宽
int height=100;//放图片组件的高
double shink=outHeight*outWidth/width/height;//缩小的比例
options.inSampleSize=(int) Math.sqrt(shink);//inSampleSize设置。则缩小比例即它的平方。如2则比例为1/4。

Bitmap bitmap=BitmapFactory.decodeFile(url, options);//此值就是我们所须要的值

下面是官方提供的方法:
    public static int computeSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) {
        int initialSize = computeInitialSampleSize(options, minSideLength, maxNumOfPixels);
        int roundedSize;
        if (initialSize <= 8) {
            roundedSize = 1;
            while (roundedSize < initialSize) {
                roundedSize <<= 1;
            }
        } else {
            roundedSize = (initialSize + 7) / 8 * 8;
        }
        return roundedSize;
    }

private static int computeInitialSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) {
        double w = options.outWidth;
        double h = options.outHeight;
        int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
        int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(Math.floor(w / minSideLength), Math.floor(h / minSideLength));
        if (upperBound < lowerBound) {
            // return the larger one when there is no overlapping zone.
            return lowerBound;
        }
        if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
            return 1;
        } else if (minSideLength == -1) {
            return lowerBound;
        } else {
            return upperBound;
        }
    }

第三个问题:Matrix来翻转、剪切图片之后再讲。
matrix通过设置postRotate来进行翻转,不管45度还是90度
Matrix matrix = new Matrix();
matrix.postRotate(45);
int x=0;//The x coordinate of the first pixel in source
int y=0;// The y coordinate of the first pixel in source
int width=bitmap.getWidth();//The number of pixels in each row
int height=bitmap.getHeight();// The number of rows
Bitmap finalBitmap = Bitmap.createBitmap(bitmap, x, y, width ,height, matrix, true);
mImg.setImageBitmap(finalBitmap);
剪切主要改变x,y,width和height。来获得对应尺寸和图形的Bitmap
matrix.postRotate的结果例如以下图
matrix.postScale(1,-1);倒立的结果例如以下图

第四个问题:Bitmap.Config几种类型间的差别

A:alpha R:red G:green B:blue

ARGB_8888 32位,8byte每一个像素 透明度为8

ARGB_4444 16位,4byte每一个像素 透明度为4

ALPHA_8     8位,1byte每一个像素 透明度为8 没有颜色

RGB_565    16位。2byte每一个像素 无透明度

第五个问题:canvas绘图

clipRect:要求画制的区域,有left、right、top、bottom4个參数

drawArc:画弧线

drawRect:画长方形

drawCircle:画圆

drawLine:画线

本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处!

写完上一篇后,总认为介绍的知识点不多,仅仅是一种在UI线程解析载入图片,两种在子线程解析。在UI线程载入图片,就这个差别。

关于图片和ImageView,事实上有很多其它能够介绍的,比方在解析图片前获得图片的宽高,用来做图片适配。比方等比例缩小图片,以减小内存占用;比方图片旋转效果等等。

第一个问题和第二个问题:

BitmapFactory.Options options = new BitmapFactory.Options();
/*
* If set to true, the decoder will return null (no bitmap), but the
* out... fields will still be set, allowing the caller to query the
* bitmap without having to allocate the memory for its pixels.
*/
options.inJustDecodeBounds = true;
String url=Environment.getExternalStorageDirectory().getPath() +"/test.jpg";//sdcard/test.jpg
BitmapFactory.decodeFile(url,options);
int outWidth=options.outWidth;//获得图片的宽
int outHeight=options.outHeight;//获得图片的高
int width=100;//放图片组件的宽
int height=100;//放图片组件的高
double shink=outHeight*outWidth/width/height;//缩小的比例
options.inSampleSize=(int) Math.sqrt(shink);//inSampleSize设置,则缩小比例即它的平方,如2则比例为1/4。

Bitmap bitmap=BitmapFactory.decodeFile(url, options);//此值就是我们所须要的值

下面是官方提供的方法:
    public static int computeSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) {
        int initialSize = computeInitialSampleSize(options, minSideLength, maxNumOfPixels);
        int roundedSize;
        if (initialSize <= 8) {
            roundedSize = 1;
            while (roundedSize < initialSize) {
                roundedSize <<= 1;
            }
        } else {
            roundedSize = (initialSize + 7) / 8 * 8;
        }
        return roundedSize;
    }

private static int computeInitialSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) {
        double w = options.outWidth;
        double h = options.outHeight;
        int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
        int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(Math.floor(w / minSideLength), Math.floor(h / minSideLength));
        if (upperBound < lowerBound) {
            // return the larger one when there is no overlapping zone.
            return lowerBound;
        }
        if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
            return 1;
        } else if (minSideLength == -1) {
            return lowerBound;
        } else {
            return upperBound;
        }
    }

第三个问题:Matrix来翻转、剪切图片之后再讲。
matrix通过设置postRotate来进行翻转,不管45度还是90度
Matrix matrix = new Matrix();
matrix.postRotate(45);
int x=0;//The x coordinate of the first pixel in source
int y=0;// The y coordinate of the first pixel in source
int width=bitmap.getWidth();//The number of pixels in each row
int height=bitmap.getHeight();// The number of rows
Bitmap finalBitmap = Bitmap.createBitmap(bitmap, x, y, width ,height, matrix, true);
mImg.setImageBitmap(finalBitmap);
剪切主要改变x,y,width和height,来获得对应尺寸和图形的Bitmap
matrix.postRotate的结果例如以下图
matrix.postScale(1,-1);倒立的结果例如以下图

第四个问题:Bitmap.Config几种类型间的差别

A:alpha R:red G:green B:blue

ARGB_8888 32位,8byte每一个像素 透明度为8

ARGB_4444 16位,4byte每一个像素 透明度为4

ALPHA_8     8位,1byte每一个像素 透明度为8 没有颜色

RGB_565    16位,2byte每一个像素 无透明度

第五个问题:canvas绘图

clipRect:要求画制的区域。有left、right、top、bottom4个參数

drawArc:画弧线

drawRect:画长方形

drawCircle:画圆

drawLine:画线



Android ImageView设置图片原理(下)的更多相关文章

  1. Android ImageView设置图片原理(上)

    本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 首先关于图片加载到ImageView上,我们来讨论几个问题: 如下: imageView.setIm ...

  2. Android开发模板代码(二)——为ImageView设置图片,退出后能保存ImageView的状态

    接着之前的那个从图库选择图片,设置到ImageView中去,但是,我发现了一个问题,就是再次进入的时候ImageView是恢复到了默认状态,搜索了资料许久之后,终于是发现了解决方法,使用SharePr ...

  3. Java开发桌面程序学习(七)——ImageView设置图片以及jar包读取fxml文件

    ImageView设置图片 JavaFx的ImageView,设置图片不能直接通过属性设置,只能通过代码来设置 ImageView设置图片 首先,我们让fxml对应的那个controller的java ...

  4. Android -- ImageView(控制图片的大小以及旋转的角度)

    1. 

  5. Android Studio设置图片背景及主题设置

    因为Android Studio是基于IDEA的,所以IDEA里面能用的插件Android Studio也能用,这次图片背景是依赖IDEA下的一个插件,名为BackgroundImage的插件,用户可 ...

  6. Android教程:ImageView 设置图片

    Android doc中是这样描述的: public void setImageResource (int resId) 这是其中的一个方法,参数resld是这样: ImageView.setImag ...

  7. ImageView 设置图片

      android doc中是这样描述的: public void setImageResource (int resId) 这是其中的一个方法,参数resld是这样: ImageView.setIm ...

  8. ImageView 设置图片来自:http://blog.csdn.net/lincyang/article/details/6562163

    android doc中是这样描述的: public void setImageResource (int resId) 这是其中的一个方法,参数resld是这样: ImageView.setImag ...

  9. Android ImageView 替换图片

    网上找了半天,找到的都是错的,都不是我想要的效果.我想要的是点击一个图片后,图片被替换. 通过一下方法可以实现:“v”是ImageView对象,“image_name”是替换后的图片资源 ((Imag ...

随机推荐

  1. oracle数据的启动

    打开实例 , 数据库到nomount状态: startup nomount;  参数文件 内存,进程 指定控制文件数据库打开到mount状态: alter datatbase mount;  控制文件 ...

  2. POJ 1821 Fence(单调队列优化DP)

    题解 以前做过很多单调队列优化DP的题. 这个题有一点不同是对于有的状态可以转移,有的状态不能转移. 然后一堆边界和注意点.导致写起来就很难受. 然后状态也比较难定义. dp[i][j]代表前i个人涂 ...

  3. HDU 1667 The Rotation Game (A*迭代搜索)

    题目大意:略 每次选择一个最大深度K,跑IDA* 估价函数H=8-中间8个格里出现次数最多的数的个数x,即把它填满这个数最少需要8-x次操作,如果dep+H>K,就跳出.. 深搜的时候暴力修改, ...

  4. svn文件管理器的使用

    服务器端: 客户端 使用SVN的注意事项 做任何操作之前,先update一下 不要修改其他人的文件 不要在SVN里直接打开.编辑文件 不要在打开.编辑文件的时候,进行操作 SVN客户端的安装,非常简单 ...

  5. 【codeforces 22C】 System Administrator

    [题目链接]:http://codeforces.com/problemset/problem/22/C [题意] 给你n个点; 要求你构造一个含m条边的无向图; 使得任意两点之间都联通; 同时,要求 ...

  6. Qt之QStackedLayout

    简述 QStackedLayout继承自QLayout. QStackedLayout类提供了多页面切换的布局,一次只能看到一个界面. QStackedLayout可用于创建类似于QTabWidget ...

  7. 009实现一个算法来删除单链表中的一个结点,仅仅给出指向那个结点的指针(keep it up)

    呵呵,这个题不能直接删除已知的结点.由于是单链表,不知道前驱,仅仅知道 后继结点,直接删除会使链表断开.只是我们能够删除已知结点的后继结点, 把后继结点的值赋值给已知结点. #include < ...

  8. 生成apk文件遇到的编译问题error: format not a string literal and no format arguments

    编译错误时使用的android-ndk为r9的版本号.报下面错误: "Compile++ thumb : cocosdenshion_static <= SimpleAudioEngi ...

  9. WET Dilutes Performance Bottlenecks

    WET Dilutes Performance Bottlenecks Kirk Pepperdine THE IMPORTANCE OF THE DRY PRINCIPLE (Don't Repea ...

  10. 通俗易懂,什么是.NET?什么是.NET Framework?什么是.NET Core?(转)

    通俗易懂,什么是.NET?什么是.NET Framework?什么是.NET Core?(转) 一.总结 一句话总结:.NET是一个平台,包含多种语言,比如(C#.Visual Basic.C++/C ...