ActionBar의 Refresh 버튼 애니메이션

Activity에서의 비동기처리와 애니메이션

public classMainActivity extends SherlockActivity {
	private MenuItem refreshMenu;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		setContentView(R.layout.레이아웃);
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getSupportMenuInflater();
		inflater.inflate(R.menu.sample_refresh_menu, menu);
		refreshMenu = menu.findItem(R.id.refresh_menu);
		return true;
	}
	class RefreshMailAsyncTask extends AsyncTask<Void, Integer, Void> {
		@Override
		protected Void doInBackground(Void... v) {
			MainActivity.this.runOnUiThread(new Runnable() {
				@Override
				public void run() {
					refreshMenu.setVisible(false);
					setProgressBarIndeterminateVisibility(Boolean.TRUE);
				}
			});
			// TODO 비동기처리
		}
		@Override
		protected void onCancelled() {
			super.onCancelled();
			MainActivity.this.runOnUiThread(new Runnable() {
				@Override
				public void run() {
					setProgressBarIndeterminateVisibility(Boolean.FALSE);
					refreshMenu.setVisible(true);
				}
			});
		}
		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			MainActivity.this.runOnUiThread(new Runnable() {
				@Override
				public void run() {
					setProgressBarIndeterminateVisibility(Boolean.FALSE);
					refreshMenu.setVisible(true);
				}
			});
		}
	}

메뉴 (sample_refresh_menu.xml)

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/refresh_mail"
        android:icon="@android:drawable/ic_popup_sync"
        android:showAsAction="ifRoom"
        android:title="@string/refresh_mail_list"/>
</menu>

애니메이션 예제

http://pixelstech.net/article/1351875079_Create_animated_refresh_button_in_Android]]>

Related Posts