android– 如何使用ImageSwitcher中的Glide加载图像

android– 如何使用ImageSwitcher中的Glide加载图像,第1张

概述为了创建图像幻灯片放映,我想使用带有计时器的图像切换器.我读了thisblogpost它非常清楚但它不会从网络加载图像.现在我想从GlideLibrary加载网络图像.ThisisMainActivity:publicclassMainActivityextendsActivity{privateImageSwitcherimageSwitcher;

为了创建图像幻灯片放映,我想使用带有计时器的图像切换器.
我读了this blog post它非常清楚但它不会从网络加载图像.
现在我想从GlIDe library加载网络图像.

This is 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将图像加载到ImageVIEw,您可以通过方法imageSwitcher.getCurrentVIEw()从ImageSwitcher获取.因此,您需要将runOnUiThread方法运行中的代码替换为下一个代码:

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中.

最后,您需要将ImageSwitcher的androID:layout_wIDth属性更改为xml中的match_parent,否则GlIDe将不会在其中加载图像.

总结

以上是内存溢出为你收集整理的android – 如何使用ImageSwitcher中的Glide加载图像全部内容,希望文章能够帮你解决android – 如何使用ImageSwitcher中的Glide加载图像所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1108432.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存