Skip to content

Commit

Permalink
行星球特效优化、历史的今天加入折叠文本
Browse files Browse the repository at this point in the history
  • Loading branch information
wosojadfjgo committed Sep 10, 2019
1 parent 24d1e7d commit 3c7df5d
Show file tree
Hide file tree
Showing 9 changed files with 607 additions and 31 deletions.
455 changes: 455 additions & 0 deletions base/src/main/java/com/heyongrui/base/widget/CollapsedTextView.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -406,13 +406,16 @@ private void processTouch() {
int left = (int) (centerX + planetModel.getLoc2DX()) - child.getMeasuredWidth() / 2;
int top = (int) (centerY + planetModel.getLoc2DY()) - child.getMeasuredHeight() / 2;
// 从View的Tag里取出位置之前的位置信息,平移新旧位置差值
int[] originLocation = (int[]) child.getTag();
if (originLocation != null && originLocation.length > 0) {
child.setTranslationX((float) (left - originLocation[0]));
child.setTranslationY((float) (top - originLocation[1]));
// 小于移动速度,刷新
if (Math.abs(mAngleX) <= speed && Math.abs(mAngleY) <= speed) {
child.invalidate();
Object childTag = child.getTag();
if (null != childTag && childTag instanceof int[]) {
int[] originLocation = (int[]) childTag;
if (originLocation != null && originLocation.length > 0) {
child.setTranslationX((float) (left - originLocation[0]));
child.setTranslationY((float) (top - originLocation[1]));
// 小于移动速度,刷新
if (Math.abs(mAngleX) <= speed && Math.abs(mAngleY) <= speed) {
child.invalidate();
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public float getScale() {

public void setScale(float scale) {
this.scale = scale;
if (this.mView != null) {
if (null != this.mView && this.mView instanceof PlanetView) {
((PlanetView) this.mView).setScale(scale);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ public PlanetView(Context context) {
init(context);
}

public PlanetView(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
init(context);
}

public PlanetView(Context context, AttributeSet attributeSet, int i) {
super(context, attributeSet, i);
init(context);
}

private void init(Context context) {
starPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
starPaint.setColor(0xFFFF0000);
Expand Down Expand Up @@ -90,20 +100,23 @@ private void init(Context context) {
radiusIncrement = starMin / 16.0f;
}

public PlanetView(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
init(context);
}

public PlanetView(Context context, AttributeSet attributeSet, int i) {
super(context, attributeSet, i);
init(context);
}

public void setScale(float scale) {
this.scale = scale;
}

/**
* 设置文本昵称颜色
*
* @param textColor int[],渐变颜色数组(eg:new int[]{0x33333333, PlanetView.COLOR_BEST_MATCH, PlanetView.COLOR_MOST_ACTIVE, 0x33333333})
* @param position int[],对应color数组位置(eg:new float[]{0.0f, 0.15f, 0.85f, 1.0f})
*/
public void setTextColor(int[] textColor, float[] position) {
int startX = sp2px(getContext(), 50.0f);
signPaint.setShader(new LinearGradient((float) startX, 0.0f, 0.0f, 0.0f,
textColor, position, TileMode.CLAMP));
invalidate();
}

/**
* 设置星星颜色
*
Expand Down
15 changes: 15 additions & 0 deletions base/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,19 @@
<attr name="startAngleX" format="float" />
<attr name="startAngleY" format="float" />
</declare-styleable>
<!--折疊文本-->
<declare-styleable name="CollapsedTextView">
<attr name="collapsedLines" format="integer" />
<attr name="expandedText" format="string" />
<attr name="collapsedText" format="string" />
<attr name="expandedDrawable" format="reference" />
<attr name="collapsedDrawable" format="reference" />
<attr name="tipsGravity">
<flag name="end" value="0" />
<flag name="bottom" value="1" />
</attr>
<attr name="tipsColor" format="reference|color" />
<attr name="tipsUnderline" format="boolean" />
<attr name="tipsClickable" format="boolean" />
</declare-styleable>
</resources>
2 changes: 2 additions & 0 deletions base/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@
<string name="no_search_content">没有搜索到相关内容</string>
<string name="no_app_support_tip">手机暂无应用支持打开此文件</string>
<string name="add">添加</string>
<string name="collapse">收起</string>
<string name="unfold">展开</string>
</resources>
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package com.heyongrui.main.planetball.view;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.heyongrui.base.assist.ConfigConstants;
import com.heyongrui.base.base.BaseActivity;
import com.heyongrui.base.widget.firefly.FireflyView;
import com.heyongrui.base.widget.planetball.adapter.PlanetAdapter;
import com.heyongrui.base.widget.planetball.view.PlanetBallView;
import com.heyongrui.base.widget.planetball.view.PlanetView;
Expand All @@ -19,6 +23,10 @@

@Route(path = ConfigConstants.PATH_PLANET_BALL)
public class PlanetBallActivity extends BaseActivity {

private FireflyView fireflyView;
private Handler mHandler = new Handler();

@Override
protected int getLayoutId() {
return R.layout.activity_planet_ball;
Expand All @@ -27,19 +35,22 @@ protected int getLayoutId() {
@Override
protected void init(Bundle savedInstanceState) {
PlanetBallView planetBall = findViewById(R.id.planet_ball);
planetBall.setAdapter(new PlanetAdapter() {
initPlanetBallView(planetBall);

fireflyView = findViewById(R.id.firefly);
}

private void initPlanetBallView(PlanetBallView planetBallView) {
planetBallView.setAdapter(new PlanetAdapter() {
@Override
public int getCount() {
return 50;
}

@Override
public View getView(Context context, int position, ViewGroup parent) {
PlanetView planetView = new PlanetView(context);
planetView.setSign(getRandomNick());

int starColor = position % 2 == 0 ? PlanetView.COLOR_FEMALE : PlanetView.COLOR_MALE;
boolean hasShadow = false;
boolean isShining = false;

String str = "";
if (position % 12 == 0) {
Expand All @@ -52,15 +63,22 @@ public View getView(Context context, int position, ViewGroup parent) {
str = "最新人";
starColor = PlanetView.COLOR_MOST_NEW;
} else if (position % 18 == 0) {
hasShadow = true;
isShining = true;
str = "最闪耀";
} else {
str = "描述";
}

PlanetView planetView = new PlanetView(context);
int[] textColor = new int[]{0x33333333, getRandomColor(), getRandomColor(), 0x33333333};
float[] colorPosition = new float[]{0.0f, 0.15f, 0.85f, 1.0f};
planetView.setTextColor(textColor, colorPosition);
planetView.setStarColor(starColor);
planetView.setHasShadow(hasShadow);
String nickName = getRandomNick();
planetView.setSign(nickName);
planetView.setHasShadow(isShining);
planetView.setMatch(position * 2 + "%", str);
if (hasShadow) {
if (isShining) {
planetView.setMatchColor(starColor);
} else {
planetView.setMatchColor(PlanetView.COLOR_MOST_ACTIVE);
Expand All @@ -71,6 +89,7 @@ public View getView(Context context, int position, ViewGroup parent) {
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(starWidth, starHeight);
planetView.setPadding(0, starPaddingTop, 0, 0);
planetView.setLayoutParams(layoutParams);
planetView.setOnClickListener(view -> ToastUtils.showShort(nickName + "\n" + position * 2 + "%"));
return planetView;
}

Expand All @@ -91,10 +110,19 @@ public void onThemeColorChanged(View view, int themeColor) {
});
}

/**
* 获取随机颜色
*/
private int getRandomColor() {
Random random = new Random();
int r = random.nextInt(256);
int g = random.nextInt(256);
int b = random.nextInt(256);
return Color.argb(255, r, g, b);
}

/**
* 获取随机昵称
*
* @return 随机昵称
*/
private String getRandomNick() {
Random random = new Random();
Expand All @@ -108,8 +136,6 @@ private String getRandomNick() {

/**
* 获取随机单个汉字
*
* @return 随机单个汉字
*/
private String getRandomSingleCharacter() {
String str = "";
Expand All @@ -129,4 +155,48 @@ private String getRandomSingleCharacter() {
return str;
}

/**
* 控制萤火虫粒子动画是否播放
*/
private void toggleAnimationEffect(boolean isStart) {
if (isStart) {
if (fireflyView != null && !fireflyView.isPlaying()) {
fireflyView.setVisibility(View.VISIBLE);
fireflyView.startAnimation();
}
} else {
if (fireflyView != null && fireflyView.isPlaying()) {
fireflyView.stopAnimation();
// fireflyView.setZOrderMediaOverlay(true);
fireflyView.setVisibility(View.GONE);
}
}
}

@Override
protected void onResume() {
super.onResume();
if (null != mHandler) {
mHandler.removeCallbacksAndMessages(null);
mHandler.postDelayed(() -> toggleAnimationEffect(true), 1000);
}
}

@Override
protected void onPause() {
super.onPause();
if (null != mHandler) {
mHandler.removeCallbacksAndMessages(null);
mHandler.postDelayed(() -> toggleAnimationEffect(false), 1000);
}
}

@Override
protected void onDestroy() {
if (null != mHandler) {
mHandler.removeCallbacksAndMessages(null);
mHandler = null;
}
super.onDestroy();
}
}
11 changes: 11 additions & 0 deletions main/src/main/res/layout/activity_planet_ball.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@
android:layout_height="match_parent"
android:background="@color/black">

<com.heyongrui.base.widget.firefly.FireflyView
android:id="@+id/firefly"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@color/black"
app:firefly_num="200"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<com.heyongrui.base.widget.planetball.view.PlanetBallView
android:id="@+id/planet_ball"
android:layout_width="wrap_content"
Expand Down
9 changes: 8 additions & 1 deletion module2/src/main/res/layout/recycle_item_today_history.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,24 @@
app:layout_constraintTop_toBottomOf="@+id/dash_top"
tools:text="2019\n09-07" />

<TextView
<com.heyongrui.base.widget.CollapsedTextView
android:id="@+id/tv_content"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/dp_30"
android:layout_marginLeft="@dimen/dp_30"
android:textColor="@color/text_color"
android:textSize="@dimen/sp_14"
app:collapsedLines="3"
app:collapsedText="@string/collapse"
app:expandedText="@string/unfold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/tv_date"
app:layout_constraintTop_toTopOf="@+id/tv_date"
app:tipsClickable="true"
app:tipsColor="@color/colorAccent"
app:tipsGravity="end"
app:tipsUnderline="false"
tools:text="@string/app_name" />

<View
Expand Down

0 comments on commit 3c7df5d

Please sign in to comment.