Search code examples
javaandroidtextviewscale

TextView and scale


I use this code

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

When scale value increase to some point, it make TextView disappear. I dont know why, and how to prevent it?

[Edit] Attach my test code

[test_text_scale.xml] It is Layout xml file for TestTextScale.java

<?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 + "");
    }
}

Solution

  • This may happen because the textview exceeds the layout bounds of its parent. To overcome this, simple put android:clipChildren="false" in the xml description of the immediate parent of the textview. You may also need to do it for their parents too.

    EDIT: Upon testing the code myself, I found that the textview did disappear with following log message:

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

    This is due to a possible issue in the Android Hardware acceleration modules. One of the ways to avoid this is:

    mTvTestZoom.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    

    This works well and the textView does not disappear after a certain size but, since it disables hardware acceleration for the specific textview, it may lose antialiasing.