前言:在网上经常会看到别人写的一些开源项目,然后会惊叹于他们的写的效果,当然那些大神也会把代码放出来,然后供大家看,但是因为他们是自己写的,所以有些地方就是单纯的贴了代码,让大家自己去看。介于我前面动画方面比较薄弱,所以有些地方就要一边跟着敲代码,一边去网上查相关知识。所以就借这次机会。我来写下我最近学的动画效果及相关的知识。
1、效果图:
2、自定义view怎样实现
package com.example.a.viewapplication;
import android.graphics.Path;
import android.view.View;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DrawFilter;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.util.AttributeSet;
public class WaveView extends View {
private Path mAbovePath,mBelowWavePath;
private Paint mAboveWavePaint,mBelowWavePaint;
private DrawFilter mDrawFilter;
private float φ;
private OnWaveAnimationListener mWaveAnimationListener;
public WaveView(Context context, AttributeSet attrs) {
super(context, attrs);
//初始化路径
mAbovePath = new Path();
mBelowWavePath = new Path();
//初始化画笔
mAboveWavePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mAboveWavePaint.setAntiAlias(true);
mAboveWavePaint.setStyle(Paint.Style.FILL);
mAboveWavePaint.setColor(Color.WHITE);
mBelowWavePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mBelowWavePaint.setAntiAlias(true);
mBelowWavePaint.setStyle(Paint.Style.FILL);
mBelowWavePaint.setColor(Color.WHITE);
mBelowWavePaint.setAlpha(80);
//画布抗锯齿
mDrawFilter = new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.setDrawFilter(mDrawFilter);
mAbovePath.reset();
mBelowWavePath.reset();
φ-=0.1f;
float y,y2;
double ω = 2*Math.PI / getWidth();
mAbovePath.moveTo(getLeft(),getBottom());
mBelowWavePath.moveTo(getLeft(),getBottom());
for (float x = 0; x <= getWidth(); x += 20) {
/**
* y=Asin(ωx+φ)+k
* A—振幅越大,波形在y轴上最大与最小值的差值越大
* ω—角速度, 控制正弦周期(单位角度内震动的次数)
* φ—初相,反映在坐标系上则为图像的左右移动。这里通过不断改变φ,达到波浪移动效果
* k—偏距,反映在坐标系上则为图像的上移或下移。
*/
y = (float) (8 * Math.cos(ω * x + φ) +8);
y2 = (float) (8 * Math.sin(ω * x + φ));
mAbovePath.lineTo(x, y);
mBelowWavePath.lineTo(x, y2);
//回调 把y坐标的值传出去(在activity里面接收让小机器人随波浪一起摇摆)
mWaveAnimationListener.OnWaveAnimation(y);
}
mAbovePath.lineTo(getRight(),getBottom());
mBelowWavePath.lineTo(getRight(),getBottom());
canvas.drawPath(mAbovePath,mAboveWavePaint);
canvas.drawPath(mBelowWavePath,mBelowWavePaint);
postInvalidateDelayed(20);
}
public void setOnWaveAnimationListener(OnWaveAnimationListener l){
this.mWaveAnimationListener = l;
}
public interface OnWaveAnimationListener{
void OnWaveAnimation(float y);
}
}
3、在activity的layout中怎样调用:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@android:color/holo_red_dark"
>
<com.example.a.viewapplication.WaveView
android:id="@+id/wave_view"
android:layout_width="match_parent"
android:layout_height="15dp"
android:layout_gravity="bottom" />
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:background="@mipmap/ic_launcher" />
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="其他内容"
android:textSize="24sp" />
</LinearLayout>
</LinearLayout>
4、在activity的代码中怎样使用:
package com.example.a.viewapplication;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.FrameLayout;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView imageView;
private WaveView waveView3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.image);
waveView3 = (WaveView) findViewById(R.id.wave_view);
final FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(-2,-2);
lp.gravity = Gravity.BOTTOM|Gravity.CENTER;
waveView3.setOnWaveAnimationListener(new WaveView.OnWaveAnimationListener() {
@Override
public void OnWaveAnimation(float y) {
lp.setMargins(0,0,0,(int)y+2);
imageView.setLayoutParams(lp);
}
});
}
}
5、解析一下自定义view:
一):自定义View流程:
步骤 | 关键字 | 作用 |
---|---|---|
1 | 构造函数 | View初始化 |
2 | onMeasure | 测量View大小 |
3 | onSizeChanged | 确定View大小 |
4 | onLayout | 确定子View布局(自定义View包含子View时有用) |
5 | onDraw | 实际绘制内容 |
6 | 提供接口 | 控制View或监听View某些状态 |
二):绘制内容(onDraw)
onDraw是实际绘制的部分,也就是我们真正关心的部分,使用的是Canvas绘图。
哈哈。没错。那二个上下浮动的曲线。我们可以用同时画二个线,一个sin函数,一个cos函数。而且处于同一水平线。不就一个交错的波浪了。
6、效果图: