我通过代码创建了一个ImageSwitcher:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#FFFFFF"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Gallery
android:id="@+id/Gallery01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></Gallery>
<ImageSwitcher
android:id="@+id/ImageSwitcher01"
android:background="@android:color/transparent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ImageSwitcher>
</LinearLayout>
但是当我运行它时,这个ImageSwitcher背景仍然是黑色的,而不是像我预期的那样翻译。我该如何解决?
答案 0 :(得分:2)
当您在ImageSwitcher上设置工厂时,请确保ViewFactory返回透明背景:
myImageSwitcher.setFactory(new ViewFactory() {
@Override
public View makeView() {
ImageView imageView = new ImageView(getActivity());
imageView.setBackgroundColor(0x00000000); // <--- return transparent bg
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setLayoutParams(new ImageSwitcher.LayoutParams( ImageSwitcher.LayoutParams.FILL_PARENT, ImageSwitcher.LayoutParams.FILL_PARENT));
return imageView;
}
});
为我修好了。
答案 1 :(得分:1)
作为解决此问题的方法,我使用了ViewFlipper而不是ImageSwitcher。 我只想在预定义图像之间制作动画,因此ViewFlipper对我来说没问题。 我没试过,但我认为你可以使用2张图像视图,在下一个视图上设置正确的图像并调用viewflipper.showNext()。
编辑:最后,我必须在我正在处理的项目中执行上面所写的操作,并且它有效。
在layout.xml中,你有:
<ViewFlipper
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="@+id/viewFlipper">
<ImageView
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:src="@drawable/market_product01"
android:id="@+id/imgFirst">
</ImageView>
<ImageView
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="@+id/imgSecond">
</ImageView>
</ViewFlipper>
然后创建一个处理图像切换的类:
public class MyImageViewSwitcher {
private ViewFlipper flipper;
private ImageView[] views;
private int currentView = 0;
public MyImageViewSwitcher(ViewFlipper flipper, ImageView first, ImageView second) {
currentView = 0;
this.flipper = flipper;
views = new ImageView[2];
views[0] = first;
views[1] = second;
}
public void setImage(Drawable image) {
currentView = (currentView + 1) % 2;
views[currentView].setImageDrawable(image);
flipper.showNext();
}
}
从您的活动中,您构建了一个ImageViewSwitcher:
ViewFlipper viewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper);
viewFlipper.setInAnimation(android.R.anim.fadeIn);
viewFlipper.setOutAnimation(android.R.anim.fadeOut);
ImageView imgFirst = (ImageView) findViewById(R.id.imgFirst);
ImageView imgSecond = (ImageView) findViewById(R.id.imgSecond);
this.imageSwitcher = new MyImageViewSwitcher(viewFlipper, imgFirst, imgSecond);
每次要更改内容时,请调用方法:
imageSwitcher.setImage(Drawable d)
答案 2 :(得分:0)
设置ImageSwitcher控件的背景颜色不起作用。要删除黑色背景,您只需将Imageswitcher内每个视图的背景颜色设置为透明:
_imageSwitcher.getCurrentView().setBackgroundColor(getResources().getColor(R.color.transparent));
为每次调用_imageSwitcher.setImageResource()执行此操作并完成操作:)