属性动画在自定义控件中的使用

1.概述

Android为开发者提供了几种动画类型:View Animation 、Drawable Animation 、Property Animation 。本文要说的就是我们在自己做控件的时候,使用Property Animation 来实现预期的动画效果。

2.Material Button

2.1效果图:

2.2动画效果分析:

a.圆形扩散
b.圆形颜色变淡

2.3具体实现

继承button类,完善初始化方法。在我们点击的时候,圆是从点击位置开始往外扩散,所以需要获取按钮的点击事件。

	public boolean onTouchEvent(MotionEvent event) {
<span style="white-space:pre">		</span>switch (event.getAction()) {
<span style="white-space:pre">		</span>case MotionEvent.ACTION_DOWN:
<span style="white-space:pre">			</span>X= event.getX() ;//获取点击X
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的沙漏动画定义件的实现过程: 1. 创建一个自定义件类SandClockView,继承自View。 2. 在构造函数初始化画笔Paint和属性动画AnimatorSet,用于制沙漏动画的进度。 3. 重写onMeasure方法,确保件的宽高比为2:3,并且高度不超过屏幕高度的一半。 4. 重写onDraw方法,在画布上绘制两个三角形和一条线段,形成沙漏的形状。 5. 创建方法startAnimation和stopAnimation,分别用于开始和停止沙漏动画。在startAnimation方法,创建两个ValueAnimator对象,分别制沙漏上下部分的动画进度,然后将它们添加到AnimatorSet,设置动画时间和循环次数,并启动动画。 6. 在onDetachedFromWindow方法停止动画。 以下是完整代码实现: ``` public class SandClockView extends View { private Paint mPaint; private AnimatorSet mAnimatorSet; private float mTopLeftX; private float mTopLeftY; private float mBottomLeftX; private float mBottomLeftY; private float mRightX; private float mTopY; private float mBottomY; private int mWidth; private int mHeight; public SandClockView(Context context) { super(context); init(); } public SandClockView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public SandClockView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mPaint.setColor(Color.BLACK); mPaint.setStrokeWidth(5); mAnimatorSet = new AnimatorSet(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int width = MeasureSpec.getSize(widthMeasureSpec); int height = MeasureSpec.getSize(heightMeasureSpec); // 保证宽高比为2:3 if (width * 3 > height * 2) { width = height * 2 / 3; } else { height = width * 3 / 2; } // 保证高度不超过屏幕高度的一半 int screenHeight = getResources().getDisplayMetrics().heightPixels; if (height > screenHeight / 2) { height = screenHeight / 2; width = height * 2 / 3; } mWidth = width; mHeight = height; setMeasuredDimension(width, height); } @Override protected void onDraw(Canvas canvas) { float triangleWidth = mWidth / 3f; float triangleHeight = mHeight / 2f; mTopLeftX = (mWidth - triangleWidth) / 2f; mTopLeftY = (mHeight - triangleHeight) / 2f; mBottomLeftX = mTopLeftX; mBottomLeftY = mTopLeftY + triangleHeight; mRightX = mTopLeftX + triangleWidth; mTopY = mTopLeftY; mBottomY = mBottomLeftY; Path path = new Path(); path.moveTo(mTopLeftX, mTopLeftY); path.lineTo(mRightX, mTopY); path.lineTo(mBottomLeftX, mBottomLeftY); path.lineTo(mTopLeftX, mTopLeftY); path.lineTo(mRightX, mBottomY); path.lineTo(mBottomLeftX, mBottomLeftY); canvas.drawPath(path, mPaint); } public void startAnimation() { ValueAnimator topAnimator = ValueAnimator.ofFloat(mTopY, mBottomY); topAnimator.setInterpolator(new LinearInterpolator()); topAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { mTopY = (float) animation.getAnimatedValue(); invalidate(); } }); ValueAnimator bottomAnimator = ValueAnimator.ofFloat(mBottomY, mTopY); bottomAnimator.setInterpolator(new LinearInterpolator()); bottomAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { mBottomY = (float) animation.getAnimatedValue(); invalidate(); } }); mAnimatorSet.play(topAnimator).with(bottomAnimator); mAnimatorSet.setDuration(1000); mAnimatorSet.setStartDelay(500); mAnimatorSet.setRepeatCount(ValueAnimator.INFINITE); mAnimatorSet.start(); } public void stopAnimation() { mAnimatorSet.cancel(); } @Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); stopAnimation(); } } ``` 使用时,可以在布局文件添加一个SandClockView件,并在代码调用startAnimation方法开始动画,调用stopAnimation方法停止动画。例如: ``` SandClockView sandClockView = findViewById(R.id.sand_clock_view); sandClockView.startAnimation(); ``` 注意:以上代码只是一个简单的实现,实际应用可能需要根据具体需求进行修改和优化。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值