Android中Bitmap用法实例分析
作者:马到成功 发布时间:2023-03-21 11:06:31
标签:Android,Bitmap
本文实例讲述了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程序设计有所帮助。


猜你喜欢
- 还记得读大学时初识计算机编程时的C语言,Main(){},那时还不明白入口函数是什么意思,只知道照抄书本上的示例,一行一行地跑printf看
- 前言在日常开发中,圆形的图片效果还是很常见的。可以通过给Paint设置Xfermode来实现,这里简单记录如下。实现实现圆形效果的核心是Po
- 一、概述现实生活中,我们常会看到这样的一种集合:IP地址与主机名,身份证号与个人,系统用户名与系统用户对象等,这种一一对应的关系,就叫做映射
- 本文实例为大家分享了Android实现界面跳转的具体代码,供大家参考,具体内容如下布局<?xml version="1.0&
- 前言本文主要给大家介绍了关于如何实现Builder模式,大家在构建大对象时,对象的属性比较多,我们可以采用一个构造器或者使用空的构造器构造,
- 一、在spring的应用中我们存在两种过滤的用法,一种是 * 、另外一种当然是过滤器。我们这里介绍过滤器在springboot的用法,在sp
- 在spring中有很多以XXXAware命名的接口,很多人也不清楚这些接口都是做什么用的,这篇文章将描述常用的一些接口。一,Applicat
- 前情提要本文中提供了九种方式获取resources目录下文件的方式。其中打印文件的方法如下: /**
- ScriptControl接口属性名称类型备注AllowUIBOOL检测是否允许运行用户的接口元素。如果为False,则诸如消息框之类的界面
- 前言这段时间比较闲,就看起了jdk源码。一般的一个高级开发工程师, 能阅读一些源码对自己的提升还是蛮大的。本文总结了一些JDK源码中的“小技
- public static void SortDicWithLinq(){ &nb
- Java数组初始化需要指定数组容量,但是在许多情况下需要动态扩充容量。有两种方法可以实现:1.采用ArrayList类数组,它可以在需要时自
- 本文实例讲述了Android使用WebView播放flash及判断是否安装flash插件的方法。分享给大家供大家参考。具体实现方法如下:一、
- 1、引言在SpringMVC的使用中,后端与前端的交互一般是使用Json格式进行数据传输,SpringMVC的@Response
- 一、bean实例化——构造方法(常用)bean本质上就是对象,创建bean使用构造方法完成BookD
- 延迟队列延迟队列存储的对象肯定是对应的延时消息,所谓”延时消息”是指当消息被发送以后,并不想让消费者立即拿到消息,而是等待指定时间后,消费者
- 动态SQL简介动态 SQL 是 MyBatis 的强大特性之一。如果你使用过 JDBC 或其它类似的框架,你应该能理解根据不同条件拼接 SQ
- Filter简介Filter也称之为过滤器,它是Servlet技术中最实用的技术,WEB开发人员通过Filter技术,对web服务器管理的所
- 1.以G71列车为例,首先对车次站台进行占位编码(从1开始到最后一站递加)对以上占位简单描述以下:G71总共18个站点那么我们的单个座位的座
- 在看别人的关于CopyOnWriteArrayList 这个类的时候,看到有人提出了关于:数组拷贝的方法Arrays.copyOf() 的问