Changing a bitmap in a loop

I have a canvas and I want to change the bitmap every 50 milliseconds.

Basically what I am trying to do is like animating a gif.

As you can see there are 4 images and every 50 milliseconds I want it to change the image.

the code below doesn't work and I don't know why.

protected void onDraw(final Canvas canvas) {

    res = getResources();
    image = BitmapFactory.decodeResource(res, R.drawable.image_1);
    canvas.drawBitmap(image, 0, 0, paint);

    new Thread(new Runnable() {

        @Override
        public void run() {
            while (!Thread.interrupted())
                try {
                    Thread.sleep(50);
                    System.out.println("OK2");

                    time++;
                    ((Activity) context).runOnUiThread(new Runnable() {
                        @Override
                        public void run() {

                            if (time == 1) {

                                image = BitmapFactory.decodeResource(res,
                                        R.drawable.image_1);

                                canvas.drawBitmap(image, 0, 0, paint);
                                invalidate();
                            }
                            if (time == 2) {
                                image = BitmapFactory.decodeResource(res,
                                        R.drawable.image_2);

                                canvas.drawBitmap(image, 0, 0, paint);
                                invalidate();

                            }

                            if (time == 3) {
                                image = BitmapFactory.decodeResource(res,
                                        R.drawable.image_3);

                                canvas.drawBitmap(image, 0, 0, paint);
                                invalidate();

                            }

                            if (time >= 4) {

                                time = 0;
                                image = BitmapFactory.decodeResource(res,
                                        R.drawable.image_4);
                                canvas.drawBitmap(image, 0, 0, paint);
                                invalidate();

                            }
                        }
                    });
                } catch (InterruptedException e) {

                }
        }

    }).start();
    super.onDraw(canvas);

}

      

Thanks in advance.

+3


source to share


2 answers


There are several problems:

You are decrypting resources every time. decodeResource

may not be fast enough to load the image in 50 milliseconds. It would be faster to decode the frames once and store them in an array. eg:.

Bitmap images[4];
void loadFrames()
{
    res = getResources();
    images[0] = BitmapFactory.decodeResource(res, R.drawable.image_1);
    images[1] = BitmapFactory.decodeResource(res, R.drawable.image_2);
    images[2] = BitmapFactory.decodeResource(res, R.drawable.image_3);
    images[3] = BitmapFactory.decodeResource(res, R.drawable.image_4);
}

      

You start a new thread every time it is called onDraw()

, which means hundreds of threads will be created. Instead, you should only create the stream once. Inside the stream, increment your counter and set image

to the correct frame, then call invalidate:



 Thread.sleep(50);
 time = (time + 1) % 4;
 image = images[time];
 ((Activity) context).runOnUiThread(new Runnable() {
      @Override
      public void run() {
          invalidate();
      }
 };

      

Then inside onDraw()

draw the current image onto the canvas:

 canvas.drawBitmap(image, 0, 0, paint);

      

+4


source


You can use this link here . This is for sliding images

    public void imageSlider(){
    HashMap<String,Integer> file_maps = new HashMap<String, Integer>();
    file_maps.put("Palava Titans - Lodha Palava, Dombivali",R.drawable.img1);
    file_maps.put("Dribble Football -Imax, Wadala",R.drawable.img2);
    file_maps.put("Astro Park - Atria, Worli",R.drawable.img3);
    file_maps.put("TigerPlay - Citimall, Andheri w", R.drawable.img4);
    file_maps.put("DSF - Ryan International, Andheri", R.drawable.img5);
    file_maps.put("The Arena, Sakinaka", R.drawable.img6);
    file_maps.put("Astro Park - Smash, Lower Parel", R.drawable.img7);
    file_maps.put("DSF - Rcity, Ghatkopar", R.drawable.img8);
    file_maps.put("Father Agnel - Vashi", R.drawable.img9);
    file_maps.put("Footbrawl - Milan Subway", R.drawable.img10);

    for(String name : file_maps.keySet()){
        TextSliderView textSliderView = new TextSliderView(this);
        // initialize a SliderLayout
        textSliderView
        .description(name)
        .image(file_maps.get(name))
        .setScaleType(BaseSliderView.ScaleType.Fit);
        //add your extra information
        textSliderView.getBundle()
        .putString("extra",name);

        mDemoSlider.addSlider(textSliderView);
    }
    mDemoSlider.setPresetTransformer(SliderLayout.Transformer.Accordion);
    mDemoSlider.setPresetIndicator(SliderLayout.PresetIndicators.Right_Bottom);
    mDemoSlider.setCustomAnimation(new DescriptionAnimation());
    mDemoSlider.setDuration(5000);
}

      



call this in onCreate method

0


source







All Articles