无法在onCreate()方法内的TextView中实现LinerGradient()

时间:2018-07-15 08:17:17

标签: android textview oncreate linear-gradients

我想在我的textView中实现linearGradient()。我希望以某种方式使活动加载时,linearGradient()会应用到我的textview中。我能够在按钮单击侦听器中做到这一点,但是只要在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;
    }
}

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

按下按钮时的屏幕截图Image 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。

这是我的屏幕截图: Image 3

有人可以帮助我提供代码吗?我想念什么?任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

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