默认情况下,Android没有提供将辉光添加到文本视图或任何其他视图的简单方法,因此,为了使我们的应用更具吸引力和美观,我们可以将辉光效果添加到我们的TextView中。为此,我们可以使用许多不同的外部库。下面给出了一个示例GIF,以了解我们将在本文中做些什么。注意,我们将使用Java语言实现该项目。
方法
步骤1:创建一个新项目
要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。请注意,选择Java作为编程语言。
第2步:在进入编码部分之前,请先执行一些预任务
转到应用程序-> res->值-> colors.xml文件,然后设置应用程序的颜色。
XML
#0F9D58
#0F9D58
#05af9b
#ffffff
XML
Java
// array of different colors
int[] colors={Color.RED,Color.GREEN,Color.BLACK,Color.CYAN,Color.DKGRAY,Color.GRAY,Color.LTGRAY,Color.BLUE,Color.WHITE,Color.YELLOW,Color.MAGENTA};
Java
// getting Glow seekBar reference
SeekBar seekBarGlow =(SeekBar)findViewById(R.id.seekBarGlow);
// getting Color seekBar reference
SeekBar seekBarColor =(SeekBar)findViewById(R.id.seekBarColor);
// setting the max of seekBar to color length -1
seekBarColor.setMax(colors.length-1);
// getting glowTextView reference
GlowTextView glowTextView =(GlowTextView)findViewById(R.id.glowTextView);
Java
// seekBar change listener for changing the glow radius
seekBarGlow.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// change the glow radius of the glow text view
glowTextView.setGlowRadius(progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
// seekBar change listener for changing color
seekBarColor.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// change the glow color of the glow text view
glowTextView.setGlowColor(colors[progress]);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
Java
import android.graphics.Color;
import android.os.Bundle;
import android.widget.SeekBar;
import androidx.appcompat.app.AppCompatActivity;
import com.riningan.widget.GlowTextView;
public class MainActivity extends AppCompatActivity {
// array of different colors
int[] colors = {Color.RED, Color.GREEN, Color.BLACK, Color.CYAN, Color.DKGRAY, Color.GRAY, Color.LTGRAY, Color.BLUE, Color.WHITE, Color.YELLOW, Color.MAGENTA};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// getting Glow seekBar reference
SeekBar seekBarGlow = (SeekBar) findViewById(R.id.seekBarGlow);
// getting Color seekBar reference
SeekBar seekBarColor = (SeekBar) findViewById(R.id.seekBarColor);
// setting the max of seekBar to color length -1
seekBarColor.setMax(colors.length - 1);
// getting glowTextView reference
GlowTextView glowTextView = (GlowTextView) findViewById(R.id.glowTextView);
// seekBar change listener for changing the glow radius
seekBarGlow.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// change the glow radius of the glow text view
glowTextView.setGlowRadius(progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
// seekBar change listener for changing color
seekBarColor.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// change the glow color of the glow text view
glowTextView.setGlowColor(colors[progress]);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
}
转到Gradle脚本> build.gradle (模块:应用程序)部分,导入以下依赖项,然后在上面的弹出窗口中单击“立即同步”。
// Adding glowtextview
implementation ‘com.riningan.widget:glowtextview:1.0’
步骤3:设计UI
在activity_main.xml中,删除默认的“文本视图”,然后将布局更改为“相对”布局,并添加GlowTextView ,我们还添加2个SeekBar来更改发光文本视图的颜色和半径,如下所示。以下是activity_main.xml文件的代码。
XML格式
特性:
- app:glowRadius :用于设置发光半径[默认值60f]
- app:glowColor :用于设置发光颜色[默认颜色值WHITE]
步骤4:编码部分
打开MainActivity。在Java文件和类中,我们创建了一个整数数组来存储不同的颜色(稍后将使用它来更改GlowTextView的颜色),如下所示
Java
// array of different colors
int[] colors={Color.RED,Color.GREEN,Color.BLACK,Color.CYAN,Color.DKGRAY,Color.GRAY,Color.LTGRAY,Color.BLUE,Color.WHITE,Color.YELLOW,Color.MAGENTA};
现在,在onCreate()中获取GlowTextView和2 SeekBar的引用,并将颜色SeekBar的最大值设置为colors.length – 1 ,如下所示。
Java
// getting Glow seekBar reference
SeekBar seekBarGlow =(SeekBar)findViewById(R.id.seekBarGlow);
// getting Color seekBar reference
SeekBar seekBarColor =(SeekBar)findViewById(R.id.seekBarColor);
// setting the max of seekBar to color length -1
seekBarColor.setMax(colors.length-1);
// getting glowTextView reference
GlowTextView glowTextView =(GlowTextView)findViewById(R.id.glowTextView);
现在创建一个SeekBarChangeListener ,同时包含SeekBar和onProgressChanged(SeekBar seekBar,int progress,boolean fromUser)内部,将GlowColor和GlowRadius值设置为该过程,如下所示
Java
// seekBar change listener for changing the glow radius
seekBarGlow.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// change the glow radius of the glow text view
glowTextView.setGlowRadius(progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
// seekBar change listener for changing color
seekBarColor.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// change the glow color of the glow text view
glowTextView.setGlowColor(colors[progress]);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
以下是MainActvity的完整代码。 Java文件。
Java
import android.graphics.Color;
import android.os.Bundle;
import android.widget.SeekBar;
import androidx.appcompat.app.AppCompatActivity;
import com.riningan.widget.GlowTextView;
public class MainActivity extends AppCompatActivity {
// array of different colors
int[] colors = {Color.RED, Color.GREEN, Color.BLACK, Color.CYAN, Color.DKGRAY, Color.GRAY, Color.LTGRAY, Color.BLUE, Color.WHITE, Color.YELLOW, Color.MAGENTA};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// getting Glow seekBar reference
SeekBar seekBarGlow = (SeekBar) findViewById(R.id.seekBarGlow);
// getting Color seekBar reference
SeekBar seekBarColor = (SeekBar) findViewById(R.id.seekBarColor);
// setting the max of seekBar to color length -1
seekBarColor.setMax(colors.length - 1);
// getting glowTextView reference
GlowTextView glowTextView = (GlowTextView) findViewById(R.id.glowTextView);
// seekBar change listener for changing the glow radius
seekBarGlow.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// change the glow radius of the glow text view
glowTextView.setGlowRadius(progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
// seekBar change listener for changing color
seekBarColor.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// change the glow color of the glow text view
glowTextView.setGlowColor(colors[progress]);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
}
输出: