首页 > 解决方案 > 无法在 onCreate() 方法内的 TextView 中实现 linerGradient()

问题描述

我想linearGradient()在我的 textView 中实现。我希望它以某种方式加载,以便在活动加载时将其linearGradient()应用于我的文本视图。我能够在按钮单击侦听器中做到这一点,但是每当我在方法中实现线性渐变时,它都不起作用onCreate()。下面是我的xml布局:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:textSize="70sp" />

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="63dp"
        android:text="Apply Gradient Text" />
</RelativeLayout>

MainActivity.java文件:

import android.app.Activity;
import android.graphics.Point;
import android.graphics.Shader;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {
    private TextView tv;
    private Button btn;
    private int mWidth;
    private int mHeight;
    private Shader shader;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv=(TextView)findViewById(R.id.tv);
        btn=(Button)findViewById(R.id.btn);
        btn.setOnClickListener(this);
        tv.setText(" Instagram  ");
        Typeface face=Typeface.createFromAsset(getAssets(), "fonts/billabong.ttf");
        tv.setTypeface(face);

    }

    public void onClick(View view) {
        mWidth=tv.getWidth();
        mHeight=tv.getHeight();
        Point size = new Point(mWidth,mHeight);
        GradientManager gm= new GradientManager(getApplicationContext(),size);
        shader=gm.getRandomLinearGradient();
        tv.setLayerType(View.LAYER_TYPE_SOFTWARE,null);
        tv.getPaint().setShader(shader);
    }
}

GradientManager.java文件:

import android.content.Context;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Point;
import android.graphics.Shader;


public class GradientManager {
    private Point mSize;

    public GradientManager(Context context, Point size){
        this.mSize = size;
    }

    protected LinearGradient getRandomLinearGradient(){

        LinearGradient gradient = new LinearGradient(0, 0, mSize.x, mSize.y,
                new int[] {Color.parseColor("#6656C8"), Color.parseColor("#8E33A9"),Color.parseColor("#BB328C"), Color.parseColor("#ED4B3E"),
                        Color.parseColor("#FA8031"), Color.parseColor("#FEC65C"), Color.parseColor("#FFD374") },null,
                Shader.TileMode.MIRROR
        );
        return gradient;
    }
}

活动开始时的屏幕截图: 图 1

按下按钮时的屏幕截图: 图 2

下面给出了我想要实现的内容(我删除了按钮):

MainActivity.java文件:

import android.app.Activity;
import android.graphics.Point;
import android.graphics.Shader;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {
    private TextView tv;
    private int mWidth;
    private int mHeight;
    private Shader shader;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv=(TextView)findViewById(R.id.tv);
        tv.setText(" Instagram  ");
        Typeface face=Typeface.createFromAsset(getAssets(), "fonts/billabong.ttf");
        tv.setTypeface(face);
        mWidth=tv.getWidth();
        mHeight=tv.getHeight();
        Point size = new Point(mWidth,mHeight);
        GradientManager gm= new GradientManager(getApplicationContext(),size);
        shader=gm.getRandomLinearGradient();
        tv.setLayerType(View.LAYER_TYPE_SOFTWARE,null);
        tv.getPaint().setShader(shader);       
    }   
}

GradientManager.java的内容没有改变,只有按钮从我的布局中删除。上面的代码没有显示任何渐变颜色,而是用线性渐变颜色参数[Color.parseColor("#6656C8")]的第一种颜色显示整个textView。

这是我的截图: 图 3

有人可以帮我写代码吗?我错过了什么?任何帮助表示赞赏。

标签: androidtextviewoncreatelinear-gradients

解决方案


您在 onCreate 期间使用 tv.getWidth 和 getHeight。那时 TextView 尚未测量,因此这些值无效。如果你必须得到渐变的宽度和高度,你应该在测量步骤完成后应用你的渐变(即使用ongloballayoutlistener)。


推荐阅读