android Animation * AnimationListener的使用方法)
时间:2022-07-03 00:55:57
AnimationListener听名字就知道是对Animation设置 * ,说简单点就是在Animation动画效果开始执行前,执行完毕和重复执行时可以触发 * ,从而执行对应的函数。
开发环境为android4.1.
AnimaitonListener的使用方法主要是在Animation上设置一个 * ,即采用Animation的方法成员setAnimationListener().其参数就是 * 的函数。
现在来说说本次实验的功能,主要有2个按钮,一个是增加图片的按钮,一个是删除图片的按钮,还有一个ImageView的控件,用来显示图片的。当增加图片的按钮按下时,图片会以无到全尺寸的尺寸大小变化出现,而删除按钮按下时,图片会从全尺寸到0尺寸逐渐退出,最后删除掉。
程序界面如下:
这里值得一提的是ViewGroup这个控件,感觉就是Layout控件一样,本次实验的图片控件ImageView里面的图片的增加和删除就是采用的ViewGrop中的addView()和removeView()方法。这2种方法里面传入的参数就是ImageView.
另外,Mars老师资料中在增加图片 * 函数中,重新定义了一个ImageView,重新把这个ImageView加入到ViewGroup中,这样会导致一个问题,那就是当我们把图片删除后且又重新加载后就删除不掉了,因为我们在删除的时候删的是布局文件中的ImageView,但是增加按钮增加的是另外一个ImageView,所以我们虽然删除掉了布局文件中的ImageView,但是屏幕上还是会显示图片的。因此解决的方法就是在增加按钮函数中直接使用布局文件中的ImageView,这样程序中可以一直增加图片和删除图片,且在屏幕中还能看到效果。
程序主要代码如下:
MainActivity.java:
package com.example.anim_5;
import android.app.Activity;
import android.os.Bundle;
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.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.ScaleAnimation;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
private Button add = null;
private Button delete = null;
private ViewGroup viewgroup = null;
private ImageView imageview = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
add = (Button)findViewById(R.id.add);
delete = (Button)findViewById(R.id.delete);
imageview = (ImageView)findViewById(R.id.image);
viewgroup = (ViewGroup)findViewById(R.id.main_layout);
add.setOnClickListener(new AddOnClickListener());
delete.setOnClickListener(new DeleteOnClickListener());
}
private class AddOnClickListener implements OnClickListener{
public void onClick(View v) {
// TODO Auto-generated method stub
//数字后面必须全部加f,否则报错
ScaleAnimation animation = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
animation.setStartOffset(500);
animation.setDuration(1000);
// ImageView image_add = new ImageView(MainActivity.this);
// image_add.setImageResource(R.drawable.london_olympic);
// viewgroup.addView(image_add, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
// image_add.setAnimation(animation);
//还是直接用布局文件中的ImageView比较好,否则加入的图片用后面的方法视觉上是删不掉的
//这里是采用setImageResource方法加载图片到ImageView控件上的。
imageview.setImageResource(R.drawable.london_olympic);
//用ViewGroup将ImageView加载到activity中
viewgroup.addView(imageview, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
//启动ImageView的Animation
imageview.startAnimation(animation);
}
}
private class DeleteOnClickListener implements OnClickListener{
public void onClick(View v) {
// TODO Auto-generated method stub
ScaleAnimation animation = new ScaleAnimation(1.0f, 0.0f, 1.0f, 0.0f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
animation.setStartOffset(500);
animation.setDuration(1000);
//设置AnimationListener
animation.setAnimationListener(new DeleteAnimationListener());
imageview.startAnimation(animation);
}
}
private class DeleteAnimationListener implements AnimationListener{
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
viewgroup.removeView(imageview);
}
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/delete"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Delete Image"
/>
<Button
android:id="@+id/add"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/delete"
android:text="Add Image" />
<ImageView
android:id= "@+id/image"
android:layout_width="wrap_content"
android:layout_height="310dip"
android:src="@drawable/london_olympic"
/>
</RelativeLayout>
总结:通过本次实验,可以了解到AnimationListener的基本使用方法。
作者:tornadomeet
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java类之间的关系图_动力节点Java学院整理
![](https://img.aspxhome.com/file/2023/9/68569_0s.jpg)
Java方法参数传递机制原理解析
![](https://img.aspxhome.com/file/2023/2/69672_0s.png)
TextView实现跑马灯效果 就这么简单!
![](https://img.aspxhome.com/file/2023/2/83542_0s.png)
spring cloud zuul增加header传输的操作
SpringBoot实现配置文件的替换
Swagger及knife4j的基本使用详解
Java Swing JProgressBar进度条的实现示例
![](https://img.aspxhome.com/file/2023/3/80893_0s.gif)
unity通过Mesh网格绘制图形球体
![](https://img.aspxhome.com/file/2023/3/82233_0s.jpg)
Java 阻塞队列和线程池原理分析
![](https://img.aspxhome.com/file/2023/3/81993_0s.png)
Java 迪杰斯特拉算法实现查找最短距离的实现
![](https://img.aspxhome.com/file/2023/6/86706_0s.png)
Spring Cloud微服务架构Sentinel数据双向同步
![](https://img.aspxhome.com/file/2023/7/79947_0s.png)
关于java入门与java开发环境配置详细教程
![](https://img.aspxhome.com/file/2023/8/59568_0s.png)
android studio3.0以上如何通过navicat访问SQLite数据库文件
![](https://img.aspxhome.com/file/2023/5/89555_0s.png)
JAVA如何定义构造函数过程解析
![](https://img.aspxhome.com/file/2023/7/58477_0s.png)
Android开发手册SeekBar拖动条使用实例
![](https://img.aspxhome.com/file/2023/7/91687_0s.png)
Java面向对象之抽象类,接口的那些事
![](https://img.aspxhome.com/file/2023/0/61530_0s.jpg)
Java多线程定时器Timer原理及实现
IDEA 中使用 Hudi的示例代码
![](https://img.aspxhome.com/file/2023/6/76236_0s.jpg)