android開發之Animation(五)
阿新 • • 發佈:2018-02-09
protected html err nim package 設置圖 move 函數 star
MainActivity.java:
package com.example.animationlistener;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ViewGroup viewGroup = null;
private Button addButton = null;
private Button removeButton = null;
private ImageView imageView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addButton = (Button)findViewById(R.id.addButton);
addButton.setOnClickListener(new AddButtonListener());
removeButton = (Button)findViewById(R.id.removeButton);
removeButton.setOnClickListener(new RemoveButtonListener());
viewGroup = (ViewGroup)findViewById(R.id.layout_id);
imageView = (ImageView)findViewById(R.id.myImage);
}
//刪除imageView控件
class RemoveButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//創建一個淡出效果的動畫對象
AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);
alphaAnimation.setDuration(2000);
//給Animation對象設置監聽器
alphaAnimation.setAnimationListener(new RemoveAnimationListener());
imageView.startAnimation(alphaAnimation);
}
}
//加入ImageView控件
class AddButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
alphaAnimation.setDuration(2000);
//在當前Activity中創建一個ImageView控件
ImageView addImageView = new ImageView(MainActivity.this);
//給ImageView設置ID
addImageView.setId(R.id.myImage);
//給ImageView控件設置圖片資源
addImageView.setImageResource(R.drawable.ic_launcher);
//viewGroup表示的是main.xml中的整個布局
//將imageView加入到布局中
viewGroup.addView(addImageView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
//alphaAnimation.setAnimationListener(new addAnimationListener());
imageView = addImageView;
addImageView.startAnimation(alphaAnimation);
}
}
//AnimationLinstener主要是在Animation動畫效果的開始和結束等周期時,會調用當中的相關函數
class RemoveAnimationListener ?implements AnimationListener{
//當Animation效果結束後調用此方法
@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
System.out.println("Animation end");
//將ImageView在布局中刪除
viewGroup.removeView(imageView);
}
//當animation效果反復運行時調用此方法
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
System.out.println("Animation repeat");
}
//當animation效果開始運行時調用此方法
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
System.out.println("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.main, menu);
return true;
}
}
主布局文件main.xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? xmlns:tools="http://schemas.android.com/tools"
? ? android:id="@+id/layout_id"
? ? android:layout_width="match_parent"
? ? android:layout_height="match_parent"
? ? android:paddingBottom="@dimen/activity_vertical_margin"
? ? android:paddingLeft="@dimen/activity_horizontal_margin"
? ? android:paddingRight="@dimen/activity_horizontal_margin"
? ? android:paddingTop="@dimen/activity_vertical_margin"
? ? tools:context=".MainActivity" >
? ? <TextView
? ? ? ? android:id="@+id/myText"
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:text="@string/hello_world" />
? ??
? ?<ImageView?
? ? ? ? ? ? android:id="@+id/myImage"
? ? ? ? ? ? android:layout_width="fill_parent"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:src="@drawable/ic_launcher"
? ? ? ? ? ? android:layout_below="@id/myText"
? ? ? ? ? ? />
? ??
? ? <Button?
? ? ? ? android:id="@+id/addButton"
? ? ? ? android:layout_width="fill_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:layout_below="@id/myImage"
? ? ? ? android:text="add image"
? ? ? ??
? ? ? ? />
? ??
? ? ?<Button?
? ? ? ? android:id="@+id/removeButton"
? ? ? ? android:layout_width="fill_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:layout_below="@id/addButton"
? ? ? ? android:text="remove image"
? ? ? ? />
? ??
</RelativeLayout>
android開發之Animation的使用(五)
本博文主要講述的是Animation中的AnimationLisenter的用法,以及此類的一些生命周期函數的調用,代碼實比例如以下:MainActivity.java:
package com.example.animationlistener;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ViewGroup viewGroup = null;
private Button addButton = null;
private Button removeButton = null;
private ImageView imageView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addButton = (Button)findViewById(R.id.addButton);
addButton.setOnClickListener(new AddButtonListener());
removeButton = (Button)findViewById(R.id.removeButton);
removeButton.setOnClickListener(new RemoveButtonListener());
viewGroup = (ViewGroup)findViewById(R.id.layout_id);
imageView = (ImageView)findViewById(R.id.myImage);
}
//刪除imageView控件
class RemoveButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//創建一個淡出效果的動畫對象
AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);
alphaAnimation.setDuration(2000);
//給Animation對象設置監聽器
alphaAnimation.setAnimationListener(new RemoveAnimationListener());
imageView.startAnimation(alphaAnimation);
}
}
//加入ImageView控件
class AddButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
alphaAnimation.setDuration(2000);
//在當前Activity中創建一個ImageView控件
ImageView addImageView = new ImageView(MainActivity.this);
//給ImageView設置ID
addImageView.setId(R.id.myImage);
//給ImageView控件設置圖片資源
addImageView.setImageResource(R.drawable.ic_launcher);
//viewGroup表示的是main.xml中的整個布局
//將imageView加入到布局中
viewGroup.addView(addImageView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
//alphaAnimation.setAnimationListener(new addAnimationListener());
imageView = addImageView;
addImageView.startAnimation(alphaAnimation);
}
}
//AnimationLinstener主要是在Animation動畫效果的開始和結束等周期時,會調用當中的相關函數
class RemoveAnimationListener ?implements AnimationListener{
//當Animation效果結束後調用此方法
@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
System.out.println("Animation end");
//將ImageView在布局中刪除
viewGroup.removeView(imageView);
}
//當animation效果反復運行時調用此方法
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
System.out.println("Animation repeat");
}
//當animation效果開始運行時調用此方法
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
System.out.println("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.main, menu);
return true;
}
}
主布局文件main.xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? xmlns:tools="http://schemas.android.com/tools"
? ? android:id="@+id/layout_id"
? ? android:layout_width="match_parent"
? ? android:layout_height="match_parent"
? ? android:paddingBottom="@dimen/activity_vertical_margin"
? ? android:paddingLeft="@dimen/activity_horizontal_margin"
? ? android:paddingRight="@dimen/activity_horizontal_margin"
? ? android:paddingTop="@dimen/activity_vertical_margin"
? ? tools:context=".MainActivity" >
? ? <TextView
? ? ? ? android:id="@+id/myText"
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:text="@string/hello_world" />
? ??
? ?<ImageView?
? ? ? ? ? ? android:id="@+id/myImage"
? ? ? ? ? ? android:layout_width="fill_parent"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:src="@drawable/ic_launcher"
? ? ? ? ? ? android:layout_below="@id/myText"
? ? ? ? ? ? />
? ??
? ? <Button?
? ? ? ? android:id="@+id/addButton"
? ? ? ? android:layout_width="fill_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:layout_below="@id/myImage"
? ? ? ? android:text="add image"
? ? ? ??
? ? ? ? />
? ??
? ? ?<Button?
? ? ? ? android:id="@+id/removeButton"
? ? ? ? android:layout_width="fill_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:layout_below="@id/addButton"
? ? ? ? android:text="remove image"
? ? ? ? />
? ??
</RelativeLayout>
android開發之Animation(五)