android学习笔记41——图形图像处理1

图像图像处理

ImageView:用于显示普通静态图片;

AnimationDrawable:用于开发逐帧动画;

Animation:用于对普通图片使用补间动画;

Bitmap、BitmapFactory

Bitmap代表一张位图,BitmapDrawable里封装的图片就是一个Bitmap对象。

开发者为了把一个Bitmap对象包装成BitmapDrawable对象,可以调用BitmapDrawable的构造器:

  BitmapDrawable drawable= new BitmapDrawable(bitmap);

如果需要获取BitmapDrawable所保证的bitmap对象,则可调用BitmapDrawable的getBitmap(),Eg:Bitmap bitmap = drawable.getBitmap.

除此之外,Bitmap还提供了一些静态方法用于创建Bitmap对象,如下所示:

createBitmap(Bitmap source,int x,int y,int width,int height)

从源位图source的指定坐标点开始,从中“挖取”宽width,高height的一块出来,

用于创建新的Bitmap对象

createScaledBitmap(Bitmap src,int dstwidth,int dstHeight,boolean filter) 对源位图src进行缩放,缩放成dstWidth,dstHeight的新位图
createBitmap(int width,int height,Bitmap.Config config) 创建一个宽width,高height的新位图 
createBitmap(Bitmap source,int x,int y, int width,int height,Matrix m,boolean filter)

从源位图source指定的坐标点开始,从中“挖取”宽width,高height的一块出来,

创建新的Bitmap对象。并按Matrix指定的规则进行变换。

--

BitmapFactory是一个工具类,其用于提供大量的方法,这些方法可用于从不同的数据源来解析、创建Bitmap对象。

BitmapFactory包含的方法如下:

decodeByteArray(byte[] data,int offset,int lenght) 从指定字节数的offset位置开始,将长度为length的字节数据解析成Bitmap对象
decodeFile(String filePathName) 从filePathName指定的文件中解析、创建Bitmap对象
decodeFileDescriptor(fileDescriptor fd) 用于从FileDescriptor对应的文件中解析、创建Bitmap对象
decodeResource(Resource res,int id) 用于根据给定的资源ID从指定资源中解析、创建Bitmap对象
decodeStream(InputStream is) 用于从指定输出流中解析、创建Bitmap对象

实例如下:实现点击按钮后切换图像显示

布局文件==》
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/btnTest"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="test" />

    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

代码实现==》
package com.example.mybitmap1;

import java.io.IOException;
import java.io.InputStream;

import android.os.Bundle;
import android.app.Activity;
import android.content.res.AssetManager;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity
{
	String[] Images = null;
	AssetManager Assets = null;
	Integer CurrentImg = 0;
	ImageView Img;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		final Button butTest = (Button) this.findViewById(R.id.btnTest);
		Img = (ImageView) this.findViewById(R.id.image);

		try
		{
			Assets = getAssets();
			// 获取/assets/目录下所有文件
			Images = Assets.list("");
			Log.i("swg", "Images length===" + Images.length);
		} catch (IOException e)
		{
			e.printStackTrace();
		}

		butTest.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				if (CurrentImg >= Images.length)
					CurrentImg = 0;
				// 查找指定类型的图片文件
				while (!Images[CurrentImg].endsWith(".ico") && !Images[CurrentImg].endsWith(".png")
						&& !Images[CurrentImg].endsWith(".jpg")
						&& !Images[CurrentImg].endsWith(".gif"))
				{
					CurrentImg++;
					if (CurrentImg >= Images.length)
						CurrentImg = 0;
				}

				InputStream stream = null;
				try
				{
					stream = Assets.open(Images[CurrentImg++]);
				} catch (IOException e)
				{
					e.printStackTrace();
				}

				BitmapDrawable bit = (BitmapDrawable) Img.getDrawable();
				// 如果图片还未回收,先强制收回改图片
				if (bit != null && !bit.getBitmap().isRecycled())
				{
					bit.getBitmap().recycle();
				}

				Img.setImageBitmap(BitmapFactory.decodeStream(stream));
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
原文地址:https://www.cnblogs.com/YYkun/p/5867992.html