Android開發之圖形映像與動畫(四)AnimationListener簡介

來源:互聯網
上載者:User

就像Button控制項有監聽器一樣,動畫效果也有監聽器,只需要實現AnimationListener就可以實現對動畫效果的監聽,其中需要重載三個函數,就是下面的這幾個函數: 複製代碼 代碼如下:private class MyListenr implements AnimationListener{
@Override
public void onAnimationEnd(Animation arg0) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationStart(Animation arg0) {
// TODO Auto-generated method stub
}

}

其中第一個函數的意思是在動畫執行完之後需要開發人員做什麼,第二個函數的意思是在動畫重複執行的過程中應該做什麼,第三個函數的意思是當動畫開始執行時有什麼動作發生。

下面我實現了一個例子,點擊刪除按鈕,圖片慢慢淡去,並最終刪除,當點擊添加按鈕時向viewGroup中添加一個imageview,實現的如下:

具體的實現代碼如下: 複製代碼 代碼如下:public class MainActivity extends Activity {
private Button button;
private Button button2;
private ImageView imageView;
private ViewGroup viewGroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.button_add);
button2=(Button)findViewById(R.id.button_delete);
imageView=(ImageView)findViewById(R.id.imageView1);
viewGroup=(ViewGroup)findViewById(R.id.viewGroup);
button.setOnClickListener(new Mybutton());
button2.setOnClickListener(new Mybutton());
}
private class Mybutton implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button_add:
Add();
break;
case R.id.button_delete:
Delete();
break;
default:
break;
}
}
}

public void Add() {
AlphaAnimation alphaAnimation=new AlphaAnimation(0.0f, 1.0f);
alphaAnimation.setDuration(2000);
alphaAnimation.setStartOffset(500);
ImageView imageViewAdd=new ImageView(MainActivity.this);
imageViewAdd.setImageResource(R.drawable.ic_launcher);
viewGroup.addView(imageViewAdd);
// viewGroup.addView(imageViewAdd, new LayoutParams(
// LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
imageViewAdd.startAnimation(alphaAnimation);
}
public void Delete() {
AlphaAnimation alphaAnimation=new AlphaAnimation(1.0f, 0.0f);
alphaAnimation.setDuration(2000);
alphaAnimation.setStartOffset(500);
imageView.startAnimation(alphaAnimation);
alphaAnimation.setAnimationListener(new MyListenr());
}

private class MyListenr implements AnimationListener{
@Override
public void onAnimationEnd(Animation arg0) {
// TODO Auto-generated method stub
viewGroup.removeView(imageView);
Log.d("BruceZhang", "Animation End!");
}
@Override
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub
Log.d("BruceZhang", "Animation Repeat!");
}
@Override
public void onAnimationStart(Animation arg0) {
// TODO Auto-generated method stub
Log.d("BruceZhang", "Animation Start!");
}

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}

此執行個體的布局檔案如下,注意,需要在根標籤下給出viewGroup的id: 複製代碼 代碼如下:<AbsoluteLayout 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=".MainActivity"
android:id="@+id/viewGroup"
>
<Button
android:id="@+id/button_add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_x="0dp"
android:layout_y="367dp"
android:text="添加圖片" />
<Button
android:id="@+id/button_delete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_x="0dp"
android:layout_y="410dp"
android:text="刪除圖片" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="153dp"
android:layout_y="155dp"
android:src="@drawable/ic_launcher" />
</AbsoluteLayout>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.