Android自定义属性动画

当我们对一个View进行透明度变化的动画,我们可以这样写:

ValueAnimator fadeAnimator = ObjectAnimator.ofFloat(myView, View.ALPHA, 1.0f, 0.0f);
fadeAnimator.setInterpolator(PathInterpolatorCompat.create(0.33f, 0f, 0.66f, 1f));
fadeAnimator.setDuration(1120);
final AnimatorSet alertAnimator = new AnimatorSet();
alertAnimator.playTogether(fadeAnimator);
alertAnimator.start();

查看View.java的代码,可以看到View.ALPHA的常量

public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {@Overridepublic void setValue(View object, float value) {object.setAlpha(value);}@Overridepublic Float get(View object) {return object.getAlpha();}};

再看setAlpha()方法,如果alpha值变化了invalidate()方法刷新界面

public void setAlpha(@FloatRange(from=0.0, to=1.0) float alpha) {ensureTransformationInfo();if (mTransformationInfo.mAlpha != alpha) {mTransformationInfo.mAlpha = alpha;if (onSetAlpha((int) (alpha * 255))) {mPrivateFlags |= PFLAG_ALPHA_SET;// subclass is handling alpha - don't optimize rendering cache invalidationinvalidateParentCaches();invalidate(true);} else {mPrivateFlags &= ~PFLAG_ALPHA_SET;invalidateViewProperty(true, false);mRenderNode.setAlpha(getFinalAlpha());notifyViewAccessibilityStateChangedIfNeeded(AccessibilityEvent.CONTENT_CHANGE_TYPE_UNDEFINED);}}}

再看getAlpha()方法,返回view当前的alpha

public float getAlpha() {return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;}

所以一个自定义的动画,需要一个动画的属性常量,并实现setget方法,set方法需要判断当动画属性值变化的时候刷新当前View.

例如我们定义圆形波浪动画:

public final static Property<WaveView, Float> RADIUS =new Property<WaveView, Float>(Float.class, "radius") {@Overridepublic Float get(WaveView view) {return view.getRadius();}@Overridepublic void set(WaveView view, Float value) {view.setRadius(value);}};public WaveView setRadius(float radius) {final float oldRadius = mRadius;if (oldRadius != radius) {mRadius = radius;invalidate(mCenterX, mCenterY, oldRadius);if (radius > oldRadius) {invalidate(mCenterX, mCenterY, radius);}}return this;}public final float getRadius() {return mRadius;}

然后在可以Animator调用RADIUS这个属性做动画,例如:

Animator animator = ObjectAnimator.ofFloat(myView, WaveView.RADIUS, 122f, 192f);animator.setInterpolator(PathInterpolatorCompat.create(0.2f, 0f, 0.24f, 1f));animator.setDuration(1120);final ValueAnimator fadeAnimator = ObjectAnimator.ofFloat(myView, View.ALPHA, 1.0f, 0.0f);fadeAnimator.setInterpolator(PathInterpolatorCompat.create(0.33f, 0f, 0.66f, 1f));fadeAnimator.setDuration(1120);final AnimatorSet alertAnimator = new AnimatorSet();alertAnimator.playTogether(animator, fadeAnimator);alertAnimator.start();

完整的Demo例子:

package com.usetsai.myalarm;import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.util.Property;
import android.view.View;/*** Created by junliang on 16-7-6.*/
public class WaveView extends View {public final static Property<WaveView, Integer> FILL_COLOR =new Property<WaveView, Integer>(Integer.class, "fillColor") {@Overridepublic Integer get(WaveView view) {return view.getFillColor();}@Overridepublic void set(WaveView view, Integer value) {view.setFillColor(value);}};public final static Property<WaveView, Float> RADIUS =new Property<WaveView, Float>(Float.class, "radius") {@Overridepublic Float get(WaveView view) {return view.getRadius();}@Overridepublic void set(WaveView view, Float value) {view.setRadius(value);}};private final Paint mCirclePaint = new Paint();private float mCenterX;private float mCenterY;private float mRadius;private float mStrokeWidth;public WaveView(Context context) {this(context, null /* attrs */);}public WaveView(Context context, AttributeSet attrs) {this(context, attrs, 0 /* defStyleAttr */);}public WaveView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.WaveView, defStyleAttr, 0 /* defStyleRes */);mCenterX = a.getDimension(R.styleable.WaveView_centerX, 0.0f);mCenterY = a.getDimension(R.styleable.WaveView_centerY, 0.0f);mRadius = a.getDimension(R.styleable.WaveView_radius, 0.0f);mStrokeWidth = a.getDimension(R.styleable.WaveView_strokeWidth, 3f);int fillColor = a.getColor(R.styleable.WaveView_fillColor, Color.BLUE);int fillEndColor = a.getColor(R.styleable.WaveView_fillEndColor, -1);if (fillEndColor == -1) {mCirclePaint.setColor(fillColor);} else {LinearGradient sweepGradient = new LinearGradient(0f,a.getDimension(R.styleable.WaveView_gradientStart, 0f),0f, a.getDimension(R.styleable.WaveView_gradientEnd, 0f),fillColor, fillEndColor, Shader.TileMode.CLAMP);mCirclePaint.setShader(sweepGradient);}mCirclePaint.setAntiAlias(true);mCirclePaint.setStyle(Paint.Style.STROKE);mCirclePaint.setStrokeWidth(mStrokeWidth);a.recycle();}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);canvas.drawCircle(mCenterX, mCenterY, mRadius, mCirclePaint);}public final int getFillColor() {return mCirclePaint.getColor();}public WaveView setFillColor(int color) {if (mCirclePaint.getColor() != color) {mCirclePaint.setColor(color);invalidate(mCenterX, mCenterY, mRadius);}return this;}public final float getCenterX() {return mCenterX;}public WaveView setCenterX(float centerX) {final float oldCenterX = mCenterX;if (oldCenterX != centerX) {mCenterX = centerX;invalidate(oldCenterX, mCenterY, mRadius);invalidate(centerX, mCenterY, mRadius);}return this;}public final float getCenterY() {return mCenterY;}public WaveView setCenterY(float centerY) {final float oldCenterY = mCenterY;if (oldCenterY != centerY) {mCenterY = centerY;invalidate(mCenterX, oldCenterY, mRadius);invalidate(mCenterX, centerY, mRadius);}return this;}public final float getRadius() {return mRadius;}public WaveView setRadius(float radius) {final float oldRadius = mRadius;if (oldRadius != radius) {mRadius = radius;invalidate(mCenterX, mCenterY, oldRadius);if (radius > oldRadius) {invalidate(mCenterX, mCenterY, radius);}}return this;}private void invalidate(float centerX, float centerY, float radius) {invalidate((int) (centerX - radius - 0.5f), (int) (centerY - radius - 0.5f),(int) (centerX + radius + 0.5f), (int) (centerY + radius + 0.5f));}
}

外部调用:

Animator animator = ObjectAnimator.ofFloat(myView, WaveView.RADIUS, 122f, 192f);animator.setInterpolator(PathInterpolatorCompat.create(0.2f, 0f, 0.24f, 1f));animator.setDuration(1120);final ValueAnimator fadeAnimator = ObjectAnimator.ofFloat(myView, View.ALPHA, 1.0f, 0.0f);fadeAnimator.setInterpolator(PathInterpolatorCompat.create(0.33f, 0f, 0.66f, 1f));fadeAnimator.setDuration(1120);final AnimatorSet alertAnimator = new AnimatorSet();alertAnimator.playTogether(animator, fadeAnimator);alertAnimator.start();



本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/430311.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

mysql数据库基础的简单操作指南

最近在学习mysql,本文是做的关于mysql学习的笔记&#xff0c;跟大家分享一下&#xff0c;希望对大家学习mysql知识有所助益。mysql现在几乎已经成了网站建设的主流数据库&#xff0c;很多php网站系统都采用了mysql数据库。比mssql最大的优势在于开源&#xff0c;免费。希望与大…

kill 进程_结束进程,查看后台进程

有一些进程想要结束怎么办呢&#xff1f;进程的优先是怎样的呢?如何查看后台正在运行的进程呢&#xff1f;下面我就来讲讲给进程发送信号(kill -l列出所有支持的信号)[rootqianfeng ~]# kill -l 编号 信号名1) SIGHUP 重新加载配置2) SIGINT 键盘中断CtrlC3) SIGQUIT 键盘退出…

软碟通

转载于:https://www.cnblogs.com/jcfxl/p/5739741.html

AlarmManager使用

AlarmManager是Android中常用的一种系统级别的提示服务&#xff0c;在特定的时刻为我们广播一个指定的Intent&#xff0c;为你的应用设定一个在未来某个时间唤醒的功能。 当闹钟响起&#xff0c;实际上是系统发出了为这个闹钟注册的广播&#xff0c;会自动开启目标应用。 注册…

controller调用controller的方法_SpringCloud(5):Feign整合Ribbon和Hystrix来进行远程调用与服务熔断...

Feign前面我们虽然使用了Ribbon和Hystrix进行了负载均衡和服务熔断&#xff0c;但是我们的消费方代码中由于要调用提供方的服务&#xff0c;比如&#xff1a;String user this.restTemplate.getForObject("http://service-provider/user/" id, String.class);使用了…

w3svc无法启动

w3svc无法启动 运行命令regedit&#xff0c;打开注册表编辑器&#xff0c;进入&#xff1a;HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\HTTP]: a) Change Start from 4 ( disabled) to 3 ( automatically)b) Delete "NoRun" key if this key exists 重启…

Retrofit的简单使用

Retrofit是REST安卓客户端请求库。使用Retrofit可以进行GET&#xff0c;POST&#xff0c;PUT&#xff0c;DELETE等请求方式。官方文档&#xff1a;http://square.github.io/retrofit/ 1.在AndroidManifest.xml中请求了网络权限&#xff1a; <uses-permission android:name&…

vba excel 退出编辑状态_VBA小常识(15)—Application对象

VBA小常识&#xff08;15&#xff09;—Application对象学习自杨洋老师《全民一起学VBA》Application是位于最顶端的对象&#xff0c;代表excel程序本身&#xff0c;所有对象都属于它。1. application.cells当前处于激活状态的工作表&#xff0c;即“活动工作表”的单元格。此时…

手机--装饰者模式

/* * 装饰者* 装饰者与被装饰对象有相同的父类* 装饰者内包含被装饰者对象的引用* * 组合替代继承来扩展对象行为&#xff0c;运行时根据需求动态扩展* 扩展开放&#xff0c;修改关闭。* */public class Phone { }public class IPhone : Phone{public IPhone() { Console.Write…

一个网站 Code/Android/

http://www.java2s.com/Code/Android/

c++ stack 遍历_划重点啦!带你解读图的两种遍历方式

01知识框架02图的遍历1深度优先遍历基本思想&#xff1a;首先访问图中起始顶点v&#xff0c;然后由v出发&#xff0c;访问与v邻接且未被访问的顶点再访问与v相邻且未被访问的顶点 w1...重复上述过程。当不能再继续向下访问时&#xff0c;依次退回到最近被的问的顶点&#xff0c…

for循环延时_单片机的独立按键学习,实现60秒循环数字的启动暂停与清零

想要学习单片机必须要准备单片机开发板一块&#xff0c;和单片机C语言版本的书籍&#xff0c;我会把每天录制视频的源代码到头条&#xff0c;并且录制相同文章名字视频到西瓜视频&#xff0c;如果你再学习独立按键这一块有不明白的地方可以点开我的头像&#xff0c;进入我的主页…

本来中午打算应付下随便吃点,可是连盐都没有放的辣椒炒蛋实在是令人不快...

可是现在由于这个问题已经产生&#xff0c;我就将计就计&#xff0c;把他的空调电池给弄了&#xff0c;额后下午起来已经是7点半&#xff0c;就出去买了一袋4元的盐&#xff0c;本来还是打算会醒来早点&#xff0c;可是自己到了夜间才睡醒&#xff0c;还是将就再吃个没有放辣椒…

调用startactivityforresult后,onactivityresult立刻返回RESULT_CANCELED

调用startActivityForResult后&#xff0c;onActivityResult立刻响应&#xff0c;原因如下&#xff1a;1. 去掉跳转到的页面的android:launchMode"singleTask"属性&#xff08;SINGLE_TASK标识以及SINGLE_INSTANCE两个标识必须在r.result0的条件中&#xff0c;即这两…

mysql创建全外连接的视图_关系型数据库 MySQL 表索引和视图详解

原创&#xff1a; JiekeXu JiekeXu之路 一、索引数据库索引通俗的讲就是和书本的目录一样&#xff0c;主要就是为了提高查询数据的效率。由于数据存储在数据库表中&#xff0c;所以索引是创建在数据库表对象上&#xff0c;由表中的一个字段或多个字段生成的键组成&#xff0c;这…

FatMouse's Speed hdu 1160(动态规划,最长上升子序列+记录路径)

http://acm.hdu.edu.cn/showproblem.php?pid1160 题意&#xff1a;现给出老鼠的体重与速度&#xff0c;要求你找出符合要求的最长子序列。 要求是 W[m[1]] < W[m[2]] < ... < W[m[n]]&#xff08;体重&#xff09; && S[m[1]] > S[m[2]] > ... > S[…

设计一个名为complex的类来表示复数_complex类java解决

作者&#xff1a;wzu_cza123出自&#xff1a;CSDN原文&#xff1a;blog.csdn.net/wzu_cza123/article/details/108857953?utm_mediumdistribute.pc_category.none-task-blog-hot-5.nonecase&depth_1-utm_sourcedistribute.pc_category.none-task-blog-hot-5.nonecase&…

java将经纬度转换为平面坐标

&#xff08;米勒投影&#xff09;算法public static double[] MillierConvertion(double lat, double lon) { double L 6381372 * Math.PI * 2;//地球周长 double WL;// 平面展开后&#xff0c;x轴等于周长 double HL/2;// y轴约等于周长一半 double mill2.3;// 米勒投影…

小程序onload_微信小程序开发入门之共享账本(十四)

微信小程序开发入门之共享账本(十四)(备注&#xff1a;微信小程序的wxml文件相当于HTML文件&#xff0c;wxss文件相当于CSS文件&#xff0c;js文件就是JavaScript文件&#xff0c;数据库为NoSQL数据库&#xff0c;数据库脚本语言也同NoSQL&#xff0c;因为是运行在微信内&#…

POJ 3342 树形DP+Hash

这是很久很久以前做的一道题&#xff0c;可惜当时WA了一页以后放弃了。 今天我又重新捡了起来。&#xff08;哈哈1A了&#xff09; 题意&#xff1a; 没有上司的舞会判重 思路&#xff1a; hash一下树形DP 题目中给的人名hash到数字&#xff0c;再进行运算。 树形DP f[x…