Android中Bitmap用法实例分析
本文实例讲述了Android中Bitmap用法。分享给大家供大家参考,具体如下:
一般在android程序中把图片文件放在res/drawable目录下就可以通过R.drawable.id来使用,但在存储卡中的图片怎样引用呢?下面通过实现这个功能来介绍Bitmap的用法。
程序如下:
importjava.io.File;
importandroid.app.Activity;
importandroid.graphics.Bitmap;
importandroid.graphics.BitmapFactory;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.Button;
importandroid.widget.ImageView;
importandroid.widget.TextView;
publicclassA10ActivityextendsActivity{
privateButtonb;
privateImageViewiv;
privateTextViewtv;
privateStringfileName="sdcard/picture/红叶.jpg";
//privateStringfileName="sdcard/picture/红叶.jpg";这种写法是错误的,路径不是以
//设备开头
/**Calledwhentheactivityisfirstcreated.*/
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b=(Button)findViewById(R.id.button);
b.setOnClickListener(newOnClickListener(){
@Override
publicvoidonClick(Viewv){
//TODOAuto-generatedmethodstub
iv=(ImageView)findViewById(R.id.imageview);
tv=(TextView)findViewById(R.id.textview);
Filef=newFile(fileName);
//先判断图片文件是否存在
if(f.exists()){
//如果存在,通过Bitmap将图片放入ImageView中显示出来
/*BitmapFactory(Android.graphics.BitmapFactory)是AndroidAPI提供的对象,该对象
*的decodeFile()方法将手机中的图片文件转换成Bitmap对象。*/
Bitmapbm=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中。
程序如下所示:
importandroid.app.Activity;
importandroid.graphics.Bitmap;
importandroid.graphics.BitmapFactory;
importandroid.graphics.Matrix;
importandroid.graphics.drawable.BitmapDrawable;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.Button;
importandroid.widget.ImageView;
importandroid.widget.TextView;
publicclassA11ActivityextendsActivity{
privateImageViewiv;
privateTextViewtv;
privateButtonleft,right;
privateinttimes;
privateintangle;
/**Calledwhentheactivityisfirstcreated.*/
@Override
publicvoidonCreate(BundlesavedInstanceState){
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("向右转");
finalBitmapbmp=BitmapFactory.decodeResource(getResources(),R.drawable.a);//自己引入一张图片a.png
finalintwidth=bmp.getWidth();
finalintheight=bmp.getHeight();
iv.setImageBitmap(bmp);
left.setOnClickListener(newOnClickListener(){
@Override
publicvoidonClick(Viewv){
//TODOAuto-generatedmethodstub
angle--;
if(angle<-20){//设置最多旋转20度
angle=-20;
}
intwidth01=width*times;
intheight01=height*times;
floatwidth02=(float)(width01/width);
floatheight02=(float)(width02/height);
Matrixm=newMatrix();
m.postScale(width02,height02);
m.setRotate(5*angle);
Bitmapbmp01=Bitmap.createBitmap(bmp,0,0,width,height,m,true);
BitmapDrawablebd=newBitmapDrawable(bmp01);
iv.setImageDrawable(bd);
tv.setText(Integer.toString(5*angle));
}
});
right.setOnClickListener(newOnClickListener(){
@Override
publicvoidonClick(Viewv){
//TODOAuto-generatedmethodstub
angle++;
if(angle>20){
angle=20;
}
intwidth01=width*times;
intheight01=height*times;
floatwidth02=(float)(width01/width);
floatheight02=(float)(width02/height);
Matrixm=newMatrix();
m.postScale(width02,height02);
m.setRotate(5*angle);
Bitmapbmp01=Bitmap.createBitmap(bmp,0,0,width,height,m,true);
BitmapDrawablebd=newBitmapDrawable(bmp01);
iv.setImageDrawable(bd);
tv.setText(Integer.toString(5*angle));
}
});
}
}
res/layout/main.xml如下:
<?xmlversion="1.0"encoding="utf-8"?> <LinearLayoutxmlns: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程序设计有所帮助。