问题描述:

I create an ImageSwitcher by code:

<?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>

But when I run this, this ImageSwitcher background still BLACK, not transprent as I expected. How do I fix it?

网友答案:

When you go to set the factory on the ImageSwitcher, be sure that the ViewFactory returns a transparent background:

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;
    }
});

That fixed it for me.

网友答案:

As a workaround to this problem, I used the ViewFlipper instead of the ImageSwitcher. I just want to animate between predefined images, so ViewFlipper is ok for me. I haven't tried but I think you can use 2 imageviews, set the correct image on the next view and call viewflipper.showNext().

Edit: In the end I had to do what I wrote above in the project I was working on, and it works.

In the layout.xml you have:

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

Then you create a class that handles the image switch:

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();
}
}

From your activity, you build an 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);

Every time you want to change the content you call the method:

imageSwitcher.setImage(Drawable d)
网友答案:

Setting the background color of the ImageSwitcher control does not work. To remove the black background, you just need to set the background color each views inside the Imageswitcher to transparent:

_imageSwitcher.getCurrentView().setBackgroundColor(getResources().getColor(R.color.transparent));

Do this for each call to _imageSwitcher.setImageResource() and your are done :)

相关阅读:
Top