我在TextView
内有一个RelativeLayout
,由于某种原因,它会向左轻微移动,然后返回原来的位置。它只发生在几分之一秒,但它是明显和恼人的。它似乎也是随机发生的。
我不知道它是什么,但几乎尝试过所有事情。
使用新字符每200毫秒更新一次,并在onDraw()
方法上绘制线条。
private Handler mHandler = new Handler();
private LinesView methodView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){
................
//Set up TextView to display the method
methodView = (LinesView) v.findViewById(R.id.MC_title);
methodView.setNumberOfBells(method.getPlayingOn());
methodView.setTextSize(24);
methodView.setTypeface(Typeface.MONOSPACE);
methodView.setText(" ");
methodView.setTextColor(Color.BLACK);
methodView.setBackgroundColor(Color.WHITE);
methodView.setClickable(false);
.....................
public class DisplayMethod extends AsyncTask<Void,Void,Void>{
@Override
protected synchronized Void doInBackground(Void... arg0) {
...........................
mHandler.post(new Runnable() {
@Override
public void run() {
methodView.clearText();
//Ensures the view draws the lines
methodView.drawLines(true);
//Sets which bell the user is playing
methodView.setBell(bellNumberTextViews.get(bellNumberTextViews.size() - 1).getText().toString());
}
});
........................
//Displays the correct amount of text on screen
methodView.setLimitingText(method.getPlayingOn(), cText, 6);
.........................
}
}
这是我正在使用的视图
public class LinesView extends TextView {
String bell = "2";
int bells;
public boolean lines = false;
Paint paint1 = new Paint();
Paint paint2 = new Paint();
public LinesView(Context context){
super(context);
}
public LinesView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public LinesView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void setBell(String bell){
this.bell = bell;
}
public void setNumberOfBells(int bell){
this.bells = bell;
}
public void clearText(){
super.setText("");
}
public void setLimitingText(int bells, String text, int lines){
int characters_allowed = ((bells + 1) * (lines)) - 1;
if (text.length() > characters_allowed){
text = text.substring(text.length() - characters_allowed, text.length());
text = text.substring(1 + text.indexOf("\n"), text.length());
}
super.setText(text);
}
public void drawLines(boolean lines){
this.lines = lines;
super.setText(super.getText().toString());
}
public boolean getdrawLines(){
return lines;
}
@Override
public void onDraw(Canvas canvas){
if (lines){
paint2.setAntiAlias(true);
paint2.setStrokeWidth(8f);
paint2.setColor(Color.BLUE);
paint1.setAntiAlias(true);
paint1.setStrokeWidth(8f);
paint1.setColor(Color.RED);
String a = (String) super.getText().toString();
int index2 = a.indexOf(bell);
int index1 = a.indexOf("1");
int next2 = 0;
int next1 = 0;
float y = Utils.dpToPx(16, getContext());
while (a.length() > bells){
a = a.substring(bells + 1, a.length());
next2 = a.indexOf(bell);
next1 = a.indexOf("1");
if (next2 != -1)
canvas.drawLine(Utils.dpToPx(6f, getContext()) + index2 * 27f, y - Utils.dpToPx(2, getContext()),
Utils.dpToPx(6f, getContext()) + next2 * 27f, y + Utils.dpToPx(27, getContext()), paint2);
if (next1 != -1)
canvas.drawLine(Utils.dpToPx(6f, getContext()) + index1 * 27f, y - Utils.dpToPx(2, getContext()),
Utils.dpToPx(6f, getContext()) + next1 * 27f, y + Utils.dpToPx(27, getContext()), paint1);
y = y + Utils.dpToPx(27.6f, getContext());
index2 = next2;
index1 = next1;
}
}
super.onDraw(canvas);
}
}
这是xml布局
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="160dp" >
<project.android.bellringing.views.LinesView
android:id="@+id/MC_title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerInParent="true"/>
</RelativeLayout>
答案 0 :(得分:1)
我认为这里的问题是TextView
的宽度为wrap_content
。这意味着它将根据其内容进行拉伸/缩小。如果你说TextView
每更新200毫秒,可能会有一段时间它必须进行拟合操作。
我会这样做:
layout_width
更改为match_parent
,看看为什么会发生这种情况。200
毫秒(0.2秒)是一个非常短的时间来查看实际发生的事情。尝试将其设置为更大的延迟(比如5000毫秒),看看你是否可以区分它发生的原因。TextView
的大小。