2012/2013 учебный год
Упражнение 1
В программе должна выполняться анимация вращения и масштабирования текстовой строки. При нажатии на кнопку Поворот выполняется один вид анимации, а при нажатии на кнопку Масштаб - второй вид.
Внимание! Откройте файл
gen/org/ggpi/AnimClick01/R.java
и проверяйте после выполнения каждого из ниже приведённых соответствующих примеров, что в нём зарегистрированы объекты
frameLayout linearLayout1 btnRotate btnScale tv myrotate myscale
Пример 1.1. Файл res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/frameLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:id="@+id/linearLayout1" android:layout_gravity="center_vertical|center_horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/btnRotate" android:layout_width="100dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_margin="30dp" android:text="Поворот" > </Button> <Button android:id="@+id/btnScale" android:layout_width="100dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_margin="30dp" android:text="Масштаб" > </Button> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical|center_horizontal" android:text="@string/hello_world" android:textSize="38sp" > </TextView> </LinearLayout> </FrameLayout>
Создайте папку res/anim/
Пример 1.2. Файл res/anim/myrotate.xml
Создайте файл res/anim/myrotate.xml
<?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:fromDegrees="0" android:pivotX="50%" android:pivotY="50%" android:toDegrees="360" android:duration="3000"> </rotate>
Пример 1.3. Файл res/anim/myscale.xml
Создайте файл res/anim/myscale.xml
<?xml version="1.0" encoding="utf-8"?> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:fromXScale="0.1" android:toXScale="1.0" android:fromYScale="0.1" android:toYScale="1.0" android:pivotX="50%" android:pivotY="50%" android:duration="3000"> </scale>
Пример 1.4. Файл MainActivity.java
package org.ggpi.AnimClick01; import android.os.Bundle; import android.app.Activity; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.view.animation.Animation; import android.view.animation.AnimationUtils; public class MainActivity extends Activity implements OnClickListener { TextView tv; Button btnRotate; Button btnScale; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // найдем View-элементы tv = (TextView) findViewById(R.id.tv); btnRotate = (Button) findViewById(R.id.btnRotate); btnScale = (Button) findViewById(R.id.btnScale); // присвоим обработчик кнопкам btnRotate.setOnClickListener(this); btnScale.setOnClickListener(this); } @Override public void onClick(View v) { Animation anim = null; // по id определяем кнопку, вызвавшую этот обработчик switch (v.getId()) { case R.id.btnRotate: // кнопка Поворот anim = AnimationUtils.loadAnimation(this, R.anim.myrotate); break; case R.id.btnScale: // кнопка Масштаб anim = AnimationUtils.loadAnimation(this, R.anim.myscale); break; } tv.startAnimation(anim); } }