Skip to content

Commit

Permalink
Merge pull request #8 from walfud/master
Browse files Browse the repository at this point in the history
Compile & Memory Optimization
  • Loading branch information
matthewrkula committed Jun 3, 2015
2 parents 920f904 + 46e51c3 commit c62c38f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/com/mattkula/secrettextview/DemoActivity.java
Expand Up @@ -23,7 +23,7 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.main);

secretTextView = (SecretTextView)findViewById(R.id.textview);
secretTextView.setmDuration(3000);
secretTextView.setDuration(3000);
secretTextView.setIsVisible(true);
secretTextView.setOnClickListener(new View.OnClickListener() {
@Override
Expand Down
29 changes: 29 additions & 0 deletions src/com/mattkula/secrettextview/MutableForegroundColorSpan.java
@@ -0,0 +1,29 @@
package com.mattkula.secrettextview;

import android.text.TextPaint;
import android.text.style.CharacterStyle;
import android.text.style.UpdateAppearance;

/**
* Created by walfud on 2015/5/29.
*/
public class MutableForegroundColorSpan extends CharacterStyle
implements UpdateAppearance {

public static final String TAG = "MutableForegroundColorSpan";

private int mColor;

@Override
public void updateDrawState(TextPaint tp) {
tp.setColor(mColor);
}

public int getColor() {
return mColor;
}

public void setColor(int color) {
this.mColor = color;
}
}
21 changes: 13 additions & 8 deletions src/com/mattkula/secrettextview/SecretTextView.java
Expand Up @@ -5,7 +5,6 @@
import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.widget.TextView;

Expand All @@ -17,6 +16,7 @@ public class SecretTextView extends TextView {
private SpannableString mSpannableString;

private double[] mAlphas;
private MutableForegroundColorSpan[] mSpans;
private boolean mIsVisible;
private boolean mIsTextResetting = false;
private int mDuration = 2500;
Expand Down Expand Up @@ -78,13 +78,12 @@ private void resetSpannableString(double percent){
mIsTextResetting = true;

int color = getCurrentTextColor();
for(int i=0; i < mSpannableString.length(); i++){
mSpannableString.setSpan(
new ForegroundColorSpan(Color.argb(clamp(mAlphas[i] + percent), Color.red(color), Color.green(color), Color.blue(color))), i, i+1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
for (int i = 0; i < this.mTextString.length(); i++) {
MutableForegroundColorSpan span = mSpans[i];
span.setColor(Color.argb(clamp(mAlphas[i] + percent), Color.red(color), Color.green(color), Color.blue(color)));
}

setText(mSpannableString);
invalidate();

mIsTextResetting = false;
}
Expand All @@ -100,6 +99,12 @@ private void resetIfNeeded(){
if (!mIsTextResetting){
mTextString = getText().toString();
mSpannableString = new SpannableString(this.mTextString);
mSpans = new MutableForegroundColorSpan[this.mTextString.length()];
for (int i = 0; i < this.mTextString.length(); i++) {
MutableForegroundColorSpan span = new MutableForegroundColorSpan();
mSpannableString.setSpan(span, i, i+1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
mSpans[i] = span;
}
resetAlphas(mTextString.length());
resetSpannableString(mIsVisible ? 2.0f : 0);
}
Expand All @@ -120,8 +125,8 @@ private int clamp(double f){
return (int)(255*Math.min(Math.max(f, 0), 1));
}

public void setDuration(int mDuration){
this.mDuration = mDuration;
animator.setDuration(mDuration);
public void setDuration(int duration){
this.mDuration = duration;
animator.setDuration(duration);
}
}

0 comments on commit c62c38f

Please sign in to comment.