Bitmap createBitmap参数(三)

来源:互联网 发布:java面向对象思维导图 编辑:程序博客网 时间:2024/05/01 01:42

API:

Bitmap createBitmap (Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)

Returns an immutable bitmap from subset of the source bitmap, transformed by the optional matrix. The new bitmap may be the same object as source, or a copy may have been made. It is initialized with the same density as the original bitmap. If the source bitmap is immutable and the requested subset is the same as the source bitmap itself, then the source bitmap is returned and no new bitmap is created.

返回一个不可变的源位图的位图的子集,改变了可选的矩阵。新的位图可能与源相同的对象,或可能是一个副本。它初始化与原始位图的密度。如果源位图是不可变的,请求的子集是一样的源位图本身,然后返回源位图,没有新的位图创建。

Parameters

sourceBitmap: The bitmap we are subsetting  产生子位图的源位图;
x int: The x coordinate of the first pixel in source 子位图第一个像素在源位图的X坐标
y int: The y coordinate of the first pixel in source 子位图第一个像素在源位图的y坐标
width int: The number of pixels in each row 子位图每一行的像素个数
height int: The number of rows 子位图的行数
m Matrix: Optional matrix to be applied to the pixels 对像素值进行变换的可选矩阵
filter boolean: true if the source should be filtered. Only applies if the matrix contains more than just translation. 如果为true,源图要被过滤。该参数仅在matrix包含了超过一个翻转才有效

Returns 
Bitmap A bitmap that represents the specified subset of source  一个描述了源图指定子集的位图。
Throws
IllegalArgumentException if the x, y, width, height values are outside of the dimensions of the source bitmap, or width is <= 0, or height is <= 0 

如果xywidthheight的值超出了源图的维度,该异常会被抛出。

/** * 返回一个不可变的源位图的位图的子集,改变了可选的矩阵。新的位图可能与源相同的对象,或可能是一个副本。它初始化与原始位图的密度。如果源位图是不可变的 * ,请求的子集是一样的源位图本身,然后返回源位图,没有新的位图创建。<br> * 方 法 名:createBitmap <br> * 创 建 人: <br> * 创建时间:2016-6-7 上午11:14:24 <br> * 修 改 人: <br> * 修改日期: <br> * @param source 产生子位图的源位图 * @param x 子位图第一个像素在源位图的X坐标 * @param y 子位图第一个像素在源位图的y坐标 * @param width 子位图每一行的像素个数 * @param height 子位图的行数 * @param m 对像素值进行变换的可选矩阵 * @param filter 如果为true,源图要被过滤。该参数仅在matrix包含了超过一个翻转才有效 * @return 一个描述了源图指定子集的位图。 Bitmap */public static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter) {Bitmap bitmap;try {bitmap = Bitmap.createBitmap(source, x, y, width, height, m, filter);} catch (OutOfMemoryError localOutOfMemoryError) {gc();bitmap = Bitmap.createBitmap(source, x, y, width, height, m, filter);}return bitmap;}

例子:

package com.example.cc;import android.os.Bundle;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Matrix;import android.view.Menu;import android.widget.ImageView;public class MainActivity extends Activity {private ImageView image_1;private ImageView image_2;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);image_1 = (ImageView) findViewById(R.id.image_1);image_2 = (ImageView) findViewById(R.id.image_2);image_1.setImageResource(R.drawable.ascii);Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.ascii);Matrix matrix = new Matrix();matrix.postScale(1, 1);// 缩放比例image_2.setImageBitmap(createBitmap(bitmap, 20, 20, bitmap.getWidth() - 20, bitmap.getHeight() - 20, matrix,true));}/** * 返回一个不可变的源位图的位图的子集,改变了可选的矩阵。新的位图可能与源相同的对象,或可能是一个副本。它初始化与原始位图的密度。如果源位图是不可变的 * ,请求的子集是一样的源位图本身,然后返回源位图,没有新的位图创建。<br> * 方 法 名:createBitmap <br> * 创 建 人: <br> * 创建时间:2016-6-7 上午11:14:24 <br> * 修 改 人: <br> * 修改日期: <br> * @param source 产生子位图的源位图 * @param x 子位图第一个像素在源位图的X坐标 * @param y 子位图第一个像素在源位图的y坐标 * @param width 子位图每一行的像素个数 * @param height 子位图的行数 * @param m 对像素值进行变换的可选矩阵 * @param filter 如果为true,源图要被过滤。该参数仅在matrix包含了超过一个翻转才有效 * @return 一个描述了源图指定子集的位图。 Bitmap */public static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter) {Bitmap bitmap;try {bitmap = Bitmap.createBitmap(source, x, y, width, height, m, filter);} catch (OutOfMemoryError localOutOfMemoryError) {gc();bitmap = Bitmap.createBitmap(source, x, y, width, height, m, filter);}return bitmap;}/** * 回收<br> * 方 法 名:gc <br> * 创 建 人: <br> * 创建时间:2016-6-7 上午9:32:37 <br> * 修 改 人: <br> * 修改日期: <br> * void */private static void gc() {System.gc();// 表示java虚拟机会做一些努力运行已被丢弃对象(即没有被任何对象引用的对象)的 finalize// 方法,前提是这些被丢弃对象的finalize方法还没有被调用过System.runFinalization();}}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <LinearLayout        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1" >        <ImageView            android:id="@+id/image_1"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:scaleType="matrix" />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1" >        <ImageView            android:id="@+id/image_2"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:scaleType="matrix" />    </LinearLayout></LinearLayout>




1 1
原创粉丝点击