2014-02-11 87 views
0

我有一個TextViewRelativeLayout之內,它出於某種原因向左輕微移動,然後返回到原來的位置。它只發生在幾分之一秒內,但它顯而易見並令人討厭。它也似乎隨機發生。自定義TextView輕微移動

我不知道它是什麼,但幾乎都嘗試過。

它每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

你可以粘貼你的'TextView'佈局,還有'RelativeLayout'嗎? – nKn

回答

1

我覺得這裏的問題是,你的TextView的寬度是wrap_content。這意味着它會根據其內容的功能進行伸縮。如果你說每200毫秒更新一次TextView,那麼可能有一段時間它必須進行擬合操作。

這就是我想要做的:

  • 更改layout_widthmatch_parent,看看這是爲什麼發生。
  • 200毫秒(0.2秒)是非常低的時間來查看實際發生的事情。嘗試將其設置爲更大的延遲(例如5000毫秒),然後查看是否可以區分它發生的原因。
  • 如果沒有,請嘗試使用調試器,並逐步查看實際上是否使您的TextView調整大小。