您的位置:首页 > 其它

自定义EditText(Fly)

2016-06-14 16:51 387 查看
想知道edittext里的内容 中的某个字的位只要取得TextView 的layout就可以了  layout里面包

含各种获取字符位置行数 列数 等的 方法不过你要先知道你需要的字符在TextView 是第几个

字符  然后用layout.getLineForOffset得到所在行数 再用 layout.getLineBounds得到 这一行

的外包矩形(Rect) 这样 这个字符的顶部Y坐标就是rect的top 底部Y坐标就是rect的bottom

要得到这个字符的左边X坐标 用layout.getPrimaryHorizontal 得到字符的右边X坐标用layout.getSecondaryHorizontal

         int position = 第几个字符
Layout layout = textView.getLayout();Rect bound = new Rect();
int line = layout.getLineForOffset(position);

       layout.getLineBounds(line, bound);

        yAxisTop = bound.top;//字符顶部y坐标
yAxisBottom = bound.bottom;//字符底部y坐标

        xAxisLeft = layout.getPrimaryHorizontal(position);//字符左边x坐标

       xAxisRight = layout.getSecondaryHorizontal(position);//字符右边x坐标

<span style="font-size:18px;">

public class FlyEditText extends EditText {
private ViewGroup contentContainer;
private int height;
private String cacheStr = "";
private Context mContext;
private TextPaint mPaint;
private Paint sPaint;

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

}

public FlyEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, android.R.attr.editTextStyle);
init();
setlistener();
mContext = context;
getDefaultScreenWH();
mPaint = new TextPaint();
mPaint.setColor(Color.BLUE);
mPaint.setStyle(Style.FILL);
mPaint.setTextSize(30);

sPaint = new TextPaint();
sPaint.setColor(Color.BLUE);
sPaint.setStyle(Style.FILL);
sPaint.setTextSize(50);

}

@Override
public void setInputType(int type) {

super.setInputType(InputType.TYPE_DATETIME_VARIATION_DATE);
}

public FlyEditText(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
//为了修改隐藏后的字体 不执行onTextChanged里面代码
boolean isShow=true;
private void setlistener() {
addTextChangedListener(new TextWatcher() {
private String temp;
private String TAG;

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
int len=s.length();

if(!isShow){
return;
}
if (cacheStr.length() < s.length()) {
char last = s.charAt(s.length() - 1);
update(last, true,s);
} else {
char last = cacheStr.charAt(cacheStr.length() - 1);
update(last, false,s);
}

cacheStr= s.toString();

}

@Override
public void afterTextChanged(Editable s) {

}
});
}

private int screenWidth;
private int screenHeight;
private Random random;

private void getDefaultScreenWH() {
WindowManager wm = (WindowManager) mContext
.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics metrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(metrics);
screenWidth = metrics.widthPixels;
screenHeight = metrics.heightPixels;
random= new Random();
}
@Override
protected void onDraw(Canvas canvas) {

super.onDraw(canvas);
}

private Rect mBounds = new Rect();
private String info;

private String TAG;

// @Override
// protected void onDraw(Canvas canvas) {
// calculateStringHeight(cacheStr);
// canvas.translate(0, 10);
// StaticLayout myStaticLayout = new StaticLayout(cacheStr, mPaint,
// canvas.getWidth(), Alignment.ALIGN_NORMAL, 1.0f, 2f, false);
// myStaticLayout.draw(canvas);
//// if(isShow)
//// canvas.drawText("|",0, mBounds.height(), sPaint);
//
// }

private void update(char last, final boolean isUp,final CharSequence all) {
final TextView textView = new TextView(getContext());

isShow=false;

textView.setTextColor(getResources().getColor(android.R.color.black));
textView.setTextSize(30);
textView.setText(String.valueOf(last));
textView.setGravity(Gravity.CENTER);
contentContainer.addView(textView, new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
textView.measure(0, 0);

int pos = getSelectionStart();
Layout layout = getLayout();

int line = layout.getLineForOffset(pos);
int baseline = layout.getLineBaseline(line);
int ascent = layout.getLineAscent(line);

float startX = 0;
float startY = 0;
float endX = 0;
float endY = 0;

if (isUp) {
//隐藏字体
if(all.length()>1){
setText(all.subSequence(0, all.length()-1));
setSelection(all.length()-1);

}else {
setText("");
}

startX = layout.getPrimaryHorizontal(pos) + 50;
startY = height / 3 * 2;
endX = startX;
endY = baseline + ascent;
} else {
//上到下 末端X坐标
endX = new Random().nextInt(contentContainer.getWidth());
endY = height / 3 * 2;
startX = layout.getPrimaryHorizontal(pos) + 70;
startY = baseline + ascent;
}

final AnimatorSet animSet = new AnimatorSet();
ObjectAnimator animX = ObjectAnimator.ofFloat(textView, "translationX",
startX, endX);
ObjectAnimator animY = ObjectAnimator.ofFloat(textView, "translationY",
startY, endY);
ObjectAnimator scaleX = ObjectAnimator.ofFloat(textView, "scaleX",
0.6f, 1.2f);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(textView, "scaleY",
0.6f, 1.0f);
int r=random.nextInt(360)+180;
ObjectAnimator rotatex = ObjectAnimator.ofFloat(textView, "rotation",
0, 300);

animY.setInterpolator(new DecelerateInterpolator());
animSet.setDuration(600);
animSet.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationStart(Animator animation) {
}

@Override
public void onAnimationEnd(Animator animation) {
setText(all);
setSelection(all.length());
isShow=true;
if(!isUp)
//如果不是删除 向上飞的话 就不删除textview 以便于 返回动画
contentContainer.removeView(textView);
}
});
animSet.playTogether(animX, animY, scaleX, scaleY,rotatex);
animSet.start();

if(isUp){
final AnimatorSet animSet2 = new AnimatorSet();
ObjectAnimator animXX = ObjectAnimator.ofFloat(textView, "translationX",
endX, startX-25);
ObjectAnimator animYY = ObjectAnimator.ofFloat(textView, "translationY",
endY, baseline);
ObjectAnimator scaleXX = ObjectAnimator.ofFloat(textView, "scaleX",
1.0F, 0.7f);
ObjectAnimator scaleYY = ObjectAnimator.ofFloat(textView, "scaleY",
1.0F, 0.7f);

ObjectAnimator rotatexx = ObjectAnimator.ofFloat(textView, "rotation",
50, 0);

animSet2.setDuration(300);
animSet2.setStartDelay(600);
animSet2.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationStart(Animator animation) {
}

@Override
public void onAnimationEnd(Animator animation) {

contentContainer.removeView(textView);
}
});
animSet2.playTogether(animXX, animYY, scaleXX, scaleYY,rotatexx);
animSet2.start();}}

private void init() {
contentContainer = (ViewGroup) ((MainActivity) getContext())
.findViewById(android.R.id.content);
contentContainer.setBackgroundColor(color.white);
WindowManager windowManager = (WindowManager) getContext()
.getSystemService(Context.WINDOW_SERVICE);
height = windowManager.getDefaultDisplay().getHeight();
}

}
</span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: