【问题标题】:TextView and scaleTextView 和缩放
【发布时间】:2016-08-01 13:05:15
【问题描述】:

我用这个代码

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

【问题讨论】:

  • 它超出了其容器的布局边界。可能。

标签: java android textview scale


【解决方案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 的硬件加速,它可能会失去抗锯齿功能。

【讨论】:

  • 我试过你的建议了,还是不行我已经附上我的测试代码了,非常感谢(T-T)
猜你喜欢
  • 1970-01-01
  • 2015-04-28
  • 1970-01-01
  • 1970-01-01
  • 2019-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多