Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TranslateAnimation on an ImageView doesn't work

I'm trying to use a TranslateAnimation on an ImageView, but the ImageView doesn't move at all. Nothing happens when I run the project. Here's my code:

import android.app.Activity;
import android.os.*;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

public class MainActivity extends Activity {
ImageView a;
TranslateAnimation pengesat;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    a = (ImageView) findViewById(R.id.imageView3);
    pengesat = new TranslateAnimation( 0.0f,  1.0f,  0.0f,  0.0f );
    pengesat.setDuration(5000);
    pengesat.setRepeatCount(1);
    pengesat.setInterpolator(new AccelerateInterpolator());
    Thread myThread= new Thread(new Runnable(){
        @Override
        public void run() {
            a.post(new Runnable() {
                @Override
                public void run() {
                    a.startAnimation(pengesat);
                }
            });
        }
    });
    myThread.start();
  }
}

And here's my xml code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@string/layout"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.ennio.lojaprove.MainActivity">

<ImageView
    android:id="@+id/img"
    android:src="@drawable/bg"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:scaleType="centerCrop"
    android:layout_alignParentBottom="true"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />
   <ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView"
    android:src="@drawable/border"
    android:scaleType="centerCrop"
    android:layout_alignTop="@+id/img"
    android:layout_centerHorizontal="true" />
  <ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView2"
    android:src="@drawable/border"
    android:scaleType="centerCrop"
    android:layout_alignBottom="@+id/img"
    android:layout_centerHorizontal="true" />
  <ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView3"
    android:src="@drawable/pengesa"
    android:layout_above="@+id/imageView2"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

   </RelativeLayout>

What am I doing wrong?

like image 475
dreameral Avatar asked Jan 01 '16 14:01

dreameral


1 Answers

You do not need to start animation from another thread. So just start it after it's initialized

a = (ImageView) findViewById(R.id.imageView3);
pengesat = new TranslateAnimation( 0.0f,  1.0f,  0.0f,  0.0f );
pengesat.setDuration(5000);
pengesat.setRepeatCount(1);
pengesat.setInterpolator(new AccelerateInterpolator());
a.startAnimation(pengesat);
like image 123
mohax Avatar answered Oct 25 '22 07:10

mohax