Xin chào mọi người, hôm nay mình sẽ giới thiệu cho các bạn về phương thức của bitmap cũng như của bitmapfactory trong android.
Bitmap android cung cấp các phương thức tĩnh sau để tạo đối tượng Bitmap:
Phương thức bitmap trong android

1. CreateBitmap(Bitmap source, int x , int y, int width, int height).
2. CreateScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter).
3. CreateBitmap(int width, int height, Bitmap.Config config)
4. CreateBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)
BitmapFactory in android provides the following methods to parse and create Bitmap objects from different data sources.
1. decodeByteArray(byte[] data,int offset,int length);
2. decodeFile(String pathName);
3. decodeFileDescriptor(FileDescriptor fd);
4. decodeResource(Resource res,int id);
5. decodeStream(inputStream is);
Android provides Bitmap with two methods to determine if it is recycled and to force Bitmap to recycle itself.
boolean isRecycled();
void recycle();
Below code:
public class MainActivity extends Activity
{
    String[] images = null;
    AssetManager assets = null;
    int currentImg = 0;
    ImageView image;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        image = (ImageView) findViewById(R.id.image);
        try
        {
            assets = getAssets();
           
            images = assets.list("");
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
     
        final Button next = (Button) findViewById(R.id.next);
        
        next.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View sources)
            {
                
                if (currentImg >= images.length)
                {
                    currentImg = 0;
                }
               
                while (!images[currentImg].endsWith(".png")
                        && !images[currentImg].endsWith(".jpg")
                        && !images[currentImg].endsWith(".gif"))
                {
                    currentImg++;
               
                    if (currentImg >= images.length)
                    {
                        currentImg = 0;
                    }
                }
                InputStream assetFile = null;
                try
                {
                   
                    assetFile = assets.open(images[currentImg++]);
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
                BitmapDrawable bitmapDrawable = (BitmapDrawable) image
                    .getDrawable();
              
                if (bitmapDrawable != null
                        && !bitmapDrawable.getBitmap().isRecycled()) // ①
                {
                    bitmapDrawable.getBitmap().recycle();
                }
               
                image.setImageBitmap(BitmapFactory
                    .decodeStream(assetFile)); // ②
            }
        });
    }
}



Tags :example bitmap on android, demo bitmapfactory in android, example android bitmap, android bitmapfactory example.
Facebook Google twitter
Từ khóa: Example Bitmap and BitmapFactory in android