欢迎莅临 SUN WU GANG 的园子!!!

世上无难事,只畏有心人。有心之人,即立志之坚午也,志坚则不畏事之不成。

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  470 随笔 :: 0 文章 :: 22 评论 :: 30万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

图像图像处理

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对象

 

 

 

 

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
布局文件==》
<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;
    }
 
}

 

posted on   sunwugang  阅读(193)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
点击右上角即可分享
微信分享提示