android UIImageview

Android的图形处理
对于2D图形的处理,它没有使用java的图形处理类(java.awt,包中,如Graphics,Canvas等类),而是自己弄了一套。
Andorid.graphics
Android.graphics.drawable.shapes
Android.view.animation
Andorid的图形处理主要包括两大类:
静态图形处理(已经拥有了这些图片)。一般将这些图形文件作为资源文件添加到项目中即可。然后通过各种drwaable类来处理
动态图形处理:需要代码不停的绘制。涉及到画布Canvas,画笔Paint等类。

1. 使用简单图片
使用 Drawable 对象
产生图片资源 : 讲图片文件拷贝到 res/ drawable 下。
访问图片资源 :

xml@drawable/文件名

在代码中R.drawable.文件名

Drawable就是一个可画的对象,其可能是一个可画的对象,也可以是一个图形(shapedrawable),其中访问这些图形类似于操作“内存画布

2. 使用方法

Void setImageResource ( int id): 使用一个 drawable id 设置画板的图片
Void setImageDrawable ( Drawable a);
Void setImageBitmap (Bitmap bm )
创建 Bitmap 对象方法 :
BitmapFactory ( android.graphices.BitmapFactory ): 一个工具类,提供了多种方法,这些方法可用于从不同的数据源来解析,创建 Bitmap 对象。主要方法如下 :

2. BitmapBitmapFactory

 

                Static Bitmap

                decodeByteArray(byte[] data, int offset, int length):从指定字节数组的offset位置开始,讲长度为length的数据解析,创建Bitmap对象

              Static Bitmap

              decodeFile(string pathname):指定文件中解析、创建Bitmap对象

               Static Bitmap

                decodeFileDescriptor(FileDescriptor fd):用于从FileDescriptor对应的文件中解析,创建Bitmap对象

               Static Bitmap

                  decodeResource(Resource res, int id):用于根据给定的资源ID从指定资源中解析、创建Bitmap对象

                Static Bitmap

                   decodeStream(InputStream is):从指定输出流中解析、创建Bitmap对象


3. 例子: 

  1. public class BitmapTest extends Activity {  
  2.     /** Called when the activity is first created. */  
  3.     String[] images = null;  
  4.     AssetManager assets = null;  
  5.     int currentImg = 0;  
  6.     ImageView image;  
  7.     @Override  
  8.     public void onCreate(Bundle savedInstanceState) {  
  9.         super.onCreate(savedInstanceState);  
  10.         setContentView(R.layout.main);  
  11.         image = (ImageView)findViewById(R.id.image);  
  12.         try  
  13.         {  
  14.             assets = getAssets();  
  15.             //获取/assets/目录下所有文件  
  16.             images = assets.list("");  
  17.         }  
  18.         catch(IOException e)  
  19.         {  
  20.             e.printStackTrace();  
  21.         }  
  22.         //获取bn按钮  
  23.         final Button next = (Button)findViewById(R.id.next);  
  24.         //为bn按钮绑定事件监听器,该监听器将会查看下一张图片  
  25.         next.setOnClickListener(new OnClickListener(){  
  26.             public void onClick(View sources)  
  27.             {  
  28.                 //如果发生数组越界  
  29.                 if(currentImg >= images.length)  
  30.                 {  
  31.                     currentImg = 0;  
  32.                 }  
  33.                 //找到下一个图片文件  
  34.                 while(!images[currentImg].endsWith(".png")  
  35.                         && !images[currentImg].endsWith(".jpg")  
  36.                         && !images[currentImg].endsWith(".gif"))  
  37.                 {  
  38.                     currentImg++;  
  39.                     //如果已发生数组越界  
  40.                     if(currentImg >= images.length)  
  41.                     {  
  42.                         currentImg = 0;  
  43.                     }  
  44.                 }  
  45.                 InputStream assetFile = null;  
  46.                 try  
  47.                 {  
  48.                     //打开指定的资源对应的输入流  
  49.                     assetFile = assets.open(images[currentImg++]);  
  50.                 }  
  51.                 catch(IOException e)  
  52.                 {  
  53.                     e.printStackTrace();  
  54.                 }  
  55.                 BitmapDrawable bitmapDrawable = (BitmapDrawable)image.getDrawable();  
  56.                 //如果图片还未回收,先强制回收该图片   
  57.                 if(bitmapDrawable!=null &&  
  58.                         !bitmapDrawable.getBitmap().isRecycled())  
  59.                 {  
  60.                     bitmapDrawable.getBitmap().recycle();  
  61.                 }  
  62.                 //改变ImageView显示的图片  
  63.                 image.setImageBitmap(BitmapFactory.decodeStream(assetFile));  
  64.             }  
  65.         });  
  66.     }  
  67. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值