0

hi im still fairly new to android and im having an issue with one of my activities, i have a layout with 4 imagebuttons each takes the user to a new activity, i want each of these imagebuttons to be animated i have the animations working in a different activity to make sure they work but if i add them to the image buttons the app force closes with java lang out of memory, so my question is how do i add these animations to my four ImageButtons without the app closing each time?

so far I've aggressively scaled back my images, and i'm using the onTouchEvent from the android tutorials sticking in a .XML in drawables and calling that to an imageview background my understanding is i just stick this on the ImageButton background, but this didn't work, any ideas

edit okay to be more specific my images are all images in res/drawables and are also stored as an animation list

xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/movingduck1" android:duration="200"/>
<item android:drawable="@drawable/movingduck2" android:duration="200"/>
<item android:drawable="@drawable/movingduck3" android:duration="200"/>
<item android:drawable="@drawable/movingduck4" android:duration="200"/>
<item android:drawable="@drawable/movingduck5" android:duration="200"/>
<item android:drawable="@drawable/movingduck6" android:duration="200"/>
<item android:drawable="@drawable/movingduck7" android:duration="200"/>
<item android:drawable="@drawable/movingduck8" android:duration="200"/>

</animation-list>

im calling it in a seperate activity (just to make sure it works, and it does) using this code

public class MainActivity2 extends Activity {
Button button;
AnimationDrawable movingDuck;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_2);


    ImageView movingduck = (ImageView) findViewById(R.id.ImageView);
    movingduck.setBackgroundResource(R.drawable.movingduck);
    movingDuck = (AnimationDrawable) movingduck.getBackground();


}

public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        movingDuck.start();
        return true;
    }
    return super.onTouchEvent(event);
}
}

on this xml

 <RelativeLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout">




<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:contentDescription="@string/duck_button"
    android:background="@drawable/movingduck"
    android:id="@+id/ImageView"
    android:scaleType="centerCrop"
    android:layout_centerInParent="true" />

</RelativeLayout>

however i want it on this layout attached to one of the image buttons

<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linear_layout">

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:id="@+id/linear_layout2">


    <ImageButton
        android:id="@+id/cat_button"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:contentDescription="@string/cat_button"
        android:src="@drawable/cat"
        android:scaleType="centerCrop"
        android:cropToPadding="true"
        android:onClick="cat_image"/>



    <ImageButton
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:contentDescription="@string/dog_button"
        android:id="@+id/dog_button"
        android:src="@drawable/dog"
        android:scaleType="centerCrop"
        android:onClick="dog_image"
        android:cropToPadding="true" />


</LinearLayout>

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/linear_layout4">

    <TextView
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:id="@+id/cat_text"
        android:text="@string/activity1_cat"
        android:textSize="23sp"
        android:textStyle="bold|italic"
        android:layout_weight="1"
        android:gravity="center" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/activity3_dog"
        android:textSize="23sp"
        android:id="@+id/textView"
        android:layout_gravity="right|center_vertical"
        android:textStyle="bold|italic"
        android:layout_weight="1"
        android:gravity="center" />

</LinearLayout>

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:id="@+id/linear_layout3">


    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/duck_button"
        android:layout_weight="1"
        android:src="@drawable/duck"
        android:id="@+id/duck_button"
        android:scaleType="centerCrop"
        android:onClick="duck_image"
        android:cropToPadding="true" />

    <ImageButton
        android:contentDescription="@string/cow_button"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:src="@drawable/cow"
        android:id="@+id/jumper_button"
        android:scaleType="centerCrop"
        android:cropToPadding="true"
        android:onClick="cow_image"/>


</LinearLayout>

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/linear_layout5">

    <TextView
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:id="@+id/duck_text"
        android:text="@string/activity2_duck"
        android:textSize="23sp"
        android:textStyle="bold|italic"
        android:layout_weight="1"
        android:gravity="center"
        android:paddingBottom="5dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/activity4_cow"
        android:textSize="23sp"
        android:id="@+id/cow_text"
        android:layout_gravity="right|center_vertical"
        android:textStyle="bold|italic"
        android:layout_weight="1"
        android:gravity="center"
        android:paddingBottom="5dp" />



</LinearLayout>

</LinearLayout>

but when i set it to be one of the image button backgrounds the app force closes with out of memory error each one of these buttons go to a seperate activity do i need to break it up into threads? and ive read the getwindow manager allows the animation to start without interaction can that be applied to an Image button? thanks for any and all feedback

martinseal1987
  • 1,862
  • 8
  • 44
  • 77
  • Can you provide as much code as possible, as well as a full stack trace? – Collin Flynn Dec 17 '13 at 20:29
  • How are you getting the image source? Are you using `Bitmap`s, `Drawable`s, or something else? You need to post your code to get a helpful response. – Phil Dec 17 '13 at 20:55
  • added code hope this is specific enough and please dont laugh but im not exactly sure what a stack trace is? @CollinFlynn – martinseal1987 Dec 17 '13 at 23:30

0 Answers0