Skip to content

Instantly share code, notes, and snippets.

@JohnJocoo
Created February 2, 2013 23:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JohnJocoo/d47e8eafb1bcff7c8db1 to your computer and use it in GitHub Desktop.
Save JohnJocoo/d47e8eafb1bcff7c8db1 to your computer and use it in GitHub Desktop.
package my.package.view;
import my.package.MyActivity;
import my.package.MyUtils.FloatingText;
import my.package.R;
import android.content.Context;
import android.text.SpannableString;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.widget.TextView;
public class BoardPostText extends TextView implements View.OnClickListener, View.OnLongClickListener {
private boolean full_mode = false;
private boolean lock = false;
private int max_h = 0;
private int hidden_h = 0;
public BoardPostText(Context context) {
super(context);
prepareMe();
}
public BoardPostText(Context context, AttributeSet attrs) {
super(context, attrs);
prepareMe();
}
public BoardPostText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
prepareMe();
}
private void prepareMe() {
setOnClickListener(this);
setOnLongClickListener(this);
max_h = getResources().getDimensionPixelSize(R.dimen.post_image_max_height);
}
class ExpandAnimation extends Animation {
ExpandAnimation() {
super();
setDuration((long) getResources().getDimension(R.dimen.text_expand_speed));
}
ExpandAnimation(Context context, AttributeSet attrs) {
super(context, attrs);
setDuration((long) getResources().getDimension(R.dimen.text_expand_speed));
}
@Override
protected void applyTransformation(float it, Transformation t) {
if(!lock) return;
int new_h = 0;
if (!full_mode) {
new_h = (int) (max_h + hidden_h*it);
} else {
new_h = (int) (max_h + hidden_h*(1.0f - it));
}
//getLayoutParams().height = new_h;
setMaxHeight(new_h);
if(it >= 1.0f) {
full_mode = !full_mode;
lock = false;
//postInvalidateDelayed(100);
/* so here some other things, i've tried
postDelayed( new Runnable() {
@Override
public void run() {
requestLayout();
}
}, 100 );*/
/*
setGravity(Gravity.TOP|Gravity.CLIP_VERTICAL|Gravity.FILL_HORIZONTAL);
requestLayout();*/
}
}
@Override
public boolean willChangeBounds() {
return true;
}
};
public void onClick(View view) {
if(lock)
return;
lock = true;
hidden_h = (getLineCount()+2)*getLineHeight() - max_h;
if(hidden_h<=0)
return;
startAnimation(new ExpandAnimation());
}
public void lock() {
lock = true;
}
public void unlock() {
lock = false;
}
public void shrink() {
stopAnimation();
lock = false;
full_mode = false;
setMaxHeight(max_h);
//getLayoutParams().height = LayoutParams.WRAP_CONTENT;
requestLayout();
}
public void expand() {
stopAnimation();
lock = true;
full_mode = true;
setMaxHeight(Integer.MAX_VALUE);
//getLayoutParams().height = LayoutParams.WRAP_CONTENT;
requestLayout();
}
private void stopAnimation() {
if(MyActivity.android_ver<8)
return;
Animation anim = getAnimation();
if(anim!=null)
anim.cancel();
}
public boolean isFullMode() {
return full_mode;
}
public void setMargin(int width, int height) {
if(MyActivity.android_ver<8)
return;
int lines = (int)Math.ceil( ((float)height)/getLineHeight() );
CharSequence text = getText();
if(text!=null && (text instanceof SpannableString)) {
FloatingText[] fl_text = ((SpannableString)text).getSpans(0, text.length(), FloatingText.class);
if( fl_text.length>0 ) {
fl_text[0].setMargin(lines, width);
postInvalidate();
}
}
}
@Override
public boolean onLongClick(View v) {
return ((View)getParent()).performLongClick();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment