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;
} }
android学习笔记41——图形图像处理1的更多相关文章
- android学习笔记43——图形图像处理3——Path
Path类 Path类可以预先在View上将N个点连成一条“路径”,然后调用Canavas的drawPath(path,paint)即可沿着路径绘制图形. android还为路径绘制提供了PathEf ...
- android学习笔记42——图形图像处理2——绘图
绘图 android的绘图应该继承View组件,并重写onDraw(Canvas canvas)方法即可. 重写onDraw(Canvas canvas)方法时涉及一个绘图API:Canvas,Can ...
- Android学习笔记进阶18 之画图并保存图片到本地
1.首先创建一个Bitmap图片,并指定大小: 2.在该图片上创建一个新的画布Canvas,然后在画布上绘制,并保存即可: 3.需要保存的目录File,注意如果写的目录如“/sdcard/so ...
- Android学习笔记进阶18之画图并保存图片到本地
1.首先创建一个Bitmap图片,并指定大小: 2.在该图片上创建一个新的画布Canvas,然后在画布上绘制,并保存即可: 3.需要保存的目录File,注意如果写的目录如“/sdcard/so ...
- Android 学习笔记之Volley(七)实现Json数据加载和解析...
学习内容: 1.使用Volley实现异步加载Json数据... Volley的第二大请求就是通过发送请求异步实现Json数据信息的加载,加载Json数据有两种方式,一种是通过获取Json对象,然后 ...
- Android学习笔记进阶之在图片上涂鸦(能清屏)
Android学习笔记进阶之在图片上涂鸦(能清屏) 2013-11-19 10:52 117人阅读 评论(0) 收藏 举报 HandWritingActivity.java package xiaos ...
- android学习笔记36——使用原始XML文件
XML文件 android中使用XML文件,需要开发者手动创建res/xml文件夹. 实例如下: book.xml==> <?xml version="1.0" enc ...
- Android学习笔记之JSON数据解析
转载:Android学习笔记44:JSON数据解析 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,为Web应用开发提供了一种 ...
- udacity android 学习笔记: lesson 4 part b
udacity android 学习笔记: lesson 4 part b 作者:干货店打杂的 /titer1 /Archimedes 出处:https://code.csdn.net/titer1 ...
随机推荐
- python numpy 的运算
一,基本运算 >>> a = array([1,2,3,4])>>> aarray([1, 2, 3, 4])>>> b=arange(4)> ...
- ZSDR017-客户订货价格和库存
*----------------------------------------------------------------------*ZSDR017-客户订货价格和库存*---------- ...
- Codeforces Round #308 (Div. 2) A B C 水 数学
A. Vanya and Table time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Round #150 (Div. 2)
A. Dividing Orange 模拟. B. Undoubtedly Lucky Numbers 暴力枚举\(x.y\). C. The Brand New Function 固定左端点,右端点 ...
- Android项目——电话拨号器
因为应用要使用手机的电话服务,所以要在清单文件AndroidManifest.xml中添加电话服务权限: <?xml version="1.0" encoding=" ...
- java编程之:生成rsa密钥
通过openssl工具生成RSA的公钥和私钥(opnssl工具可在互联网中下载到,也可以点此下载无线接口包,里面包含此工具) 打开openssl文件夹下的bin文件夹,执行openssl.exe文件: ...
- caffe matlab 借口怎么提取灰度图的 feature ? What happened if I mixed the color images with gray images together for training ?
1. caffe matlab 接口提供了提取feature的脚本,但是由于中间要对这些图像进行RGB ---> BGR 的变换,卧槽,灰度图没有三通道啊?怎么破?从上午就在纠结怎么会跑着跑着程 ...
- C#中有关string和byte[]转换的问题
byte[] byteArray = System.Text.Encoding.Default.GetBytes( str ); 怎么样,够简单吧? 反过来也是一样,把byte[]转成string: ...
- Concurrent inserts on MyISAM and the binary log
Recently I had an interesting surprise with concurrent inserts into a MyISAM table. The inserts were ...
- 经典ASP.NET MVC3.0入门详解
http://blog.csdn.net/csh624366188/article/details/7064269 :由于本文原在word文档里编写,写本文章时运用了大量截图,直接复制到博客里,没有显 ...