我正在尝试使用Android Studio制作一个简单的Android翻译动画,但我无法让它工作。我已经使用了几个教程,但是他们总是需要了解太多细节才能理解。我是应用程序开发的完全初学者,所以我想从学习最基本的动画开始。我只想让我的图像移动到不同的位置,但模拟器不断给我ANR消息,要求我等待或关闭它。每当我点击等待时,无论我等多久我的动画都不会发生。我尝试将动画移动到一个线程,但应用程序关闭而不是给我ANR消息。我无法让它发挥作用。我想最终制作一个类似flappy bird的应用程序,仅用于学习目的,因此任何关于使用哪些动画和简单教程的建议也会有所帮助。请记住,我是一个完整的初学者,因此首选详细说明。谢谢!
这是我的主要活动:
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void startApp(View view)
{
Intent intent = new Intent(this, Game.class);
startActivity(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是我的第二项活动(游戏活动):
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.view.animation.Animation;
public class Game extends ActionBarActivity {
ImageView imageView;
Animation animation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
imageView = (ImageView) findViewById(R.id.imageGame);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_game, menu);
return true;
}
public void anim(View view)
{
animation = new TranslateAnimation(0, 100, 0, 100);
animation.setDuration(2000);
imageView.startAnimation(animation);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是我的activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/mario_background"
android:id="@+id/mainLayout"
android:onClick="startApp"
android:gravity="center" >
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/untitled" />
</LinearLayout>
这是我的activity_game.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/mario_background"
android:id="@+id/gameLayout"
android:onClick="anim" >
<ImageView
android:id="@+id/imageGame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/untitled" />
</LinearLayout>