●實作練習:預期畫面
●實作練習:補充說明
(一)動畫=>動畫範例限定android api level 16(android 4.1),如果開專案的時候沒選api16,就在這邊改
(二)建立動畫Resource File
(三)AnimationDrawable說明
(四)任務說明
●實作練習:xml
(一)activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.student.lab06.MainActivity">
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/img"
android:layout_marginTop="20dp"
android:orientation="horizontal">
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click"
android:text="start" />
<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click"
android:text="stop" />
<Button
android:id="@+id/secs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click"
android:text="after5secStop" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<View
android:id="@+id/viewlogo"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:background="@drawable/nba_logo_spurs" />
<TextView
android:id="@+id/logoname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="" />
</LinearLayout>
<Button
android:id="@+id/go"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:onClick="go"
android:text="Go" />
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text=""
android:textSize="20sp" />
</LinearLayout>
(二)arrays.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="nbalogo">
<item>@drawable/nba_logo_knicks</item>
<item>@drawable/nba_logo_magic</item>
<item>@drawable/nba_logo_pistons</item>
<item>@drawable/nba_logo_spurs</item>
<item>@drawable/nba_logo_nets</item>
<item>@drawable/nba_logo_clippers</item>
<item>@drawable/nba_logo_76ers</item>
<item>@drawable/nba_logo_suns</item>
<item>@drawable/nba_logo_hornets</item>
<item>@drawable/nba_logo_raptors</item>
<item>@drawable/nba_logo_lakers</item>
<item>@drawable/nba_logo_warriors</item>
</array>
<string-array name="nabname">
<item>knicks</item>
<item>magic</item>
<item>pistons</item>
<item>spurs</item>
<item>nets</item>
<item>clippers</item>
<item>76ers</item>
<item>suns</item>
<item>hornets</item>
<item>raptors</item>
<item>lakers</item>
<item>warriors</item>
</string-array>
</resources>
(三)frame_animation.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/d1" android:duration="100"/>
<item android:drawable="@drawable/d2" android:duration="100"/>
<item android:drawable="@drawable/d3" android:duration="100"/>
<item android:drawable="@drawable/d4" android:duration="100"/>
<item android:drawable="@drawable/d5" android:duration="100"/>
<item android:drawable="@drawable/d6" android:duration="100"/>
<item android:drawable="@drawable/d7" android:duration="100"/>
<item android:drawable="@drawable/d8" android:duration="100"/>
<item android:drawable="@drawable/d9" android:duration="100"/>
<item android:drawable="@drawable/d10" android:duration="100"/>
</animation-list>
●實作練習:java code
public class MainActivity extends AppCompatActivity {
private ImageView img;
//在屬性要先設定動畫物件
private AnimationDrawable animate;
private TextView text;
//view可以設定背景圖片,比imageview省資源
private View viewlogo;
private TextView logoname, message;
//資源檔陣列
// getResources().obtainTypedArray()會回傳TypedArray
private TypedArray NBAlogos;
//陣列有多少張圖
private int logoNumber;
private Button go;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intiView();
initNBAlogos();
initAnimation();
}
private void intiView() {
img = findViewById(R.id.img);
text = findViewById(R.id.text);
viewlogo = findViewById(R.id.viewlogo);
logoname = findViewById(R.id.logoname);
message = findViewById(R.id.message);
go = findViewById(R.id.go);
}
//設定圖片陣列資源,設定view背景圖片
private void initNBAlogos() {
//obtainTypedArray():
// Returns a TypedArray holding an array of the array values.
//取得nbalogo陣列資源
NBAlogos = getResources().obtainTypedArray(R.array.nbalogo);
//取得陣列長度
logoNumber = NBAlogos.length();
//getDrawable (int index)
//Retrieve the Drawable for the attribute at index.
//取得陣列第0項的圖設定給view
viewlogo.setBackground(NBAlogos.getDrawable(0));
}
private void initAnimation() {
//設定imageview
img.setBackgroundResource(R.drawable.frame_animation);
//取得imageview的背景給動畫物件animate
animate = (AnimationDrawable) img.getBackground();
}
public void click(View view) {
int id = view.getId();
switch (id) {
case R.id.start:
animate.start(); //開始動畫
break;
case R.id.stop:
animate.stop(); //結束動畫
break;
case R.id.secs:
animation5seconds();
break;
}
}
//Handler 處理代辦事項
//Each Handler instance is associated with
// a single thread and that thread's message queue.
//There are two main uses for a Handler:
// (1) to schedule messages and runnables to
// be executed as some point in the future; and
// (2) to enqueue an action to be performed
// on a different thread than your own.
private Handler handle = new Handler();
private void animation5seconds() {
int delaytime = 5000;
animate.start();
Runnable task = new Task(); //多型的作法
boolean result = handle.postDelayed(task, delaytime);
text.setText(result ? "交付成功" : "交付失敗");
}
//這邊使用內部類別來implement Runnable,
// 因為如果另外建立一個外部類別,將無法使用private變數
//The Runnable interface should be implemented by
// any class whose instances are intended to be executed by a thread.
// The class must define a method of no arguments called run.
private class Task implements Runnable {
@Override
public void run() {
animate.stop();
text.setText("時間到");
}
}
private Runnable start = new startchange();
private Runnable stop = new stopchange();
public void go(View view) {
handle.post(start); //立刻執行任務
handle.postDelayed(stop, 3000);
go.setEnabled(false);
}
private class startchange implements Runnable {
@Override
public void run() {
//取得字串陣列資源
String[] name = getResources().getStringArray(R.array.nabname);
//隨機選取0~12的數字
int index = (int) (Math.random() * logoNumber);
//換圖
viewlogo.setBackground(NBAlogos.getDrawable(index));
//換隊名
logoname.setText(name[index]);
//this是指 startchange這個物件本身
boolean result = handle.postDelayed(this, 100);
message.setText(result ? "設定成功" : "設定失敗");
}
}
private class stopchange implements Runnable {
@Override
public void run() {
//取消任務start
handle.removeCallbacks(start);
go.setEnabled(true);
//不可以放在go()裡面,
// 因為go以後要等三秒後才會顯示時間到,
// 如果放在go()裡面會被設定成功覆蓋掉
message.setText("時間到");
}
}
}
*程式參考(gitHub):Animation動畫的實作與練習
文章標籤
全站熱搜