2016-08-01 41 views
1

我使用此代碼的TextView和規模

TextView.animate().setDuration(0).scaleX(scale).scaleY(scale).start(); 

當刻度值上升到某一點上,它使TextView的消失。我不知道爲什麼,以及如何預防它?

[編輯]附上我的測試代碼

[test_text_scale.xml]這是TestTextScale.java

佈局xml文件
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:clipChildren="false" 
    android:orientation="vertical"> 

    <TextView 
     android:id="@+id/tv_test_scale" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:text="Test" /> 

    <Button 
     android:id="@+id/bt_up" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_toEndOf="@+id/bt_down" 
     android:layout_toRightOf="@+id/bt_down" 
     android:text="Up" /> 

    <Button 
     android:id="@+id/bt_down" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:text="Down" /> 

</RelativeLayout> 

[TestTextScale.java]

import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 


public class TestTextScale extends AppCompatActivity { 

    // View 
    private TextView mTvTestZoom; 
    private Button mBtUp, mBtDown; 

    // Model 
    private int mCurrentScale = 1; 

    // OnClick listener 
    private View.OnClickListener mOnClickListener = new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      switch (v.getId()) { 

       case R.id.bt_up : { 
        upScale(); 
        setText(); 
       } break; 

       case R.id.bt_down : { 
        downScale(); 
        setText(); 
       } break; 
      } 
     } 
    }; 

    @Override 
    protected void onCreate(@Nullable Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.test_text_scale); 

     initView(); 
     initEvent(); 
    } 

    private void initView() { 

     mTvTestZoom = (TextView) findViewById(R.id.tv_test_scale); 
     mBtUp = (Button) findViewById(R.id.bt_up); 
     mBtDown = (Button) findViewById(R.id.bt_down); 

    } 

    private void initEvent() { 
     mBtDown.setOnClickListener(mOnClickListener); 
     mBtUp.setOnClickListener(mOnClickListener); 
    } 

    private void upScale() { 
     mCurrentScale += 1; 
     mTvTestZoom.animate().setDuration(0).scaleX(mCurrentScale).scaleY(mCurrentScale).start(); 
    } 

    private void downScale() { 
     mCurrentScale -= 1; 
     mTvTestZoom.animate().setDuration(0).scaleX(mCurrentScale).scaleY(mCurrentScale).start(); 
    } 

    private void setText() { 
     mTvTestZoom.setText(mCurrentScale + ""); 
    } 
} 
+1

它超出了其容器的佈局範圍。有可能。 – Shaishav

回答

1

這可能因爲textview超出了其父級的佈局範圍。爲了克服這個問題,簡單的把android:clipChildren="false"放在textview直接父級的xml描述中。你也可能需要爲他們的父母做。

編輯: 在測試自己的代碼,我發現,TextView的並具有以下日誌消息消失:

E/OpenGLRenderer: Font size too large to fit in cache. width, height = 635, 1028 

這是由於在Android硬件加速模塊可能的問題。一來避免這種情況的方法是:

mTvTestZoom.setLayerType(View.LAYER_TYPE_SOFTWARE, null); 

這種運作良好,並TextView的一定規模後不會消失,但是,因爲它禁止對特定TextView的硬件加速,它可能會失去抗鋸齒。

+0

我試過了你的建議,但它不起作用我已經附上了我的測試代碼,非常感謝(T- T) –

+0

@toptaps好的,讓我看看。 – Shaishav