东莞网站建设(曼哈顿信科),权威网站排名,机关网站建设前期准备工作,可以做外链的音乐网站BitMap类#xff1a;
public void recycle()——回收位图占用的内存空间#xff0c;把位图标记为Dead public final boolean isRecycled() ——判断位图内存是否已释放 public final int getWidth()——获取位图的宽度 public final int getHeight()——获取位图的高度 pub… BitMap类
public void recycle()——回收位图占用的内存空间把位图标记为Dead public final boolean isRecycled() ——判断位图内存是否已释放 public final int getWidth()——获取位图的宽度 public final int getHeight()——获取位图的高度 public final boolean isMutable()——图片是否可修改 public int getScaledWidth(Canvas canvas)——获取指定密度转换后的图像的宽度 public int getScaledHeight(Canvas canvas)——获取指定密度转换后的图像的高度 public boolean compress(CompressFormat format, int quality, OutputStream stream)——按指定的图片格式以及画质将图片转换为输出流。 formatBitmap.CompressFormat.PNG或Bitmap.CompressFormat.JPEG quality画质0-100.0表示最低画质压缩100以最高画质压缩。对于PNG等无损格式的图片会忽略此项设置。 常用的静态方法 public static Bitmap createBitmap(Bitmap src) ——以src为原图生成不可变得新图像 public static Bitmap createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter)——以src为原图创建新的图像指定新图像的高宽以及是否可变。 public static Bitmap createBitmap(int width, int height, Config config)——创建指定格式、大小的位图 public static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height)以source为原图创建新的图片指定起始坐标以及新图像的高宽。 public static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter) BitmapFactory工厂类 Option 参数类 public boolean inJustDecodeBounds——如果设置为true不获取图片不分配内存但会返回图片的高度宽度信息。 public int inSampleSize——图片缩放的倍数。如果设为4则宽和高都为原来的1/4则图是原来的1/16。 public int outWidth——获取图片的宽度值 public int outHeight——获取图片的高度值 —————————————————————————————— public int inDensity——用于位图的像素压缩比 public int inTargetDensity——用于目标位图的像素压缩比要生成的位图 public boolean inScaled——设置为true时进行图片压缩从inDensity到inTargetDensity。 读取一个文件路径得到一个位图。如果指定文件为空或者不能解码成文件则返回NULL。 public static Bitmap decodeFile(String pathName, Options opts) public static Bitmap decodeFile(String pathName) 读取一个资源文件得到一个位图。如果位图数据不能被解码或者opts参数只请求大小信息时则返回NuLL。 即当Options.inJustDecodeBoundstrue,只请求图片的大小信息。 public static Bitmap decodeResource(Resources res, int id) public static Bitmap decodeResource(Resources res, int id, Options opts) 从输入流中解码位图 public static Bitmap decodeStream(InputStream is) 从字节数组中解码生成不可变的位图 public static Bitmap decodeByteArray(byte[] data, int offset, int length) BitmapDrawable类继承于Drawable你可以从文件路径、输入流、XML文件以及Bitmap中创建。 常用的构造函数 Resources resgetResources();//获取资源 public BitmapDrawable(Resources res)——创建一个空的drawable。Response用来指定初始时所用的像素密度替代public BitmapDrawable()方法此方法不处理像素密度 public BitmapDrawable(Resources res, Bitmap bitmap)——Create drawable from a bitmap public BitmapDrawable(Resources res, String filepath)——Create a drawable by opening a given file path and decoding the bitmap. public BitmapDrawable(Resources res, java.io.InputStream is)——Create a drawable by decoding a bitmap from the given input stream. 转http://blog.csdn.net/xxxx1243/article/details/5834187 首先介绍我们最常用的Bitmap(位图)。位图是我们开发中最常用的资源毕竟一个漂亮的界面对用户是最有吸引力的。按照对位图的操作分为以下几个功能分别介绍 从资源中获取位图 获取位图的信息 显示位图 位图缩放 位图旋转 1. 从资源中获取位图 可以使用BitmapDrawable或者BitmapFactory来获取资源中的位图。 当然首先需要获取资源 Resources resgetResources(); 使用BitmapDrawable获取位图 1. 使用BitmapDrawable (InputStream is)构造一个BitmapDrawable 2. 使用BitmapDrawable类的getBitmap()获取得到位图 通过Resource的函数InputStream openRawResource(int id)获取得到资源文件的数据流后也可以通过2种方法来获取Bitmap如下 使用BitmapDrawable A Drawable that wraps a bitmap and can be tiled, stretched, or aligned. 使用BitmapDrawable (InputStream is)构造一个BitmapDrawable 使用BitmapDrawable类的getBitmap()获取得到位图 BitmapDrawable也提供了显示位图等操作。 Java代码 InputStream isres.openRawResource(R.drawable.pic180); // 读取资源文件获取输入流 BitmapDrawable bmpDrawnew BitmapDrawable(is); Bitmap bmpbmpDraw.getBitmap(); 或者 BitmapDrawable bmpDraw(BitmapDrawable)res.getDrawable(R.drawable.pic180); Bitmap bmpbmpDraw.getBitmap(); 使用BitmapFactory Creates Bitmap objects from various sources, including files, streams, and byte-arrays. 使用BitmapFactory类decodeStream(InputStream is)解码位图资源获取位图。 使用BitmapFactory类Bitmap bmpBitmapFactory.decodeResource(res, R.drawable.pic180); 方法解码位图资源。 BitmapFactory的所有函数都是static这个辅助类可以通过资源ID、路径、文件、数据流等方式来获取位图。 以上方法在编程的时候可以自由选择在Android SDK中说明可以支持的图片格式如下png (preferred), jpg (acceptable), gif (discouraged)虽然bmp格式没有明确说明但是在Android SDK Support Media Format中是明确说明了。 代码 方法1 2. 获取位图的信息 要获取位图信息比如位图大小、是否包含透明度、颜色格式等获取得到Bitmap就迎刃而解了这些信息在Bitmap的函数中可以轻松获取到。Android SDK中对Bitmap有详细说明阅读起来也比较容易不在此详细说明这里只是辅助说明以下2点 在Bitmap中对RGB颜色格式使用Bitmap.Config定义仅包括ALPHA_8、ARGB_4444、ARGB_8888、RGB_565缺少了一些其他的比如说RGB_555在开发中可能需要注意这个小问题 Bitmap还提供了compress()接口来压缩图片不过AndroidSAK只支持PNG、JPG格式的压缩其他格式的需要Android开发人员自己补充了。 3. 显示位图 显示位图可以使用核心类Canvas通过Canvas类的drawBirmap()显示位图或者借助于BitmapDrawable来将Bitmap绘制到Canvas。当然也可以通过BitmapDrawable将位图显示到View中。 转换为BitmapDrawable对象显示位图 // 获取位图 Bitmap bmpBitmapFactory.decodeResource(res, R.drawable.pic180); // 转换为BitmapDrawable对象 BitmapDrawable bmpDrawnew BitmapDrawable(bmp); // 显示位图 ImageView iv2 (ImageView)findViewById(R.id.ImageView02); iv2.setImageDrawable(bmpDraw); 使用Canvas类显示位图 这儿采用一个继承自View的子类Panel在子类的OnDraw中显示 Java代码 public class MainActivity extends Activity { Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new Panel(this)); } class Panel extends View{ public Panel(Context context) { super(context); } public void onDraw(Canvas canvas){ Bitmap bmp BitmapFactory.decodeResource(getResources(), R.drawable.pic180); canvas.drawColor(Color.BLACK); canvas.drawBitmap(bmp, 10, 10, null); } } } 4. 位图缩放 1将一个位图按照需求重画一遍画后的位图就是我们需要的了与位图的显示几乎一样drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint)。 2在原有位图的基础上缩放原位图创建一个新的位图CreateBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter) 3借助Canvas的scale(float sx, float sy) Preconcat the current matrix with the specified scale.不过要注意此时整个画布都缩放了。 4借助Matrix Java代码 Bitmap bmp BitmapFactory.decodeResource(getResources(), R.drawable.pic180); Matrix matrixnew Matrix(); matrix.postScale(0.2f, 0.2f); Bitmap dstbmpBitmap.createBitmap(bmp,0,0,bmp.getWidth(), bmp.getHeight(),matrix,true); canvas.drawColor(Color.BLACK); canvas.drawBitmap(dstbmp, 10, 10, null); 5. 位图旋转 同样位图的旋转也可以借助Matrix或者Canvas来实现。Matrix在线性代数中都学习过Android SDK提供了Matrix类可以通过各种接口来设置矩阵。结合上面的例子程序将位图缩放例子程序在显示位图的时候前增加位图旋转功能修改代码如下 Matrix matrix new Matrix(); //matrix.postScale(0.5f, 0.5f); matrix.setRotate(90,120,130); canvas.drawBitmap(mbmpTest, matrix, mPaint); 旋转后的位图显示如下 除了这种方法之外我们也可以在使用Bitmap提供的函数如下 public static Bitmap createBitmap (Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)在原有位图旋转的基础上创建新位图。 总结说明 对位图的操作结合Android SDK中的类详细的介绍完了。最后还需要强调的是这篇文章只是对Android SDK中代码阅读分析它代替不了你阅读Android SDK深入的学习还是要仔细的阅读Android SDK。 转载自http://blog.sina.com.cn/s/blog_5da93c8f0100w3xz.html