我试图在TranslateAnimation
上使用ImageView
,但ImageView
根本没有移动。运行项目时没有任何反应。这是我的代码:
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();
}
}
这是我的xml代码:
<?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>
我做错了什么?
答案 0 :(得分:2)
您无需从其他线程启动动画。所以只需在它初始化之后启动它
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);
答案 1 :(得分:0)