Android 多个animation同时执行

假如,我们想要一个控件在变大的同时向左移动,我们可以通过以下几个步骤来实现:


1、在res文件夹下新建一个anim文件夹(存放动画xml文件的文件夹一般都这么命名);

2、创建两个xml文件,假设命名为分别命名为scale_big.xml、trans_left.xml

scale_big.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:shareInterpolator="false"     
     android:fillAfter="true">

    <scale
        android:duration="150"
        android:fromXScale="1"
        android:fromYScale="1"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1.1"
        android:toYScale="1.1"        
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        />
  <!--  
  fromXScale[float]  fromYScale[float]  为动画起始时,X、Y坐标上的伸缩尺寸;
  					0.0表示收缩到没有 ;1.0表示正常无伸缩.
  					值小于1.0表示收缩. 值大于1.0表示放大
  
  toXScale [float]   toYScale[float]    为动画结束时,X、Y坐标上的伸缩尺寸
  
  pivotX[float]        pivotY[float]   为动画相对于物件的X、Y坐标的开始位置
  					属性值说明:从0%-100%中取值,50%为
  					物件的X或Y方向坐标上的中点位置
  -->
</set>

trans_left.xml

<?xml version="1.0" encoding="utf-8"?>
   <translate
        xmlns:android="http://schemas.android.com/apk/res/android"
	    android:fromXDelta="0"
	    android:toXDelta="-10"
	    android:duration="150"
	    android:interpolator="@android:anim/decelerate_interpolator"
		>  
   </translate> 

3、在java代码中加载这些动画(transLeftAnim同理,略去)

Animation scaleBigAnim = AnimationUtils.loadAnimation(MainActivity.this, R.anim.scale_big);


4、创建AnimationSet

		animationSet = new AnimationSet(true);
		animationSet.addAnimation(scaleBigAnim);
		animationSet.addAnimation(transLeftAnim);

5、执行该动画

<span style="white-space:pre">		</span>textView.startAnimation(animationSet);


就这样,我们可以实现在变大的时候并同时左移的动画效果,关键是明白这个原理,就可以实现rotate、scale、translate或者alpha等同时变换的组合动画。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android中,可以使用SwipeRefreshLayout控件来实现下拉刷新的功能,并且可以添加一个旋转的遮罩来提示用户正在刷新。下面是实现方法: 1. 在XML文件中添加SwipeRefreshLayout控件: ``` <androidx.swiperefreshlayout.widget.SwipeRefreshLayout android:id="@+id/swipe_refresh_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 添加需要刷新的布局内容 --> </androidx.swiperefreshlayout.widget.SwipeRefreshLayout> ``` 2. 在Java代码中获取SwipeRefreshLayout控件对象,并设置刷新监听器: ``` SwipeRefreshLayout swipeRefreshLayout = findViewById(R.id.swipe_refresh_layout); swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { // 执行刷新操作 } }); ``` 3. 设置遮罩的颜色: ``` swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_bright); ``` 4. 在布局中添加一个继承自ProgressBar的自定义控件,并将它添加到SwipeRefreshLayout中: ``` <com.example.myapp.LoadingProgressBar android:id="@+id/loading_progress_bar" android:layout_width="50dp" android:layout_height="50dp" android:layout_gravity="center"/> swipeRefreshLayout.addView(loadingProgressBar); ``` 5. 自定义LoadingProgressBar控件,继承自ProgressBar,并实现旋转动画: ``` public class LoadingProgressBar extends ProgressBar { public LoadingProgressBar(Context context) { super(context); init(); } public LoadingProgressBar(Context context, @Nullable AttributeSet attrs) { super(context, attrs); init(); } public LoadingProgressBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { setIndeterminate(true); getIndeterminateDrawable().setColorFilter( ContextCompat.getColor(getContext(), android.R.color.white), PorterDuff.Mode.SRC_IN); } @Override public void setVisibility(int visibility) { super.setVisibility(visibility); if (visibility == VISIBLE) { startAnimation(); } else { stopAnimation(); } } private void startAnimation() { RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); rotateAnimation.setDuration(1000); rotateAnimation.setInterpolator(new LinearInterpolator()); rotateAnimation.setRepeatCount(Animation.INFINITE); startAnimation(rotateAnimation); } private void stopAnimation() { clearAnimation(); } } ``` 以上就是添加一个刷新时旋转的遮罩的方法,你可以根据具体需求进行更多的设置和调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值