Android 实现网络图片上显示文字,且不使用第三方库或依赖。
可以把图片下载下来,使用Graphics2D添加文字
针对该问题,可以通过使用Android自带的ImageView来实现图片与文字的显示,然后通过回调的方式实现在不依赖第三方库或者依赖的情况下实现这个功能,具体步骤如下:
1.在xml布局文件中,添加一个ImageView和一个TextView用于分别显示网络图片和文字信息。
<LinearLayout
android:id="@+id/linear_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/image_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop"/>
<TextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:textColor="@android:color/white"/>
</LinearLayout>
2.在Activity中,获取到ImageView和TextView的引用,并通过回调的方式实现图片和文字的显示。
public class MainActivity extends AppCompatActivity {
private ImageView mImageView;
private TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageView = findViewById(R.id.image_view);
mTextView = findViewById(R.id.text_view);
loadContent(new Callback() {
@Override
public void onImageLoaded(Bitmap bitmap) {
mImageView.setImageBitmap(bitmap);
}
@Override
public void onTextLoaded(String text) {
mTextView.setText(text);
}
@Override
public void onError(Exception e) {
Log.e(TAG, "onError: ", e);
}
});
}
public void loadContent(final Callback callback) {
//在这里实现图片和文字的网络请求获取
//当获取到相应结果时,通过回调的方式实现在界面中的显示。
//具体流程:通过一个Callback接口,该接口定义了三个方法,分别是onImageLoaded、onTextLoaded、onError方法,用于对图片、文字的获取和错误情况的处理,然后将Callback接口传递到获取数据的方法中,在获取到数据时回调相应的方法来实现图片和文字的显示。
}
private interface Callback {
void onImageLoaded(Bitmap bitmap);
void onTextLoaded(String text);
void onError(Exception e);
}
}
3.实现Callback接口中的三个方法。
public void loadContent(final Callback callback) {
//获取图片
new Thread(new Runnable() {
@Override
public void run() {
try {
URL url = new URL("图片URL");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(10000);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.connect();
InputStream inputStream = conn.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
callback.onImageLoaded(bitmap);
inputStream.close();
conn.disconnect();
} catch (Exception e) {
callback.onError(e);
}
}
}).start();
//获取文字
new Thread(new Runnable() {
@Override
public void run() {
try {
URL url = new URL("文字URL");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(10000);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String text = reader.readLine();
callback.onTextLoaded(text);
reader.close();
conn.disconnect();
} catch (Exception e) {
callback.onError(e);
}
}
}).start();
}
其中,onImageLoaded方法用于获取到图片后的回调,将Bitmap对象传递给ImageView进行显示;onTextLoaded方法用于获取到文字信息后的回调,将字符串传递给TextView进行显示;onError方法用于出现错误情况时的回调,用于在Log中输出错误信息。