Saturday, December 29, 2018

Example Bitmap and BitmapFactory in android

Bitmap android provides the following static methods to create a Bitmap object: Bitmap in android methods. 1. CreateBitmap(Bitmap sou... thumbnail 1 summary
Bitmap android provides the following static methods to create a Bitmap object:
Bitmap in android methods.
Example Bitmap and BitmapFactory in 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.

No comments

Post a Comment