用Android 動畫 演示冒泡排序

之前面試遇到的一道機試題,當時時間不夠沒有調出來,有時間把它整了一下

 

代碼

public class MainActivity extends ActionBarActivity implements OnClickListener {

 private ArrayList<TextView> allViews;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  initView();
 }

 private void initView() {
  allViews = new ArrayList<TextView>();
  allViews.add((TextView) findViewById(R.id.a));
  allViews.add((TextView) findViewById(R.id.b));
  allViews.add((TextView) findViewById(R.id.c));
  allViews.add((TextView) findViewById(R.id.d));
  allViews.add((TextView) findViewById(R.id.e));

  findViewById(R.id.f).setOnClickListener(this);
  findViewById(R.id.g).setOnClickListener(this);
 }

 // 開始排序(運行在子線程,需要暫停)
 private void starSort(final ArrayList<TextView> allViews) {
  new Thread(new Runnable() {
   @Override
   public void run() {
    // 冒泡排序
    maoPao(allViews);
    // 這裏子線程 必須先Looper.prepare(); 才能toast
    Looper.prepare();
    Toast.makeText(MainActivity.this, "排序完成", 0).show();
    Looper.loop();
   }
  }).start();
 }

 // 冒泡排序
 private void maoPao(final ArrayList<TextView> allViews) {
  for (int i = 0; i < allViews.size() - 1; i++) {
   for (int j = 0; j < allViews.size() - 1 - i; j++) {
    if (getTextNum(allViews.get(j)) < getTextNum(allViews
      .get(j + 1))) {
     // 開始動畫
     startMove(MainActivity.this, allViews.get(j),
       allViews.get(j + 1));
     // 交換位置
     TextView tempView = allViews.get(j + 1);
     // 先移除 一個
     allViews.remove(j + 1);
     // 在對應位置添加移除的達到交換位置(這裏要注意越界)
     allViews.add(
       j == allViews.size() ? allViews.size() - 1 : j,
       tempView);
     try {
      // 休眠3秒
      Thread.sleep(3000);
     } catch (InterruptedException e) {
      e.printStackTrace();
     }
    }
   }
  }
 }

 // 開始動畫
 private void startMove(Activity a, final TextView textView,
   final TextView textView2) {
  final float t1Y = textView.getY();// 獲得view的縱座標
  final float t2Y = textView2.getY();
  final float dY = Math.abs(t1Y - t2Y);
  // 動畫要運行在主線程(操作了textview)
  a.runOnUiThread(new Runnable() {
   @Override
   public void run() {
    // 用值動畫 完成移動 ,作用對象、屬性名稱、(Y代表Y軸移動 後面2個代表 起點跟終點)
    ObjectAnimator.ofFloat(textView, "Y", t1Y, t1Y + dY)
    // 設置執行時間(1000ms)
      .setDuration(1000)
      // 開始動畫
      .start();
    ObjectAnimator.ofFloat(textView2, "Y", t2Y, t2Y - dY)
      .setDuration(1000).start();
   }
  });
 }

 // 獲取textView的數字
 private int getTextNum(TextView tx) {
  try {
   return Integer.parseInt(tx.getText().toString());
  } catch (NumberFormatException e) {
   e.printStackTrace();
  }
  return 0;
 }

 @Override
 public void onClick(View v) {
  switch (v.getId()) {
  case R.id.f:// 點擊開始
   starSort(allViews);
   break;
  case R.id.g:// 點擊重新
   setContentView(R.layout.activity_main);
   initView();
   break;
  }

 }

}

---佈局-----------------------------

 

<RelativeLayout 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"
    tools:context="com.example.testanimation.MainActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/a"
            android:layout_width="20dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:background="#fd2288"
            android:gravity="center"
            android:text="1" />

        <TextView
            android:id="@+id/b"
            android:layout_width="20dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:background="#fd2288"
            android:gravity="center"
            android:text="2" />

        <TextView
            android:id="@+id/c"
            android:layout_width="20dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:background="#fd2288"
            android:gravity="center"
            android:text="3" />

        <TextView
            android:id="@+id/d"
            android:layout_width="20dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:background="#fd2288"
            android:gravity="center"
            android:text="4" />

        <TextView
            android:id="@+id/e"
            android:layout_width="20dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:background="#fd2288"
            android:gravity="center"
            android:text="5" />

        <TextView
            android:id="@+id/f"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:background="#fd2288"
            android:text="開始" />

        <TextView
            android:id="@+id/g"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:background="#fd2288"
            android:text="重新" />
    </LinearLayout>

</RelativeLayout>

 

 

發佈了30 篇原創文章 · 獲贊 34 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章