【问题标题】:Cannot implement linerGradient() in TextView inside onCreate() method无法在 onCreate() 方法内的 TextView 中实现 linerGradient()
【发布时间】:2018-07-15 08:17:16
【问题描述】:

我想在我的 textView 中实现 linearGradient()。我希望它以某种方式在活动加载时,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;
    }
}

活动开始时的屏幕截图:

按下按钮时的屏幕截图:

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

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。

这是我的截图:

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

【问题讨论】:

    标签: android textview oncreate linear-gradients


    【解决方案1】:

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

    【讨论】:

    • 实际上我需要一种方法来在 onCreate() 中应用这样的线性渐变。我到处搜索,但我没有找到一个。所以我不得不输入硬编码值而不是 tv.width 和 tv.height。你能给我一个实现相同的解决方案吗?这样我就可以得到 set Text() 中给出的任何 textview 文本的长度和宽度。即使我在布局 xml 文件中放入一些值,此代码也不起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-03
    • 1970-01-01
    • 2019-02-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多