Skip to content

Commit

Permalink
zzz
Browse files Browse the repository at this point in the history
  • Loading branch information
1139618418 committed Dec 1, 2016
1 parent 7272e3a commit d35bc7d
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 1 deletion.
4 changes: 3 additions & 1 deletion app/src/main/AndroidManifest.xml
Expand Up @@ -2,13 +2,15 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="me.happy.demo">

<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".LockScreenActivity">
<activity android:name=".WaveActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
36 changes: 36 additions & 0 deletions app/src/main/java/me/happy/demo/WaveActivity.java
@@ -0,0 +1,36 @@
package me.happy.demo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import me.happy.demo.view.wave.WaveView3;

public class WaveActivity extends AppCompatActivity {

private ImageView imageView;
private WaveView3 waveView3;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wave);
imageView = (ImageView) findViewById(R.id.image);
waveView3 = (WaveView3) findViewById(R.id.wave_view);

final FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(-2,-2);
lp.gravity = Gravity.BOTTOM|Gravity.CENTER;
waveView3.setOnWaveAnimationListener(new WaveView3.OnWaveAnimationListener() {
@Override
public void OnWaveAnimation(float y) {
lp.setMargins(0,0,0,(int)y+2);
imageView.setLayoutParams(lp);
}
});
}
}
99 changes: 99 additions & 0 deletions app/src/main/java/me/happy/demo/view/wave/WaveView3.java
@@ -0,0 +1,99 @@
package me.happy.demo.view.wave;

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.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

/**
* Created by Administrator on 2016/11/30.
*/

public class WaveView3 extends View {

private Path mAbovePath,mBelowWavePath;
private Paint mAboveWavePaint,mBelowWavePaint;

private DrawFilter mDrawFilter;

private float φ;

private OnWaveAnimationListener mWaveAnimationListener;

public WaveView3(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) {

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);
}

}


42 changes: 42 additions & 0 deletions app/src/main/res/layout/activity_wave.xml
@@ -0,0 +1,42 @@
<?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="#FF3366">

<me.happy.demo.view.wave.WaveView3
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>
Binary file added art/wave_view.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d35bc7d

Please sign in to comment.