Android - 图像

一个APP能否被用于接受,很大的一个因素是用户界面。Android系统提供了丰富的处理图像的功能。

如:ImageView 显示静态图片,AnimationDrawable 开发逐帧动画,Aniamtion对普通图片使用补间动画。


Drawable 对象

当你把图片复制到 drawable 资源文件夹中,Android SDK 会为该图片在 R 文件中创建一个索引项:R.drawable.file_name 。

你可以在 XML 中使用 @drawable/file_name 来访问该图片,也可以在 java 程序中通过 R.drawable.file_name 访问该图片。

Bitmap

Bitmap BitmapDrawable BitmapFactory 

Bitmap 代表位图,BitmapDrawable 可以封装 Bitmap 对象。

BitmapDrawable drawable = new BitmapDrawable(bitamp); 

也可以获取 BitmapDrawable 对象中的 bitmap 对象。

Bitmap bitmap = drawable.getBitmap();

BitmapFactory 是一个工具类,可以用于创建 Bitmap 对象。

image.setImageBitmap(BitmapFactory.decodeStream(inputstream)); // 从指定输入流中解析创建 Bitmap 对象并显示

Bitmap 对象的回收

一般,我们是通过资源 ID 来获取封装某图片的 Drawable 对象。

如果系统不停地解析、创建 Bitmap 对象,可能由于前面创建的 Bitmap 对象还没有被回收,而导致 OOM 。

Android 为 Bitmap 提供了两种方法来处理 Bitmap 对象的回收问题:

1  boolean isRecycle()  该 Bitmap 对象是否已被回收

2  void recycle()  强制 Bitmap 对象立即回收自己

if( bitmapDrawable != null  &&  !bitmapDrawable.getBitmap().isRecycle() ) { // 对象存在 && 对象还没被回收

bitmapDrawable.getBitmap().recycle();

}


CodeDemo

public class MyView extends View {

	public MyView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	protected void onDraw(Canvas canvas) { // 重写onDraw()
		super.onDraw(canvas);
		
		Paint paint = new Paint(); // 创建画笔对象
		paint.setAntiAlias(true); // 抗锯齿:边缘柔化
		paint.setColor(Color.BLUE); // 蓝色画笔
		int viewWidth = this.getWidth(); // 获得当前画布宽
		
		paint.setStyle(Paint.Style.STROKE); // 默认为填充,要改成描边。
		paint.setStrokeWidth(5); // 画笔宽
		canvas.drawCircle(viewWidth/5 + 60, viewWidth/5 + 10, viewWidth/10, paint); // 画圆
		canvas.drawRect(60, viewWidth/3 + 60, viewWidth/2 + 20, viewWidth*2/3 + 10, paint); // 画矩形
		Path path = new Path(); // 创建路径对象
		path.moveTo(100, viewWidth*9/10 + 60); // 初始点
		path.lineTo(viewWidth/3, viewWidth*2/3 + 60); // 边
		path.lineTo(viewWidth/3 + 45, viewWidth + 15); // 边
		path.close(); // 释放
		canvas.drawPath(path, paint); // 加入画布
		
		paint.setTextSize(48); // Text大小
		canvas.drawText("圆", viewWidth*3/5 + 20, viewWidth/5 + 20, paint); // 添加Text
		canvas.drawText("矩形", viewWidth*3/5 + 20, viewWidth/2 + 45, paint);
		canvas.drawText("三角形", viewWidth*3/5 + 20, viewWidth, paint);
	}
}

AnimationDrawable


一  新建drawable/anim.xml

<?xml version="1.0" encoding="utf-8"?>
<animation-list 
    xmlns:android="http://schemas.android.com/apk/res/android"  
    android:oneshot="false" >
    <item android:drawable="@drawable/a1" android:duration="200" />  
    <item android:drawable="@drawable/a2" android:duration="200" />  
    <item android:drawable="@drawable/a3" android:duration="200" /> 
</animation-list>
android:oneshot="false"  循环播放

android:duration="200"  延迟

二  activity_main.xml

    <Button 
        android:id="@+id/btn_start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="start" />
    <Button 
        android:id="@+id/btn_stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="stop" />
    <ImageView 
        android:id="@+id/iv"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
三  MainActivity.java

public class MainActivity extends Activity implements OnClickListener{

	ImageView iv;
	Button btn_start,btn_stop;
	AnimationDrawable animation;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		iv = (ImageView) findViewById(R.id.iv); 
		<span style="color:#ff0000;">iv.setBackgroundResource(R.drawable.anim);</span> 
		<span style="color:#ff0000;">animation = (AnimationDrawable) iv.getBackground();</span>
		
		btn_start = (Button) findViewById(R.id.btn_start);
		btn_stop = (Button) findViewById(R.id.btn_stop);
		btn_start.setOnClickListener(this);
		btn_stop.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_start:
			animation.start();
			break;
		case R.id.btn_stop:
			animation.stop();
			break;
		default:
			break;
		}
	}

	
}
核心

1  将 anim.xml 加载给ImageView

2  定义 AnimationDrawable 对象,并获取 ImageView 的背景作为启动界面。

3  start() 开启动画,stop() 停止动画。

注意

如果放入的图片过大,会报 OOM ,无法加载资源。










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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值