android UIImageview

来源:互联网 发布:mac ai 毛笔字体 编辑:程序博客网 时间:2024/06/05 18:19
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. 使用方法

VoidsetImageResource(intid):使用一个drawableid设置画板的图片
VoidsetImageDrawable(Drawablea);
VoidsetImageBitmap(Bitmapbm)
创建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
原创粉丝点击