我用这个代码
TextView.animate().setDuration(0).scaleX(scale).scaleY(scale).start();
当缩放值增加到某个点时,它会使 TextView 消失。我不知道为什么,以及如何预防?
[编辑]附上我的测试代码
[test_text_scale.xml] TestTextScale.java的Layout 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 + "");
}
}
最佳答案
这可能是因为 TextView 超出了其父 View 的布局边界。为了克服这个问题,简单地将 android:clipChildren="false"
放在 textview 的直接父级的 xml 描述中。您可能还需要为他们的 parent 做这件事。
编辑: 在自己测试代码后,我发现 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 的硬件加速,它可能会失去抗锯齿。
关于java - TextView 和缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38699712/