Bitmapfactory例子 [英] Bitmapfactory example

查看:196
本文介绍了Bitmapfactory例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个动态的图像视图,在我的画廊每一个形象将要使用bitmapfactory,而不是图像绘制结合的图像视图。有一些网站,有bitmapfactory教程呢?我相信,通过bitmapfactory使用较少的内存,图像结合成图像的看法?这是正确的吗?我还要内存泄漏的风险最小化这就是我为什么要使用bitmapfactory。请帮忙。我找不到基本的例子,教bitmapfactory。

I want to create a dynamic image view where every image in my gallery will going to use bitmapfactory and not image drawable that binds in the image view. Is there some sites that has bitmapfactory tutorial for this? i believe that using bitmapfactory uses less memory that binding the image into image view? Is this right? I want also to minimize the risk of memory leaks thats why I want to use bitmapfactory. Please help. I cant find basic examples that teaches bitmapfactory.

推荐答案

建筑位图对象

1)的从文件

使用亚行工具推选项复制test2.png到SD卡

Use the adb tool with push option to copy test2.png onto the sdcard

这是从SD卡装载位图的最简单方法。只需将路径传递到图像BitmapFactory.de codeFILE(),并让Android SDK中做休息。

This is the easiest way to load bitmaps from the sdcard. Simply pass the path to the image to BitmapFactory.decodeFile() and let the Android SDK do the rest.

public class TestImages extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ImageView image = (ImageView) findViewById(R.id.test_image);
        Bitmap bMap = BitmapFactory.decodeFile("/sdcard/test2.png");
        image.setImageBitmap(bMap);
    }
}

这一切,code确实是加载图像test2.png我们previously复制到SD卡。该BitmapFactory创建与此图像的位图对象,我们使用 ImageView.setImageBitmap()方法来更新ImageView的组成部分。

All this code does is load the image test2.png that we previously copied to the sdcard. The BitmapFactory creates a bitmap object with this image and we use the ImageView.setImageBitmap() method to update the ImageView component.

2)的从输入流

使用 BitmapFactory.de codeStream()转换成BufferedInputStream转换成位图对象。

Use BitmapFactory.decodeStream() to convert a BufferedInputStream into a bitmap object.

public class TestImages extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ImageView image = (ImageView) findViewById(R.id.test_image);
        FileInputStream in;
        BufferedInputStream buf;
        try {
            in = new FileInputStream("/sdcard/test2.png");
            buf = new BufferedInputStream(in);
            Bitmap bMap = BitmapFactory.decodeStream(buf);
            image.setImageBitmap(bMap);
            if (in != null) {
            in.close();
            }
            if (buf != null) {
            buf.close();
            }
        } catch (Exception e) {
            Log.e("Error reading file", e.toString());
        }
    }
}

这code使用基本的Java的FileInputStream和BufferedInputStream为创造 BitmapFactory.de codeStream输入流()。该文件访问code应该由try / catch块包围赶上抛出的FileInputStream或的BufferedInputStream任何异常。此外,当你与流完成后处理它们应该被关闭。

This code uses the basic Java FileInputStream and BufferedInputStream to create the input stream for BitmapFactory.decodeStream(). The file access code should be surrounded by a try/catch block to catch any exceptions thrown by FileInputStream or BufferedInputStream. Also when you're finished with the stream handles they should be closed.

3)的从你的Andr​​oid项目的资源

使用 BitmapFactory.de codeResource(RES,ID)来从一个Android资源获得一个位图。

Use BitmapFactory.decodeResource(res, id) to get a bitmap from an Android resource.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ImageView image = (ImageView) findViewById(R.id.test_image);
    Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
    image.setImageBitmap(bMap);
}

这篇关于Bitmapfactory例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆