Maj*_*rya 3 android android-widget imageswitcher android-glide
为了创建图像幻灯片放映,我想使用带有计时器的图像切换器.我看过这篇博文非常清楚但它不会从网络加载图片.现在我想从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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试用滑动加载图像,但我不知道该怎么办.
这是很容易做到,你只需要使用载入图片Glide的ImageView,你可以从获得ImageSwitcher的方法imageSwitcher.getCurrentView().因此,您需要将方法内run的 代码替换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());
Run Code Online (Sandbox Code Playgroud)
另外,不要忘记用适当的网址替换你的图片网址(你现在有一些我看到的虚拟网址).所以你的gallery数组应该是一个String[]数组.
别忘了也包括android.permission.INTERNET你的AndroidManifest.xml.
最后,你需要改变android:layout_width你的财产ImageSwitcher,以match_parent在XML作为滑翔不会加载它,否则图像.
| 归档时间: |
|
| 查看次数: |
2991 次 |
| 最近记录: |