Android Bitmap(点阵图像、绘制图像)

Bitmap 称为点阵图像或绘制图像,是由称作像素(图片元素)的单个点组成的,这些点通过不同的排列和染色以构成图样。

Bitmap 是 Android 系统中图像处理最重要的类之一,用它可以获取图像文件信息,对图像进行剪切、旋转、缩放等操作,并可以将图像保存成特定格式的文件。

Bitmap 位于 android.graphics 包中,不提供对外的构造方法,只能通过 BitmapFactory 类进行实例化。利用 BitmapFactory 的 decodeFile 方法可以从特定文件中获取 Bitmap 对象,也可以使用 decodeResource() 从特定的图片资源中获取 Bitmap 对象。

实例 BitmapDemo 从资源文件中创建 Bitmap 对象,并对其进行一些操作,运行效果如图 1 所示。
 

Bitmap对象的效果
图 1  Bitmap对象的效果


其对应布局文件 Main.xml 的内容如下:

 
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical">
  6.  
  7. <SeekBar
  8. android:id="@+id/seekBarId"
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content" />
  11.  
  12. <ImageView
  13. android:id="@+id/imageview"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:src="@drawable/img1" />
  17. </LinearLayout>

BitmapActivity.Java 的代码如下:

 
  1. package introduction.android.bitmapdemo;
  2.  
  3. import android.annotation.SuppressLint;
  4. import android.app.Activity;
  5. import android.graphics.Bitmap;
  6. import android.graphics.BitmapFactory;
  7. import android.graphics.Matrix;
  8. import android.os.Bundle;
  9. import android.widget.ImageView;
  10. import android.widget.SeekBar;
  11.  
  12. public class BitmapActivity extends Activity {
  13.  
  14. ImageView myImageView;
  15. Bitmap myBmp, newBmp;
  16. int bmpWidth, bmpHeight;
  17. SeekBar seekbarRotate;
  18. float rotAngle;
  19.  
  20. @Override
  21. public void onCreate(Bundle savedInstanceState) {
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.activity_bitmap);
  24. myImageView = (ImageView) findViewById(R.id.imageview);
  25. //由Resource载入图片
  26. myBmp = BitmapFactory.decodeResource(getResources(), R.drawable.img1);
  27. bmpWidth = myBmp.getWidth();
  28. bmpHeight = myBmp.getHeight();
  29. //实例化matrix
  30. Matrix matrix = new Matrix();
  31. //设定Matrix属性 x、y缩放比例为1.5
  32. matrix.postScale(1.5F, 1.5F);
  33. //顺时针旋转45度
  34. matrix.postRotate(45.0F);
  35. newBmp = Bitmap.createBitmap(myBmp, 0, 0, bmpWidth, bmpHeight, matrix, true);
  36. seekbarRotate = (SeekBar) findViewById(R.id.seekBarId);
  37. seekbarRotate.setOnSeekBarChangeListener(onRotate);
  38. }
  39.  
  40. private SeekBar.OnSeekBarChangeListener onRotate = new SeekBar.OnSeekBarChangeListener() {
  41. public void onStopTrackingTouch(SeekBar seekBar) {
  42. // TODO Auto-generated method stub
  43. }
  44.  
  45. public void onStartTrackingTouch(SeekBar seekBar) {
  46. // TODO Auto-generated method stub
  47. }
  48.  
  49. public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
  50. // TODO Auto-generated method stub
  51. Matrix m = new Matrix();
  52. m.postRotate((float) progress * 3.6F);
  53. newBmp = Bitmap.createBitmap(myBmp, 0, 0, bmpWidth, bmpHeight, m, true);
  54. myImageView.setImageBitmap(newBmp);
  55. }
  56. };
  57. }

本实例实现了拖动进度条图片旋转的效果。

使用 BitmapFactory 从资源中载入图片,并获取图片的宽和高,之后使用 Matrix 类对图片进行缩放和旋转操作。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值