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

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

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

 

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

BitmapTest01.java

package org.test.bitmaptest01;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;

public class BitmapTest01 extends Activity {
    //声明图片视图ImageView
	private ImageView myImageView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        myImageView = (ImageView)findViewById(R.id.ImageView01);
        //为ImageView设置图片资源
        myImageView.setImageResource(R.drawable.beau);
    }
}

 main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="测试"
    />
    <ImageView
    	android:id="@+id/ImageView01"
    	android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
    />
</LinearLayout>

 运行效果如下:


 

或者在XML中配置。

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

<application android:icon="@drawable/icon" android:label="@string/app_name">

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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="测试"
    />
    <ImageView
    	android:id="@+id/ImageView01"
    	android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
    	android:src="@drawable/beau"
    />
</LinearLayout>

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

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

setWallpaper就可以了

代码如下:

package com.loulijun.bitmapwallpaper;

import java.io.IOException;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        String path = "/sdcard/wallpaper.png";
        //通过BitmapFactory获取Bitmap实例
        Bitmap bm = BitmapFactory.decodeFile(path);
        try
        {
        	//设置桌面
        	setWallpaper(bm);
        }catch(IOException e)
        {
        	e.printStackTrace();
        }
    }
}
 

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

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值