簡體   English   中英

在循環上更改位圖圖像

[英]Changing bitmap image on loop

我有一個畫布,我希望每50毫秒更改一次位圖圖像。

基本上我正在嘗試做的就像一個gif動畫。

正如你所看到的那樣,有4張圖像,每50毫秒我想要它來改變圖像。

下面的代碼不起作用,我不知道為什么。

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);

}

提前致謝。

有幾個問題:

您每次都在重復解碼資源。 decodeResource可能不夠快,無法在50毫秒內加載圖像。 解碼幀一次並將它們存儲在一個數組中會更快。 例如:

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);
}

每次調用onDraw()都會啟動一個新線程,這意味着將創建數百個線程。 相反,您應該只創建一次線程。 在線程內部,遞增計數器並將image設置為正確的幀,然后調用invalidate:

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

然后在onDraw()內部,使用當前圖像繪制到畫布上:

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

您可以在此處使用此鏈接。 這是為了滑動圖像

    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);
}

在onCreate方法中調用它

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM