ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2019-08-29 05:25:43  阅读:215  来源: 互联网

标签:android-glide android android-widget imageswitcher


为了创建图像幻灯片放映,我想使用带有计时器的图像切换器.
我读了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-glide,android,android-widget,imageswitcher
来源: https://codeday.me/bug/20190829/1757674.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有