源码教程 2025年06月7日
0 收藏 0 点赞 636 浏览 2398 个字
摘要 :

Bitmap 称为点阵图像或绘制图像,是由称作像素(图片元素)的单个点组成的,这些点通过不同的排列和染色以构成图样。Bitmap 是 Android 系统中图像处理最重要……

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

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

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

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

Android点阵图像、绘制图像Bitmap控件使用Android点阵图像、绘制图像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. <SeekBar

  7. android:id="@+id/seekBarId"

  8. android:layout_width="fill_parent"

  9. android:layout_height="wrap_content" />

  10. <ImageView

  11. android:id="@+id/imageview"

  12. android:layout_width="wrap_content"

  13. android:layout_height="wrap_content"

  14. android:src="@drawable/img1" />

  15. </LinearLayout>

BitmapActivity.Java 的代码如下:

  1. package introduction.android.bitmapdemo;

  2. import android.annotation.SuppressLint;

  3. import android.app.Activity;

  4. import android.graphics.Bitmap;

  5. import android.graphics.BitmapFactory;

  6. import android.graphics.Matrix;

  7. import android.os.Bundle;

  8. import android.widget.ImageView;

  9. import android.widget.SeekBar;

  10. public class BitmapActivity extends Activity {

  11. ImageView myImageView;

  12. Bitmap myBmp, newBmp;

  13. int bmpWidth, bmpHeight;

  14. SeekBar seekbarRotate;

  15. float rotAngle;

  16. @Override

  17. public void onCreate(Bundle savedInstanceState) {

  18. super.onCreate(savedInstanceState);

  19. setContentView(R.layout.activity_bitmap);

  20. myImageView = (ImageView) findViewById(R.id.imageview);

  21. //由Resource载入图片

  22. myBmp = BitmapFactory.decodeResource(getResources(), R.drawable.img1);

  23. bmpWidth = myBmp.getWidth();

  24. bmpHeight = myBmp.getHeight();

  25. //实例化matrix

  26. Matrix matrix = new Matrix();

  27. //设定Matrix属性 x、y缩放比例为1.5

  28. matrix.postScale(1.5F, 1.5F);

  29. //顺时针旋转45度

  30. matrix.postRotate(45.0F);

  31. newBmp = Bitmap.createBitmap(myBmp, 0, 0, bmpWidth, bmpHeight, matrix, true);

  32. seekbarRotate = (SeekBar) findViewById(R.id.seekBarId);

  33. seekbarRotate.setOnSeekBarChangeListener(onRotate);

  34. }

  35. private SeekBar.OnSeekBarChangeListener onRotate = new SeekBar.OnSeekBarChangeListener() {

  36. public void onStopTrackingTouch(SeekBar seekBar) {

  37. // TODO Auto-generated method stub

  38. }

  39. public void onStartTrackingTouch(SeekBar seekBar) {

  40. // TODO Auto-generated method stub

  41. }

  42. public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

  43. // TODO Auto-generated method stub

  44. Matrix m = new Matrix();

  45. m.postRotate((float) progress * 3.6F);

  46. newBmp = Bitmap.createBitmap(myBmp, 0, 0, bmpWidth, bmpHeight, m, true);

  47. myImageView.setImageBitmap(newBmp);

  48. }

  49. };

  50. }

微信扫一扫

支付宝扫一扫

版权: 转载请注明出处:https://www.zuozi.net/997.html

管理员

相关推荐
2025-07-05

对于一些使用WordPress进行外贸建站的商家来说,大部分人会通过在WordPress中添加JS代码和CSS样式表…

708
2025-07-05

当商家遇到WordPress独立站改版或者域名到期等问题时,不免会涉及到WordPress域名的更改,那么这一…

726
2025-07-05

用户在使用WooCommerce进行跨境电商独立站搭建工作时,有可能会借助WooCommerce短代码实现更加醒目…

314
2025-07-05

随着外贸建站行业的不断深入发展,WordPress的多语言功能也显得越来越重要,一个具有多语言的独立站…

1,050
2025-07-05

WooCommerce作为WordPress外贸建站生态的一部分,WooCommerce运费设置是商家在建站过程中不可忽视的…

843
2025-07-05

在外贸建站的过程中,有些商家会选择使用WordPress幻灯片为网站页面增加一定的动感和可观性,进而提…

729
发表评论
暂无评论

还没有评论呢,快来抢沙发~

助力内容变现

将您的收入提升到一个新的水平

点击联系客服

在线时间:08:00-23:00

客服QQ

122325244

客服电话

400-888-8888

客服邮箱

122325244@qq.com

扫描二维码

关注微信客服号