实战属性动画与自定义控件相结合波浪
来源:互联网 发布:购物秒杀软件 编辑:程序博客网 时间:2024/05/17 21:43
多说不如直接上代码
初始化属性动画:
List<Animator> animators =new ArrayList<>();
// horizontal animation.
// wave waves infinitely.
ObjectAnimator waveShiftAnim = ObjectAnimator.ofFloat(
mWaveView,"waveShiftRatio",0f,1f);
waveShiftAnim.setRepeatCount(ValueAnimator.INFINITE);
waveShiftAnim.setDuration(5000);
waveShiftAnim.setInterpolator(newLinearInterpolator());
animators.add(waveShiftAnim);
// vertical animation.
// water level increases from 0 to center of WaveView
ObjectAnimator waterLevelAnim = ObjectAnimator.ofFloat(
mWaveView,"waterLevelRatio",0f,0.5f);
// waterLevelAnim.setDuration(10000);
waterLevelAnim.setInterpolator(newDecelerateInterpolator());
animators.add(waterLevelAnim);
// amplitude animation.
// wave grows big then grows small, repeatedly
ObjectAnimator amplitudeAnim = ObjectAnimator.ofFloat(
mWaveView,"amplitudeRatio",0.0001f,0.05f);
amplitudeAnim.setRepeatCount(ValueAnimator.INFINITE);
amplitudeAnim.setRepeatMode(ValueAnimator.REVERSE);
amplitudeAnim.setDuration(5000);
amplitudeAnim.setInterpolator(newLinearInterpolator());
animators.add(amplitudeAnim);
mAnimatorSet= newAnimatorSet();
mAnimatorSet.playTogether(animators);
在创建这些属性动画的时候,注意要对相对应得自定义书属性创建相对应的set方法:
public floatgetWaveShiftRatio() {
returnmWaveShiftRatio;
}
/**
* Shift the wave horizontally according to<code>waveShiftRatio</code>.
*
* @paramwaveShiftRatio Should be 0 ~ 1. Default to be 0.
* <br/>Result of waveShiftRatio multiples width of WaveView is the length to shift.
*/
public void setWaveShiftRatio(floatwaveShiftRatio) {
if(mWaveShiftRatio!= waveShiftRatio) {
mWaveShiftRatio= waveShiftRatio;
invalidate();
}
}
public float getWaterLevelRatio() {
returnmWaterLevelRatio;
}
/**
* Set water level according to <code>waterLevelRatio</code>.
*
* @paramwaterLevelRatio Should be 0 ~ 1. Default to be 0.5.
* <br/>Ratio of water level to WaveView height.
*/
public void setWaterLevelRatio(floatwaterLevelRatio) {
if(mWaterLevelRatio!= waterLevelRatio) {
mWaterLevelRatio= waterLevelRatio;
invalidate();
}
}
public float getAmplitudeRatio() {
returnmAmplitudeRatio;
}
/**
* Set vertical size of wave according to<code>amplitudeRatio</code>
*
* @paramamplitudeRatio Default to be 0.05. Result of amplitudeRatio + waterLevelRatio should be less than 1.
* <br/>Ratio of amplitude to height of WaveView.
*/
public void setAmplitudeRatio(floatamplitudeRatio) {
if(mAmplitudeRatio!= amplitudeRatio) {
mAmplitudeRatio= amplitudeRatio;
invalidate();
}
}
public float getWaveLengthRatio() {
returnmWaveLengthRatio;
}
/**
* Set horizontal size of wave according to<code>waveLengthRatio</code>
*
* @paramwaveLengthRatio Default to be 1.
* <br/>Ratio of wave length to width of WaveView.
*/
public void setWaveLengthRatio(floatwaveLengthRatio) {
mWaveLengthRatio= waveLengthRatio;
}
public boolean isShowWave() {
returnmShowWave;
}
public void setShowWave(booleanshowWave) {
mShowWave= showWave;
}
在自定义波浪中首先创建画笔功工具:
private voidinit() {
mShaderMatrix= newMatrix();
mViewPaint= newPaint();
mViewPaint.setAntiAlias(true);
}
/**
* Create the shader with default waves which repeat horizontally, and clamp vertically
*/
private void createShader() {
mDefaultAngularFrequency= 2.0f* Math.PI/ DEFAULT_WAVE_LENGTH_RATIO/ getWidth();
mDefaultAmplitude= getHeight() * DEFAULT_AMPLITUDE_RATIO;
mDefaultWaterLevel= getHeight() * 0.9f;
mDefaultWaveLength= getWidth();
Bitmap bitmap = Bitmap.createBitmap(getWidth(),getHeight(),Bitmap.Config.ARGB_8888);
Canvas canvas =new Canvas(bitmap);
Paint wavePaint =new Paint();
wavePaint.setStrokeWidth(2);
wavePaint.setAntiAlias(true);
// Draw default waves into the bitmap
// y=Asin(ωx+φ)+h
final intendX = getWidth() + 1;
final int endY = getHeight() +1;
float[] waveY =new float[endX];
wavePaint.setColor(mBehindWaveColor);
for (intbeginX = 0;beginX < endX;beginX++) {
doublewx = beginX * mDefaultAngularFrequency;
float beginY = (float) (mDefaultWaterLevel+ mDefaultAmplitude* Math.sin(wx));
canvas.drawLine(beginX,beginY,beginX,endY,wavePaint);
waveY[beginX] = beginY;
}
wavePaint.setColor(mFrontWaveColor);
final int wave2Shift = (int) (mDefaultWaveLength/ 4);
for (intbeginX = 0;beginX < endX;beginX++) {
canvas.drawLine(beginX,waveY[(beginX + wave2Shift) % endX],beginX,endY,wavePaint);
}
// use the bitamp to create the shader
mWaveShader= newBitmapShader(bitmap,Shader.TileMode.REPEAT,Shader.TileMode.CLAMP);
mViewPaint.setShader(mWaveShader);
}
工具创建完之后,对布局的刻画以及布局改变时的操作:
@Override
protected void onDraw(Canvas canvas) {
// modify paint shader according to mShowWave state
if(mShowWave&& mWaveShader!= null) {
// first call after mShowWave, assign it to our paint
if(mViewPaint.getShader() ==null) {
mViewPaint.setShader(mWaveShader);
}
// sacle shader according to mWaveLengthRatio and mAmplitudeRatio
// this decides the size(mWaveLengthRatio for width, mAmplitudeRatio for height) of waves
mShaderMatrix.setScale(
mWaveLengthRatio/ DEFAULT_WAVE_LENGTH_RATIO,
mAmplitudeRatio/ DEFAULT_AMPLITUDE_RATIO,
0,
mDefaultWaterLevel);
// translate shader according to mWaveShiftRatio and mWaterLevelRatio
// this decides the start position(mWaveShiftRatio for x, mWaterLevelRatio for y) of waves
mShaderMatrix.postTranslate(
mWaveShiftRatio* getWidth(),
(DEFAULT_WATER_LEVEL_RATIO- mWaterLevelRatio) * getHeight());
// assign matrix to invalidate the shader
mWaveShader.setLocalMatrix(mShaderMatrix);
float borderWidth =mBorderPaint ==null ?0f :mBorderPaint.getStrokeWidth();
if (borderWidth >0) {
canvas.drawRect(
borderWidth / 2f,
borderWidth /2f,
getWidth() - borderWidth /2f -0.5f,
getHeight() - borderWidth /2f -0.5f,
mBorderPaint);
}
canvas.drawRect(borderWidth,borderWidth,getWidth() - borderWidth,
getHeight() - borderWidth,mViewPaint);
}else {
mViewPaint.setShader(null);
}
}
布局改变时重新绘制波浪图形:
@Override
protected void onSizeChanged(intw, inth, intoldw, intoldh) {
super.onSizeChanged(w,h,oldw,oldh);
createShader();
}
就这样简单的完成了
- 实战属性动画与自定义控件相结合波浪
- Android属性动画与自定义控件画圆球移动
- 自定义控件与自定义属性
- 动画实战:自定义提醒控件AlertView
- 【android自定义控件】属性动画 <五>
- 属性动画、事件分发和自定义控件
- 属性动画在自定义控件中的使用
- 自定义View——实现波浪动画
- Android 自定义View实现波浪动画
- Android 自定义控件与属性
- Android自定义控件与属性
- 一个炫酷的 TabLayout 与 CoordinatorLayout 相结合的自定义控件
- Android自定义控件--波浪球SurfaceView实现
- Android双波浪自定义控件(DoubleWaveView)
- Android自定义控件波浪线工具类
- Android开发之自定义控件与属性动画Animation的结合使用
- 安卓属性动画与自定义控件实现圆球移动并实现颜色渐变
- 自定义控件 ----- 属性与组合控件
- document.domain解决跨域
- JAVA BEAN
- 通过Hibernate API编写访问数据库的代码
- zookeeper手把手搭建伪分布集群环境
- 第二十七条:优先使用泛型方法
- 实战属性动画与自定义控件相结合波浪
- iOS动画——文字晃动警告
- Java 面试-1
- 《Android 群英传》 阅读笔记 第四章~第六章
- 自用Android程序破解,逆向分析工具集
- c/c++ core dump使用
- 内表使用Binary Search的限制
- CentOS6环境单服务器FastDFS+Nginx+fastdfs-nginx-module 安装配置
- Linux网络编程 - TCP Socket 简单练习:新进程辅助通信