玩转Android---2D图形及动画---图片处理

原址:http://hualang.iteye.com/category/143855

在Android中很多地方都使用到图片,比如各种图标,图片按钮等。在Android中操作图片是通过使用Drawable类来完成的。Drawable类有很多个子类,如BitmapDrawable用来操作位图;ColorDrawable用来操作颜色;ShapeDrawable用来操作各种形状。

有三种凡事来实例化Drawable对象:一是用来保存在工程中的图片资源;而是在XML中定义Drawable属性;三是通过构造方法实例化,这种方法在实际开发中一般不会用到。

 

例子1:在代码中通过setImageResource()方法来设置

BitmapTest01.java

Java代码   收藏代码
  1. package org.test.bitmaptest01;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.widget.ImageView;  
  6.   
  7. public class BitmapTest01 extends Activity {  
  8.     //声明图片视图ImageView  
  9.     private ImageView myImageView;  
  10.     @Override  
  11.     public void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         setContentView(R.layout.main);  
  14.         myImageView = (ImageView)findViewById(R.id.ImageView01);  
  15.         //为ImageView设置图片资源  
  16.         myImageView.setImageResource(R.drawable.beau);  
  17.     }  
  18. }  

 main.xml

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <TextView    
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="wrap_content"   
  10.     android:text="测试"  
  11.     />  
  12.     <ImageView  
  13.         android:id="@+id/ImageView01"  
  14.         android:layout_width="wrap_content"  
  15.         android:layout_height="wrap_content"  
  16.     />  
  17. </LinearLayout>  

 运行效果如下:


 

或者在XML中配置。

应用程序的图标子在AndroidManifest.xml中配置

Xml代码   收藏代码
  1. <application android:icon="@drawable/icon" android:label="@string/app_name">  

 图片资源可以在布局文件中的ImageView属性中设置:

Java代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <TextView    
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="wrap_content"   
  10.     android:text="测试"  
  11.     />  
  12.     <ImageView  
  13.         android:id="@+id/ImageView01"  
  14.         android:layout_width="wrap_content"  
  15.         android:layout_height="wrap_content"  
  16.         <strong>android:src="@drawable/beau"</strong>  
  17.     />  
  18. </LinearLayout>  

 
如果我们的图片文件保存在SDCard中又该怎么办呢?在这种情况下,我们可以使用Bitmap和BitmapFactory两个类来读取文件。下面是如何从SDCard中读取图片文件并将其设置为壁纸的例子。首先需要在SDCard中添加一个名为wallpaper.png的图片资源。

我们在onCreate()方法中通过BitmapFactory的decodeFile()方法传递文件路径,获取Bitmap对象,然后调用

setWallpaper就可以了

代码如下:

Java代码   收藏代码
  1. package com.loulijun.bitmapwallpaper;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import android.app.Activity;  
  6. import android.graphics.Bitmap;  
  7. import android.graphics.BitmapFactory;  
  8. import android.os.Bundle;  
  9.   
  10. public class MainActivity extends Activity {  
  11.     /** Called when the activity is first created. */  
  12.     @Override  
  13.     public void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.main);  
  16.         String path = "/sdcard/wallpaper.png";  
  17.         //通过BitmapFactory获取Bitmap实例  
  18.         Bitmap bm = BitmapFactory.decodeFile(path);  
  19.         try  
  20.         {  
  21.             //设置桌面  
  22.             setWallpaper(bm);  
  23.         }catch(IOException e)  
  24.         {  
  25.             e.printStackTrace();  
  26.         }  
  27.     }  
  28. }  
 

效果不贴了,就是更换了桌面背景图片


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值