Android中Bitmap用法实例分析

 更新时间:2016年02月01日 09:13:50   作者:马到成功  
这篇文章主要介绍了Android中Bitmap用法,结合实例形式分析了Android操作图片的载入、属性设置、旋转等相关技巧,需要的朋友可以参考下

本文实例讲述了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程序设计有所帮助。

相关文章

  • 解析Kotlin JSON格式

    解析Kotlin JSON格式

    这篇文章主要介绍了Kotlin JSON格式解析,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-03-03
  • Android自动播放Banner图片轮播效果

    Android自动播放Banner图片轮播效果

    这篇文章主要介绍了Android自动播放Banner图片轮播效果的相关资料,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-07-07
  • Android学习之AppWidget高级效果

    Android学习之AppWidget高级效果

    这篇文章主要为大家详细介绍了Android学习之AppWidget高级效果的相关资料,感兴趣的小伙伴们可以参考一下
    2016-08-08
  • Android Studio真机无线连接USB设备调试运行详解流程

    Android Studio真机无线连接USB设备调试运行详解流程

    你在Android Studio写app时是否也有想过如果可以不用数据线连接手机调试运行就好了?如果需要取出数据线插接的话我肯定是嫌麻烦的,但是模拟器有时候需要测试一些需要硬件支持的功能时又不管用,所以最好的测试还是在真机上,本篇教你扔掉数据线来无线调试
    2021-11-11
  • Android App实现闪屏页广告图的全屏显示实例

    Android App实现闪屏页广告图的全屏显示实例

    这篇文章主要为大家介绍了Android App实现闪屏页广告图的全屏显示实例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-08-08
  • Android NDK入门初识(组件结构开发流程)

    Android NDK入门初识(组件结构开发流程)

    这篇文章主要为大家介绍了Android NDK入门之初识组件结构开发流程详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-08-08
  • Android中的Intent对象完全解析

    Android中的Intent对象完全解析

    这篇文章主要介绍了Android中的Intent对象,深入讲解了intent对象传递消息的各种用法,需要的朋友可以参考下
    2016-04-04
  • MPAndroidChart自定义图表Chart的Attribute及Render绘制逻辑

    MPAndroidChart自定义图表Chart的Attribute及Render绘制逻辑

    这篇文章主要为大家介绍了MPAndroidChart自定义图表Chart的Attribute及Render绘制逻辑,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-12-12
  • Android更多条目收缩展开控件ExpandView的示例代码

    Android更多条目收缩展开控件ExpandView的示例代码

    本篇文章主要介绍了Android更多条目收缩展开控件ExpandView的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-01-01
  • Android中WebView实现点击超链接启动QQ的方法

    Android中WebView实现点击超链接启动QQ的方法

    这篇文章主要给大家介绍了在Android中WebView如何实现点击超链接启动QQ的方法,文中给出了详细的示例代码,相信对大家的学习或者工作具有一定的参考价值,需要的朋友们下面来一起看看吧。
    2017-04-04

最新评论