当前位置: 移动技术网 > IT编程>移动开发>Android > Android中Bitmap用法实例分析

Android中Bitmap用法实例分析

2019年07月24日  | 移动技术网IT编程  | 我要评论

浪漫庄园随心助手,收录优美图,索爱手机论坛

本文实例讲述了android中bitmap用法。分享给大家供大家参考,具体如下:

一般在android程序中把图片文件放在res/drawable目录下就可以通过r.drawable.id来使用,但在存储卡中的图片怎样引用呢?下面通过实现这个功能来介绍bitmap的用法。

程序如下:

import java.io.file;
import android.app.activity;
import android.graphics.bitmap;
import android.graphics.bitmapfactory;
import android.os.bundle;
import android.view.view;
import android.view.view.onclicklistener;
import android.widget.button;
import android.widget.imageview;
import android.widget.textview;
public class a10activity extends activity {
 private button b;
 private imageview iv;
 private textview tv;
 private string filename="sdcard/picture/红叶.jpg";
 //private string filename="sdcard/picture/红叶.jpg";这种写法是错误的,路径不是以
//设备开头
  /** called when the activity is first created. */
  @override
  public void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.main);
    b=(button)findviewbyid(r.id.button);
    b.setonclicklistener(new onclicklistener(){
  @override
  public void onclick(view v) {
  // todo auto-generated method stub
  iv=(imageview)findviewbyid(r.id.imageview);
  tv=(textview)findviewbyid(r.id.textview);
  file f=new file(filename);
//先判断图片文件是否存在
  if(f.exists()){
//如果存在,通过bitmap将图片放入imageview中显示出来
/*bitmapfactory(android.graphics.bitmapfactory)是android api提供的对象,该对象
*的decodefile()方法将手机中的图片文件转换成bitmap对象。*/
   bitmap bm=bitmapfactory.decodefile(filename);
   iv.setimagebitmap(bm);
   tv.settext(filename);
  }
  else{
   tv.settext("文件不存在");
  }
  }
  });
  }
}

bitmapfactory也提供了其他方法,例如decoderesource()可以将res/drawable内预先存入的图片文件转换成bitmap对象,decodestream()方法可将inputstream转化成bitmap对象。 

下面这个例子是利用matrix.setrotate()方法来实现imageview的旋转。原理是将imageview放入bitmap中,然后利用bitmap.createbitmap()方法来创建新的bitmap对象,在创建的同时,matrix对象里的setrotate()方法动态旋转新创建的bitmap.然后将旋转好的bitmap对象以新构造的方式创建新的bitmap,并将其放入原来的imageview中。

程序如下所示:

import android.app.activity;
import android.graphics.bitmap;
import android.graphics.bitmapfactory;
import android.graphics.matrix;
import android.graphics.drawable.bitmapdrawable;
import android.os.bundle;
import android.view.view;
import android.view.view.onclicklistener;
import android.widget.button;
import android.widget.imageview;
import android.widget.textview;
public class a11activity extends activity {
 private imageview iv;
 private textview tv;
 private button left,right; 
 private int times;
 private int angle;
  /** called when the activity is first created. */
  @override
  public void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.main);
    times=1;
    angle=1;
    iv=(imageview)findviewbyid(r.id.iv);
    tv=(textview)findviewbyid(r.id.tv);
    left=(button)findviewbyid(r.id.left);
    left.settext("向左转");    
    right=(button)findviewbyid(r.id.right);
    right.settext("向右转");
    final bitmap bmp=bitmapfactory.decoderesource(getresources(), r.drawable.a); //自己引入一张图片a.png
    final int width=bmp.getwidth();
    final int height=bmp.getheight();
    iv.setimagebitmap(bmp);
    left.setonclicklistener(new onclicklistener(){
  @override
  public void onclick(view v) {
  // todo auto-generated method stub
  angle--;
  if(angle<-20){ //设置最多旋转20度
   angle=-20;
  }
  int width01=width*times;
  int height01=height*times;
  float width02=(float)(width01/width);
  float height02=(float)(width02/height);
  matrix m=new matrix();
  m.postscale(width02, height02);
  m.setrotate(5*angle);
  bitmap bmp01=bitmap.createbitmap(bmp, 0, 0, width, height, m, true);
  bitmapdrawable bd=new bitmapdrawable(bmp01);
  iv.setimagedrawable(bd);
  tv.settext(integer.tostring(5*angle));
  }
  });
  right.setonclicklistener(new onclicklistener(){
  @override
  public void onclick(view v) {
  // todo auto-generated method stub
  angle++;
  if(angle>20){
   angle=20;
  }
  int width01=width*times;
  int height01=height*times;
  float width02=(float)(width01/width);
  float height02=(float)(width02/height);
  matrix m=new matrix();
  m.postscale(width02, height02);
  m.setrotate(5*angle);
  bitmap bmp01=bitmap.createbitmap(bmp, 0, 0, width, height, m, true);
  bitmapdrawable bd=new bitmapdrawable(bmp01);
  iv.setimagedrawable(bd);
  tv.settext(integer.tostring(5*angle));
  }
  });
  }
}

res/layout/main.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical" >
  <textview
    android:id="@+id/tv"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello" />
  <button 
    android:id="@+id/left"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
  <button 
    android:id="@+id/right"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
  <imageview 
    android:id="@+id/iv"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
</linearlayout>

更多关于android相关内容感兴趣的读者可查看本站专题:《android开发入门与进阶教程》及《android图形与图像处理技巧总结

希望本文所述对大家android程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网