0

Is there a way of animating the changing of background image for a Relative Layout? Thanks.

SleepNot
  • 2,982
  • 10
  • 43
  • 72
  • which type of animation you want? – Narendra Pal Aug 06 '12 at 04:07
  • you can check this link. http://stackoverflow.com/questions/4503039/layout-animation-not-working-on-first-run – Narendra Pal Aug 06 '12 at 04:08
  • Or this one also be useful for the factor of animation. http://stackoverflow.com/questions/8933224/relativelayout-causes-animation-not-to-work – Narendra Pal Aug 06 '12 at 04:09
  • I am changing the background of an image on the same activity with a button and want it to have a fading animation while or before loading the next background into the relative layout. – SleepNot Aug 06 '12 at 04:15

1 Answers1

1

I found work-around for it,, :)

<ImageView
        android:id="@+id/giftAppBgImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/giftApp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/giftApp"
        android:layout_below="@+id/TopborderBGImage"
        android:clickable="true"
        android:scaleType="fitXY"
        android:src="@drawable/cell_bg" />

    <RelativeLayout
        android:id="@+id/giftApp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/TopborderBGImage"
        android:background="@null"
        android:clickable="false"
        android:focusable="false"
        android:focusableInTouchMode="false" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:src="@drawable/gift_icon" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/imageView1"
            android:text="Gift this app"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="@color/titleMiddleColor"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/moreShaderCell1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/textView1"
            android:layout_below="@+id/textView1"
            android:text="Continuous reward for you, InshaAllah!"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@color/titleMiddleColor" />
    </RelativeLayout>

setOnClickListener

findViewById(R.id.giftAppBgImage).setOnClickListener(this);

@Override
    public void onClick(final View v) {
        // TODO AK-generated method stub
        showLog("catch onClick event!" + v.getClass().getName());
        if (v instanceof ImageView) {
            ((ImageView) v).setImageResource(R.drawable.cell_bg_hover);
            Animation anim = AnimationUtils.loadAnimation(getActivity(), R.anim.more_animation);
            anim.setAnimationListener(new AnimationListener() {

                @Override
                public void onAnimationStart(Animation arg0) {
                    // TODO AK-generated method stub

                }

                @Override
                public void onAnimationRepeat(Animation arg0) {
                    // TODO AK-generated method stub

                }

                @Override
                public void onAnimationEnd(Animation arg0) {
                    // TODO AK-generated method stub
                    ((ImageView) v).setImageResource(R.drawable.cell_bg);
                }
            });
            v.startAnimation(anim);

        }
    }

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:interpolator="@android:anim/accelerate_interpolator">
    <alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:fillAfter="true"
        android:duration="500"/>

</set>
Arfan Mirza
  • 668
  • 1
  • 14
  • 24