GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — 逐字显示的textview · luohaiah/AndroidCustomView@5f549dd

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • app/src/main
    • AndroidManifest.xml
    • java/com/allen/androidcustomview
      • activity
        • AnimationViewActivity.java
        • MainActivity.java
      • widget
        • BaseView.java
        • FadeInTextView.java
        • LoadingButton.java
    • res/layout
      • activity_animation_view.xml
      • activity_main.xml
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎app/src/main/AndroidManifest.xml‎

Copy file name to clipboardExpand all lines: app/src/main/AndroidManifest.xml
+4-2Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="com.allen.androidcustomview">
44

5-
<uses-permission android:name="android.permission.INTERNET"/>
5+
<uses-permission android:name="android.permission.INTERNET" />
6+
67
<application
78
android:allowBackup="true"
89
android:icon="@mipmap/ic_launcher"
@@ -23,7 +24,8 @@
2324
<activity android:name=".tagview.TagActivity" />
2425
<activity android:name=".activity.AnimationBtnActivity" />
2526
<activity android:name=".activity.ProgressBarActivity" />
26-
<activity android:name=".activity.PayPsdViewActivity"/>
27+
<activity android:name=".activity.PayPsdViewActivity" />
28+
<activity android:name=".activity.AnimationViewActivity"></activity>
2729
</application>
2830

2931
</manifest>
Collapse file

‎app/src/main/java/com/allen/androidcustomview/activity/AnimationViewActivity.java‎

Copy file name to clipboard
+40Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.allen.androidcustomview.activity;
2+
3+
import android.support.v7.app.AppCompatActivity;
4+
import android.os.Bundle;
5+
import android.view.View;
6+
import android.widget.Toast;
7+
8+
import com.allen.androidcustomview.R;
9+
import com.allen.androidcustomview.widget.FadeInTextView;
10+
import com.allen.androidcustomview.widget.LoadingButton;
11+
12+
public class AnimationViewActivity extends AppCompatActivity {
13+
14+
private FadeInTextView fadeInTextView;
15+
private LoadingButton loadingButton;
16+
17+
@Override
18+
protected void onCreate(Bundle savedInstanceState) {
19+
super.onCreate(savedInstanceState);
20+
setContentView(R.layout.activity_animation_view);
21+
22+
fadeInTextView = (FadeInTextView) findViewById(R.id.fade_in_tv);
23+
loadingButton = (LoadingButton) findViewById(R.id.loading_btn);
24+
25+
fadeInTextView.setTextString("自定义view实现字符串逐字显示").startFadeInAnimation().setTextAnimationListener(new FadeInTextView.TextAnimationListener() {
26+
@Override
27+
public void animationFinish() {
28+
loadingButton.stopLoading();
29+
}
30+
});
31+
32+
loadingButton.setOnClickListener(new View.OnClickListener() {
33+
@Override
34+
public void onClick(View v) {
35+
loadingButton.startLoading();
36+
fadeInTextView.startFadeInAnimation();
37+
}
38+
});
39+
}
40+
}
Collapse file

‎app/src/main/java/com/allen/androidcustomview/activity/MainActivity.java‎

Copy file name to clipboardExpand all lines: app/src/main/java/com/allen/androidcustomview/activity/MainActivity.java
+12Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
import android.support.v7.app.AppCompatActivity;
66
import android.view.View;
77
import android.widget.Button;
8+
import android.widget.TextView;
9+
import android.widget.Toast;
810

911
import com.allen.androidcustomview.R;
1012
import com.allen.androidcustomview.tagview.TagActivity;
13+
import com.allen.androidcustomview.widget.FadeInTextView;
1114

1215

1316
public class MainActivity extends AppCompatActivity {
@@ -21,6 +24,7 @@ public class MainActivity extends AppCompatActivity {
2124
private Button pay_psd_view_btn;
2225
private Button progress_btn;
2326

27+
private Button animationViewBtn;
2428

2529

2630
@Override
@@ -36,6 +40,7 @@ protected void onCreate(Bundle savedInstanceState) {
3640
animation_btn = (Button) findViewById(R.id.animation_btn);
3741
pay_psd_view_btn = (Button) findViewById(R.id.pay_psd_view_btn);
3842
progress_btn = (Button) findViewById(R.id.progress_btn);
43+
animationViewBtn = (Button) findViewById(R.id.animation_view_btn);
3944

4045
button_bubble.setOnClickListener(new View.OnClickListener() {
4146
@Override
@@ -86,6 +91,13 @@ public void onClick(View view) {
8691
startActivity(new Intent(MainActivity.this, ProgressBarActivity.class));
8792
}
8893
});
94+
animationViewBtn.setOnClickListener(new View.OnClickListener() {
95+
@Override
96+
public void onClick(View v) {
97+
startActivity(new Intent(MainActivity.this, AnimationViewActivity.class));
98+
}
99+
});
100+
89101

90102
}
91103

Collapse file

‎app/src/main/java/com/allen/androidcustomview/widget/BaseView.java‎

Copy file name to clipboard
+121Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package com.allen.androidcustomview.widget;
2+
3+
import android.content.Context;
4+
import android.graphics.Paint;
5+
import android.support.annotation.Nullable;
6+
import android.util.AttributeSet;
7+
import android.util.TypedValue;
8+
import android.view.View;
9+
10+
/**
11+
* Created by xiaoyao on 2017/5/23.
12+
*/
13+
14+
public class BaseView extends View {
15+
16+
public int mWidth;
17+
public int mHeight;
18+
public int mViewHeight;
19+
20+
21+
public BaseView(Context context) {
22+
this(context, null);
23+
24+
mViewHeight = dp2px(45);
25+
}
26+
27+
public BaseView(Context context, @Nullable AttributeSet attrs) {
28+
super(context, attrs);
29+
}
30+
31+
@Override
32+
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
33+
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
34+
int width = MeasureSpec.getSize(widthMeasureSpec);
35+
36+
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
37+
int height = MeasureSpec.getSize(heightMeasureSpec);
38+
39+
setMeasuredDimension(measureWidth(widthMode, width), measureHeight(heightMode, height));
40+
}
41+
42+
/**
43+
* 测量宽度
44+
*
45+
* @param mode
46+
* @param width
47+
* @return
48+
*/
49+
private int measureWidth(int mode, int width) {
50+
switch (mode) {
51+
case MeasureSpec.UNSPECIFIED:
52+
case MeasureSpec.AT_MOST:
53+
break;
54+
case MeasureSpec.EXACTLY:
55+
mWidth = width;
56+
break;
57+
}
58+
return mWidth;
59+
}
60+
61+
/**
62+
* 测量高度
63+
*
64+
* @param mode
65+
* @param height
66+
* @return
67+
*/
68+
private int measureHeight(int mode, int height) {
69+
switch (mode) {
70+
case MeasureSpec.UNSPECIFIED:
71+
case MeasureSpec.AT_MOST:
72+
mHeight = mViewHeight;
73+
break;
74+
case MeasureSpec.EXACTLY:
75+
mHeight = height;
76+
break;
77+
}
78+
return mHeight;
79+
}
80+
81+
/**
82+
* 统一处理paint
83+
* @param strokeWidth
84+
* @param color
85+
* @param style
86+
* @return
87+
*/
88+
public Paint getPaint(int strokeWidth, int color, Paint.Style style) {
89+
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
90+
paint.setStrokeWidth(strokeWidth);
91+
paint.setColor(color);
92+
paint.setAntiAlias(true);
93+
paint.setStrokeCap(Paint.Cap.ROUND);
94+
paint.setStyle(style);
95+
return paint;
96+
}
97+
98+
99+
/**
100+
* dp 2 px
101+
*
102+
* @param dpVal
103+
*/
104+
protected int dp2px(int dpVal) {
105+
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
106+
dpVal, getResources().getDisplayMetrics());
107+
}
108+
109+
/**
110+
* sp 2 px
111+
*
112+
* @param spVal
113+
* @return
114+
*/
115+
protected int sp2px(int spVal) {
116+
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
117+
spVal, getResources().getDisplayMetrics());
118+
119+
}
120+
121+
}
Collapse file

‎app/src/main/java/com/allen/androidcustomview/widget/FadeInTextView.java‎

Copy file name to clipboard
+162Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
package com.allen.androidcustomview.widget;
2+
3+
import android.animation.ValueAnimator;
4+
import android.content.Context;
5+
import android.graphics.Canvas;
6+
import android.graphics.Paint;
7+
import android.graphics.Rect;
8+
import android.support.annotation.Nullable;
9+
import android.util.AttributeSet;
10+
import android.util.Log;
11+
import android.view.animation.LinearInterpolator;
12+
import android.widget.TextView;
13+
14+
/**
15+
* Created by allen on 2017/6/5.
16+
* <p>
17+
* 字符串逐字显示的view
18+
*/
19+
20+
public class FadeInTextView extends TextView {
21+
22+
23+
private Rect textRect = new Rect();
24+
25+
private StringBuffer stringBuffer = new StringBuffer();
26+
27+
private String[] arr;
28+
29+
private int textCount;
30+
31+
private int currentIndex = -1;
32+
33+
/**
34+
* 每个字出现的时间
35+
*/
36+
private int duration = 300;
37+
private ValueAnimator textAnimation;
38+
39+
private TextAnimationListener textAnimationListener;
40+
41+
42+
public FadeInTextView setTextAnimationListener(TextAnimationListener textAnimationListener) {
43+
this.textAnimationListener = textAnimationListener;
44+
return this;
45+
}
46+
47+
public FadeInTextView(Context context) {
48+
this(context, null);
49+
}
50+
51+
public FadeInTextView(Context context, @Nullable AttributeSet attrs) {
52+
super(context, attrs);
53+
54+
}
55+
56+
@Override
57+
protected void onDraw(final Canvas canvas) {
58+
super.onDraw(canvas);
59+
if (stringBuffer != null) {
60+
drawText(canvas, stringBuffer.toString());
61+
}
62+
}
63+
64+
/**
65+
* 绘制文字
66+
*
67+
* @param canvas 画布
68+
*/
69+
private void drawText(Canvas canvas, String textString) {
70+
textRect.left = getPaddingLeft();
71+
textRect.top = getPaddingTop();
72+
textRect.right = getWidth() - getPaddingRight();
73+
textRect.bottom = getHeight() - getPaddingBottom();
74+
Paint.FontMetricsInt fontMetrics = getPaint().getFontMetricsInt();
75+
int baseline = (textRect.bottom + textRect.top - fontMetrics.bottom - fontMetrics.top) / 2;
76+
//文字绘制到整个布局的中心位置
77+
canvas.drawText(textString, getPaddingLeft(), baseline, getPaint());
78+
}
79+
80+
/**
81+
* 文字逐个显示动画 通过插值的方式改变数据源
82+
*/
83+
private void initAnimation() {
84+
85+
textAnimation = ValueAnimator.ofInt(0, textCount - 1);
86+
textAnimation.setDuration(textCount * duration);
87+
textAnimation.setInterpolator(new LinearInterpolator());
88+
textAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
89+
@Override
90+
public void onAnimationUpdate(ValueAnimator valueAnimator) {
91+
int index = (int) valueAnimator.getAnimatedValue();
92+
//过滤去重,保证每个字只重绘一次
93+
if (currentIndex != index) {
94+
stringBuffer.append(arr[index]);
95+
Log.d("allen1", "onAnimationUpdate: " + stringBuffer.toString());
96+
97+
currentIndex = index;
98+
if (currentIndex == (textCount - 1)) {
99+
if (textAnimationListener != null) {
100+
currentIndex = -1;
101+
textAnimationListener.animationFinish();
102+
}
103+
}
104+
105+
invalidate();
106+
}
107+
}
108+
});
109+
}
110+
111+
/**
112+
* 设置逐渐显示的字符串
113+
*
114+
* @param textString
115+
* @return
116+
*/
117+
public FadeInTextView setTextString(String textString) {
118+
if (textString != null) {
119+
textCount = textString.length();
120+
arr = new String[textCount];
121+
for (int i = 0; i < textCount; i++) {
122+
arr[i] = textString.substring(i, i + 1);
123+
}
124+
initAnimation();
125+
}
126+
127+
return this;
128+
}
129+
130+
/**
131+
* 开启动画
132+
*
133+
* @return
134+
*/
135+
public FadeInTextView startFadeInAnimation() {
136+
if (textAnimation != null) {
137+
stringBuffer.setLength(0);
138+
currentIndex = -1;
139+
textAnimation.start();
140+
}
141+
return this;
142+
}
143+
144+
/**
145+
* 停止动画
146+
*
147+
* @return
148+
*/
149+
public FadeInTextView stopFadeInAnimation() {
150+
if (textAnimation != null) {
151+
textAnimation.end();
152+
}
153+
return this;
154+
}
155+
156+
/**
157+
* 回调接口
158+
*/
159+
public interface TextAnimationListener {
160+
void animationFinish();
161+
}
162+
}

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.