Android 视图标签相关学习
Android 视图标签相关学习
视图动画类(View Animation)
帧动画(Frame Animation)
一张一张的图片,一帧一帧的组成的动画
帧动画有两种实现方式:xml,java
最常用的是xml文件实现
在主布局文件中添加一个ImageView和两个Button
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/run"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="run!run!change"/>
<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="stop!stop!"/>
在drawable中定义两个xml
<!-- 这里使用自带的ic_launcher_background和定义的xml -->
<!--不贴代码了-->
在drawable 文件夹中新建 animation-list 标签的xml文件
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<!-- android:oneshot="false" 表示播放多次 -->
<item
android:drawable="@drawable/ic_launcher_background"
android:duration="50" />
<item
android:drawable="@drawable/toast_shape_bg_toast"
android:duration="50" />
</animation-list>
主活动类修改
@SuppressLint("MissingInflatedId")
public class MainActivity extends AppCompatActivity {
@SuppressLint("ResourceType")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AnimationDrawable drawable = (AnimationDrawable)getResources().getDrawable(R.drawable.change_anim);
ImageView img = findViewById(R.id.imageView);
img.setImageDrawable(drawable);
//播放动画
findViewById(R.id.run).setOnClickListener(v -> {
if(drawable != null && !drawable.isRunning()){
drawable.start();
}
});
//停止动画
findViewById(R.id.stop).setOnClickListener(v -> {
if(drawable != null && drawable.isRunning()){
drawable.stop();
}
});
}
}
代码实现
@SuppressLint("MissingInflatedId")
public class MainActivity extends AppCompatActivity {
private AnimationDrawable drawable;
private ImageView img;
@SuppressLint("ResourceType")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = findViewById(R.id.imageView);
createAnim();
//播放动画
findViewById(R.id.run).setOnClickListener(v -> {
if (drawable != null && !drawable.isRunning()) {
drawable.start();
}
});
//停止动画
findViewById(R.id.stop).setOnClickListener(v -> {
if (drawable != null && drawable.isRunning()) {
drawable.stop();
}
});
}
private void createAnim() {
drawable = new AnimationDrawable();
int id = getResources().getIdentifier("ic_launcher_background", "drawable", getPackageName());
int id2 = getResources().getIdentifier("toast_shape_bg_toast", "drawable", getPackageName());
//根据id获取到Drawable
Drawable dra = getResources().getDrawable(id);
Drawable dra2 = getResources().getDrawable(id2);
drawable.addFrame(dra, 50);
drawable.addFrame(dra2, 50);
//是否只运行一次
drawable.setOneShot(false);
//给imageView设置动画
img.setImageDrawable(drawable);
}
}
补间动画(Tween Animation)
补间动画包含:
----------------AnimationSet
位移translate----TranslateAnimation
透明度alpha----AlphaAnimation
缩放scale----ScaleAnimation
旋转rotate----RotateAnimation
五类都继承Animation抽象类
实现方式:xml和java
公共属性
android:duration 动画持续时长,单位是毫秒
android:fillAfter 动画结束后应用动画转变(是否停留在最后一帧) 默认值是false
android:fillBefore 动画开始之前应用动画转变(是否直接从动画转变的第一帧开始)默认值为true
android:fillEnaabled 如果为true则应用fillBefore的值,否则忽略fillBefore的值 默认值为false
android:interpolator 动画过渡用的插值器
android:repeatCount 动画重复多少次 默认值为0
android:repeatMode repeatCount大于0时动画结束时的重复模式,restart 是从开始的地方重新播 放,reverse是从结束的地方重新播放 默认值是restart
android:startOffset 动画开始前延迟 单位:毫秒
android:zAdjustment 允许调整动画在运行期间的z轴顺序 默认值为normal
android:detackWallpaper 窗口动画特殊属性,如果这个窗口在壁纸之上,请不要使用它为墙纸设置动画
1.set
这里的set标签就是一个容易类,里面可以放标签集合,去实现动画效果
组合动画
特有属性
android:shareInterpolator="boolean" //如果为true,所有动画都使用AnimationSet的插值器。否则每个动画使用自己的插值器
在组合动画中,部分AnimationSet的属性添加到AnimationSet会影响自己,有的属性则下推到子对象去使用,还有的属性会被忽略掉
duration, repeatMode, fillBefore, fillAfter:当这些属性在AnimationSet对象设置时,将会下推到子对象。
repeatCount, fillEnabled:AnimationSet会忽略掉这些属性
startOffset, shareInterpolator:这些属性将会应用到AnimationSet本身
2.translate
位置变换
特有属性
android:fromXDelta 动画改变之前的X轴坐标
android:fromYDelta 动画改变之前的Y轴坐标
android:toXDelta 动画改变之后的X轴坐标
android:toYDelta 动画改变之后的Y轴坐标
fromXType、 fromYType、toXType、toYType不能在XML中使用,使用了会报错误 error: attribute android:xx not found.
fromXType//指定应该如何去解释 fromXDelta
fromYType//指定应该如何去解释 fromYDelta
toXType//指定应该如何去解释 toXDelta
toYType//指定应该如何去解释 toYDelta
xml实现,定义一个translate.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="50%"
android:toYDelta="50%"
android:duration="1000">
</translate>
若加载冒红问题,可重启下开发工具,这里我今天就遇到了这个问题(可能是缓存的问题造成的),明明写的都是对的,但是R.anim.translate,就是找不到,在此记录下。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = findViewById(R.id.imageView);
Animation animation = AnimationUtils.loadAnimation(this, R.anim.translate);
findViewById(R.id.run).setOnClickListener(v -> {
if(animation != null){
img.startAnimation(animation);
}
});
}
java 代码实现
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = findViewById(R.id.imageView);
TranslateAnimation animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 1f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 1f);
animation.setDuration(2000);
animation.setInterpolator(this, android.R.anim.anticipate_interpolator);
findViewById(R.id.run).setOnClickListener(v -> {
if(animation != null){
img.startAnimation(animation);
}
});
}
3.alpha
渐变透明度
特有属性
android:fromAlpha 动画开始透明度,最小值0.0表示全透明,最大值1.0表示完全不透明
android:toAlpha 动画结束透明度,最小值0.0表示全透明,最大值1.0表示完全不透明
xml实现,定义一个alpha.xml
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="1"
android:toAlpha="0"
android:duration="1000"/>
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = findViewById(R.id.imageView);
Animation animation = AnimationUtils.loadAnimation(this, R.anim.alpha);
findViewById(R.id.run).setOnClickListener(v -> {
if(animation != null){
img.startAnimation(animation);
}
});
}
java 代码实现
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = findViewById(R.id.imageView);
AlphaAnimation animation = new AlphaAnimation(1.0f, 0.0f);
animation.setDuration(1000);
findViewById(R.id.run).setOnClickListener(v -> {
if(animation != null){
img.startAnimation(animation);
}
});
}
4.scale
动画缩放
ScaleAnimation是控制对象缩放的动画,你可以指定缩放的中心点。
特有属性
android:fromXScale="float" //动画开始前横轴缩放因子
android:fromYScale="float" //动画开始前纵轴缩放因子
android:toXScale="float" //动画结束时横轴缩放因子
android:toYScale="float" //动画结束时纵轴缩放因子
android:pivotX="float" //定义缩放点的X轴坐标,0是左上角。这个值可以是百分比(1.0是100)或者如果pivotXType 是ABSOLUTE这个值可以是一个绝对数字
android:pivotY="float" //定义旋转点的Y轴坐标,0是左上角。这个值可以是百分比( 1.0是100)或者如果 pivotXType 是 ABSOLUTE这个值可以是一个绝对数字
pivotXType和pivotYType不能在XML中使用,使用了会报错误 error: attribute android:xx not found.
pivotXType //指定应该如何去解释 pivotX
pivotYType //指定应该如何去解释 pivotY
xml实现,定义一个scale.xml
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="1"
android:fromYScale="1"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="2"
android:toYScale="2"
android:duration="1000">
</scale>
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = findViewById(R.id.imageView);
Animation animation = AnimationUtils.loadAnimation(this, R.anim.scale);
findViewById(R.id.run).setOnClickListener(v -> {
if(animation != null){
img.startAnimation(animation);
}
});
}
java 代码实现
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = findViewById(R.id.imageView);
ScaleAnimation animation = new ScaleAnimation(1f, 2f, 1f, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation.setDuration(2000);
animation.setInterpolator(this, android.R.anim.linear_interpolator);
findViewById(R.id.run).setOnClickListener(v -> {
if(animation != null){
img.startAnimation(animation);
}
});
}
5.rotate
动画旋转
特有属性
android:fromDegrees 动画开始前的角度
android:toDegrees 动画结束时的角度
android:pivotX 旋转点的X轴坐标,0是左上角。这个值可以是百分比(旋转坐标在自身x轴的百分比 )或者如果pivotXType 是ABSOLUTE这个值可以是一个绝对数字
android:pivotY 旋转点的Y轴坐标,0是左上角。这个值可以是百分比( 旋转坐标在自身y轴的百分比 )或者如果 pivotXType 是 ABSOLUTE这个值可以是一个绝对数字
pivotXType和pivotYType不能在XML中使用,使用了会报错误 error: attribute android:xx not found.
这两个分别是对pivotX和pivotY的类型做解释的
xml实现,定义一个rotate.xml
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="720"
android:fromDegrees="0"
android:duration="1000"
android:interpolator="@android:anim/anticipate_interpolator">
</rotate>
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = findViewById(R.id.imageView);
Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate);
findViewById(R.id.run).setOnClickListener(v -> {
if(animation != null){
img.startAnimation(animation);
}
});
}
这里需要注意的,旋转的话,是旋转了ImageView整个控件,所以图片需要在这个ImageView中设置下,不然学习的时候,这里出不来旋转的效果
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"/>
java 代码实现
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = findViewById(R.id.imageView);
RotateAnimation animation = new RotateAnimation(0, 720, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation.setDuration(1000);
animation.setInterpolator(this, android.R.anim.anticipate_interpolator);
findViewById(R.id.run).setOnClickListener(v -> {
if(animation != null){
img.startAnimation(animation);
}
});
}
三个值分别是:
Animation.ABSOLUTE 指定尺寸(dimension)是一个绝对像素数量。使用之后pivotX 或pivotY 的值都是绝对像素数量
Animation.RELATIVE_TO_SELF 指定尺寸(dimension)是一个浮点数,这个浮点数用来乘动画对象的宽或高。例如使用之后动画最终的x轴 =(pivotX 的值) X (动画对象的宽 )
Animation.RELATIVE_TO_PARENT 指定尺寸(dimension)是一个浮点数,这个浮点数用来乘动画父对象的宽或高。例如使用之后动画最终的x轴 =(pivotX 的值) X ( 动画父对象的宽 )
属性动画(Property Animation)
博客内容学习参考https://www.jianshu.com/p/250babcf9568
本文来自博客园,作者:阿寳同學Zybao,转载请注明原文链接:https://www.cnblogs.com/zybao/p/16914432.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步