3

为了创建图像幻灯片,我想使用带有计时器的图像切换器。我读了这篇博文,很清楚,但它没有从网络加载图像。现在我想使用 Glide Library 从网络加载图像。

这是 MainActivity :

public class MainActivity extends Activity {
    private ImageSwitcher imageSwitcher;

    private int[] gallery = { http://www.helloworld.com/image1.png, http://www.helloworld.com/image2.png, http://www.helloworld.com/image3.png,
            http://www.helloworld.com/image4.png, };

    private int position;

    private static final Integer DURATION = 2500;

    private Timer timer = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);
        imageSwitcher.setFactory(new ViewFactory() {

            public View makeView() {
                return new ImageView(MainActivity.this);
            }
        });

        // Set animations
        // https://danielme.com/2013/08/18/diseno-android-transiciones-entre-activities/
        Animation fadeIn = AnimationUtils.loadAnimation(this, R.anim.fade_in);
        Animation fadeOut = AnimationUtils.loadAnimation(this, R.anim.fade_out);
        imageSwitcher.setInAnimation(fadeIn);
        imageSwitcher.setOutAnimation(fadeOut);
    }

    // ////////////////////BUTTONS
    /**
     * starts or restarts the slider
     * 
     * @param button
     */
    public void start(View button) {
        if (timer != null) {
            timer.cancel();
        }
        position = 0;
        startSlider();
    }

    public void stop(View button) {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
    }

    public void startSlider() {
        timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {

            public void run() {
                // avoid exception:
                // "Only the original thread that created a view hierarchy can touch its views"
                runOnUiThread(new Runnable() {
                    public void run() {
                        imageSwitcher.setImageResource(gallery[position]);
                        position++;
                        if (position == gallery.length) {
                            position = 0;
                        }
                    }
                });
            }

        }, 0, DURATION);
    }

    // Stops the slider when the Activity is going into the background
    @Override
    protected void onPause() {
        super.onPause();
        if (timer != null) {
            timer.cancel();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (timer != null) {
            startSlider();
        }

    }

}

我尝试使用 glide 加载图像,但我不知道该怎么做。

4

3 回答 3

5

这很容易做到,您只需将图像加载GlideImageView您可以ImageSwitcher通过方法获得的图像imageSwitcher.getCurrentView()。因此,您需要将方法中的代码替换runrunOnUiThread下一个代码:

Glide.with(MainActivity.this)
    .load(gallery[position])
    .asBitmap()
    .listener(new RequestListener<String, Bitmap>() {
        @Override
        public boolean onException(Exception e, String model, Target<Bitmap> target, boolean isFirstResource) {
            return false;
        }

        @Override
        public boolean onResourceReady(Bitmap resource, String model, Target<Bitmap> target, boolean isFromMemoryCache, boolean isFirstResource) {
            position++;
            if (position == gallery.length) {
                position = 0;
            }
            imageSwitcher.setImageDrawable(new BitmapDrawable(getResources(), resource));
            return true;
        }
    }).into((ImageView) imageSwitcher.getCurrentView());

另外不要忘记用适当的网址替换您的图片网址(您现在有一些我看到的虚拟网址)。所以你的gallery数组应该是一个String[]数组。

不要忘记也包括android.permission.INTERNET到您的AndroidManifest.xml.

最后,您需要在 xml 中更改 to的android:layout_width属性,否则 Glide 不会在其中加载图像。ImageSwitchermatch_parent

于 2016-06-19T10:11:29.403 回答
1

我认为批准的答案很棒,但缺少一些关键。当他们更新当前图像时,这基本上只是替换了视图中的图像。这意味着我们可能根本不使用 ImageSwitcher。我们需要做的是更新下一个视图然后显示它。这将使我们还可以看到我们添加的任何过渡效果。

我已经在我自己的代码中分离了所有这些逻辑以使其干净,但这里它是 RAW 形式。

设置 ImageSwitcher

   

    Animation in = AnimationUtils.loadAnimation(this,android.R.anim.fade_in);
       Animation out = AnimationUtils.loadAnimation(this,android.R.anim.fade_out);
       imageSwitcher.setFactory(() -> {
                ImageView imageView = new ImageView(getApplicationContext());
                imageView.setLayoutParams(new FrameLayout.LayoutParams(
                        FrameLayout.LayoutParams.MATCH_PARENT,
                        FrameLayout.LayoutParams.MATCH_PARENT
                ));
                return imageView;
            });
       imageSwitcher.setInAnimation(in);
       imageSwitcher.setOutAnimation(out);

  

调用它然后更新下一个图像

   

    RequestOptions requestOptions = new 
       RequestOptions().diskCacheStrategy(DiskCacheStrategy.ALL);
       Bitmap nextImage = getAppropriateImage();
       GlideApp.with(this)
          .load(nextImage)
          .apply(requestOptions)
          .into((ImageView) imageSwitcher.getNextView());
       imageSwitcher.showNext();

于 2021-05-13T10:33:06.443 回答
0

我使用了@rom4ek 的版本,但我遇到了一些崩溃:

致命异常:java.lang.RuntimeException Canvas:尝试使用回收的位图 android.graphics.Bitmap@7ad49c4

我认为这是因为drawable没有设置成相同的ImageView传递给into(...).

我将其更改为使用“下一个视图”。我们必须为 Glide设置从GONEto的可见性。INVISIBLE

imageSwitcher.getNextView().setVisibility(View.INVISIBLE);
Glide.with(...)
    .load(url)
    .listener(new RequestListener<String, Bitmap>() {
        @Override
        public boolean onException(Exception e, String model, Target<Drawable> target, boolean isFirstResource) {
            return false;
        }

        @Override
        public boolean onResourceReady(Drawable resource, String model, Target<Drawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
            imageSwitcher.setImageDrawable(resource);
            return true;
        }
    }).into((ImageView) imageSwitcher.getNextView());

科特林版本:

it.nextView.visibility = View.INVISIBLE
Glide.with(...)
    .load(url)
    .listener(object : RequestListener<Drawable> {
        override fun onLoadFailed(
            e: GlideException?,
            model: Any?,
            target: Target<Drawable>?,
            isFirstResource: Boolean
        ): Boolean {
            return false
        }

        override fun onResourceReady(
            resource: Drawable?,
            model: Any?,
            target: Target<Drawable>?,
            dataSource: DataSource?,
            isFirstResource: Boolean
        ): Boolean {
            it.setImageDrawable(resource)
            return true
        }
    })
    .into(it.nextView as ImageView)
于 2018-04-13T12:14:01.997 回答