Skip to content

Commit

Permalink
v0.1.1版本新增浮动粒子控件
Browse files Browse the repository at this point in the history
  • Loading branch information
HpWens committed Jul 6, 2018
1 parent faba57b commit ac70219
Show file tree
Hide file tree
Showing 9 changed files with 405 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<activity android:name=".meis.MeiVideoDragListActivity"></activity>
<activity android:name=".meis.MeiMoBikeActivity"></activity>
<activity android:name=".meis.MeiRoseActivity"></activity>
<activity android:name=".meis.MeiFireflyActivity"></activity>
</application>

</manifest>
4 changes: 4 additions & 0 deletions app/src/main/java/com/demo/widget/meis/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,8 @@ public void onMokibe(View view) {
public void onRose(View view) {
startActivity(new Intent(this, MeiRoseActivity.class));
}

public void onFirefly(View view) {
startActivity(new Intent(this, MeiFireflyActivity.class));
}
}
25 changes: 25 additions & 0 deletions app/src/main/java/com/demo/widget/meis/MeiFireflyActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.demo.widget.meis;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.demo.widget.R;
import com.demo.widget.utils.Eyes;
import com.meis.widget.particle.FireflyView;

/**
* Created by wenshi on 2018/7/5.
* Description 浮动粒子界面
*/
public class MeiFireflyActivity extends AppCompatActivity {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mei_fire_fly_activity);
Eyes.translucentStatusBar(this, true);
}
}
18 changes: 18 additions & 0 deletions app/src/main/res/layout/mei_fire_fly_activity.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.meis.widget.particle.FireflyView
android:id="@+id/firefly"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@mipmap/ic_firefly_bg"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />


</android.support.constraint.ConstraintLayout>
25 changes: 25 additions & 0 deletions app/src/main/res/layout/meis_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -192,5 +192,30 @@

</android.support.v7.widget.CardView>

<android.support.v7.widget.CardView
android:id="@+id/cv_firefly"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="16dp"
android:foreground="?attr/selectableItemBackground"
android:onClick="onFirefly"
app:cardBackgroundColor="@android:color/white"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="true">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="16dp"
android:text="浮动粒子"
android:textAllCaps="false"
android:textColor="#999999"
android:textSize="16sp" />

</android.support.v7.widget.CardView>

</LinearLayout>
</android.support.v4.widget.NestedScrollView>
Binary file added app/src/main/res/mipmap-xxxhdpi/ic_firefly_bg.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
164 changes: 164 additions & 0 deletions widget/src/main/java/com/meis/widget/particle/FireflyView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
package com.meis.widget.particle;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.meis.widget.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
* Created by wenshi on 2018/7/5.
* Description 浮点粒子控件
*/
public class FireflyView extends SurfaceView implements SurfaceHolder.Callback {

// 粒子的最大数量
private static final int MAX_NUM = 400;
// 粒子集合
private List<FloatParticle> mListParticles;
// 随机数
private Random mRandom;

private SurfaceHolder mHolder;

// 动画线程
private Handler mHandler;

// 粒子半径
private int mParticleMaxRadius;

// 粒子数量
private int mParticleNum;

// 粒子移动速率
private int mParticleMoveRate;

private static final int EMPTY_FLAG = 1;

public FireflyView(Context context) {
this(context, null);
}

public FireflyView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}

public FireflyView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// 关闭硬件加速
setLayerType(LAYER_TYPE_SOFTWARE, null);
init();

TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.FireflyView);
mParticleMaxRadius = ta.getInt(R.styleable.FireflyView_firefly_max_radius, 5);
mParticleNum = ta.getInt(R.styleable.FireflyView_firefly_num, MAX_NUM);
mParticleMoveRate = ta.getInt(R.styleable.FireflyView_firefly_move_rate, 5);
ta.recycle();
}

private void init() {
// 设置透明
setZOrderOnTop(true);
// 配合清屏 canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
mHolder = getHolder();
mHolder.setFormat(PixelFormat.TRANSLUCENT);
mHolder.addCallback(this);
// 初始化随机数
mRandom = new Random();
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
int measuredWidth = getMeasuredWidth();
int measureHeight = getMeasuredHeight();

initParticlesData(measuredWidth, measureHeight);
startAnimation();
}

// 初始化浮点粒子数据
private void initParticlesData(int width, int height) {
mListParticles = new ArrayList<>();
for (int i = 0; i < mParticleNum; i++) {
FloatParticle fp = new FloatParticle(width, height);
mParticleMaxRadius = mParticleMaxRadius < 2 ? 2 : mParticleMaxRadius;
fp.setRadius(mRandom.nextInt(mParticleMaxRadius - 1) + 1);
mListParticles.add(fp);
}
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
stopAnimation();
}

public void stopAnimation() {
mHandler.removeCallbacksAndMessages(null);
}

public void startAnimation() {
if (mHandler != null) return;
HandlerThread fireThread = new HandlerThread(this.getClass().getName());
fireThread.start();
mHandler = new Handler(fireThread.getLooper()) {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
Canvas mCanvas = mHolder.lockCanvas(null);
if (mCanvas != null) {
synchronized (mHolder) {
// 清屏
mCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
for (FloatParticle fp : mListParticles) {
fp.drawParticle(mCanvas);
}
}
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (mCanvas != null) {
mHolder.unlockCanvasAndPost(mCanvas);
}
mHandler.sendEmptyMessageDelayed(EMPTY_FLAG, mParticleMoveRate);
}
};
mHandler.sendEmptyMessage(EMPTY_FLAG);
}

public int getParticleMoveRate() {
return mParticleMoveRate;
}

public void setParticleMoveRate(int particleMoveRate) {
mParticleMoveRate = particleMoveRate;
}

public int getParticleMaxRadius() {
return mParticleMaxRadius;
}

public int getParticleNum() {
return mParticleNum;
}
}
Loading

0 comments on commit ac70219

Please sign in to comment.