在许多android应用程序中,您将看到一个功能,其中您可以看到在ImageView内显示一个简单的文本,或者可以看到一个文本以特定的图像或形状显示。通常,这种视图类型是在Android设备上提供的“联系人”应用程序中看到的。在该应用程序中,您将在圆形图像视图中看到每个联系人姓名的首字母。在本文中,我们将研究在我们的Android应用程序中创建相同类型的视图。
我们将在本文中构建什么?
我们将构建一个简单的应用程序,在该应用程序中,我们将使用可绘制的文本在Android的图像视图中显示一个简单的文本。我们将以不同的形状显示文本。下面是屏幕快照,在其中我们将看到我们将要构建的内容。
分步实施
步骤1:创建一个新项目
要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。请注意,选择Java作为编程语言。
步骤2:添加依赖项和JitPack存储库
导航到Gradle脚本> build.gradle(Module:app)并将以下依赖项添加到“依赖项”部分。
implementation ‘com.amulyakhare:com.amulyakhare.textdrawable:1.0.1’
将JitPack存储库添加到您的构建文件中。将其添加到allprojects {}部分中存储库末尾的根build.gradle中。
allprojects {
repositories {
…
maven { url ‘http://dl.bintray.com/amulyakhare/maven’ }
}
}
添加此依赖项后,同步您的项目,现在我们将继续执行它。
步骤3:使用activity_main.xml文件
导航到应用程序> res>布局> activity_main.xml,然后将以下代码添加到该文件中。以下是activity_main.xml文件的代码。
XML
Java
import android.os.Bundle;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
import com.amulyakhare.textdrawable.TextDrawable;
public class MainActivity extends AppCompatActivity {
// creating a variable for image view.
private ImageView tileIV, borderIV, circleIV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// on below line we are initializing our image view
tileIV = findViewById(R.id.idIVTile);
borderIV = findViewById(R.id.idIVBorder);
circleIV = findViewById(R.id.idIVCircle);
// on below line we are creating a new text drawable
TextDrawable tileImg = TextDrawable.builder()
// begin config method is use to start the config.
.beginConfig()
// on below line we are setting width and height for our drawable.
.width(130) // width in px
.height(130) // height in px
// on below line we are ending the config.
.endConfig()
// as we are building a rectangle we are using
// a build rect method to create a new rectangle
// and inside that we are passing the text
// as G and color for the drawable.
.buildRect("G", getResources().getColor(R.color.purple_200));
tileIV.setImageDrawable(tileImg);
// below text drawable is for round rectangle
TextDrawable roundRect = TextDrawable.builder().beginConfig()
.width(130) // width in px
.height(130) // height in px
.endConfig()
// as we are building a rectangle with round corners we are calling a build round rect method
// in that method we are passing the text, color and radius for our radius.
.buildRoundRect("G", getResources().getColor(R.color.purple_200), 10); // radius in px
borderIV.setImageDrawable(roundRect);
// below text drawable is a circular.
TextDrawable drawable2 = TextDrawable.builder().beginConfig()
.width(130) // width in px
.height(130) // height in px
.endConfig()
// as we are building a circular drawable we
// are calling a build round method.
// in that method we are passing our text and color.
.buildRound("F", getResources().getColor(R.color.purple_200));
circleIV.setImageDrawable(drawable2);
}
}
步骤4:使用MainActivity。 Java文件
转到MainActivity。 Java文件并参考以下代码。下面是MainActivity的代码。 Java文件。在代码内部添加了注释,以更详细地了解代码。
Java
import android.os.Bundle;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
import com.amulyakhare.textdrawable.TextDrawable;
public class MainActivity extends AppCompatActivity {
// creating a variable for image view.
private ImageView tileIV, borderIV, circleIV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// on below line we are initializing our image view
tileIV = findViewById(R.id.idIVTile);
borderIV = findViewById(R.id.idIVBorder);
circleIV = findViewById(R.id.idIVCircle);
// on below line we are creating a new text drawable
TextDrawable tileImg = TextDrawable.builder()
// begin config method is use to start the config.
.beginConfig()
// on below line we are setting width and height for our drawable.
.width(130) // width in px
.height(130) // height in px
// on below line we are ending the config.
.endConfig()
// as we are building a rectangle we are using
// a build rect method to create a new rectangle
// and inside that we are passing the text
// as G and color for the drawable.
.buildRect("G", getResources().getColor(R.color.purple_200));
tileIV.setImageDrawable(tileImg);
// below text drawable is for round rectangle
TextDrawable roundRect = TextDrawable.builder().beginConfig()
.width(130) // width in px
.height(130) // height in px
.endConfig()
// as we are building a rectangle with round corners we are calling a build round rect method
// in that method we are passing the text, color and radius for our radius.
.buildRoundRect("G", getResources().getColor(R.color.purple_200), 10); // radius in px
borderIV.setImageDrawable(roundRect);
// below text drawable is a circular.
TextDrawable drawable2 = TextDrawable.builder().beginConfig()
.width(130) // width in px
.height(130) // height in px
.endConfig()
// as we are building a circular drawable we
// are calling a build round method.
// in that method we are passing our text and color.
.buildRound("F", getResources().getColor(R.color.purple_200));
circleIV.setImageDrawable(drawable2);
}
}
现在运行您的应用程序,并查看该应用程序的输出。
输出: