我是android的新手,我想要这件事的一些帮助。我有一个图像,其URL在我的在线服务器上。
例如。
我的网址图片是:http:/example.com/image.png
并且存储在我的服务器中,我希望将此图片添加到 该图片中间的空白部分。
我已经在地图上的标记上添加了以上链接的图像,但是我不知道如何在空白部分内添加其他图像。我的代码段是这样的:
marker = map.addMarker(new MarkerOptions().position(position).title(title).snippet(snippetText)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.custom_marker)));
R.drawable.custom_marker
->是上面链接中的图像。有什么帮助吗?有没有办法从我的Web服务器检索图像并添加此功能?
提前致谢。
要在可绘制对象上方绘制图像,首先需要获取图像。为此,您将需要使用AsyncTask,因为UI线程上禁止进行网络操作。
要将(从服务器收到的)位图绘制到现有可绘制对象的顶部,您需要从中创建一个可变的位图。
最后,您可以在其上进行绘制,并将结果用作标记的基础。
这是一个例子:
class ImageTask extends AsyncTask<Void, Void, Bitmap> {
@Override
protected Bitmap doInBackground(Void... params) {
// Get bitmap from server
Bitmap overlay;
try {
URL url = new URL("http://www.quarktet.com/Icon-small.jpg");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
overlay = BitmapFactory.decodeStream(input);
} catch (IOException e) {
e.printStackTrace();
return null;
}
return overlay; }
protected void onPostExecute(Bitmap bitmap) {
// If received bitmap successfully, draw it on our drawable
if (bitmap != null) {
Bitmap marker = BitmapFactory.decodeResource(getResources(), R.drawable.custom_marker);
Bitmap newMarker = marker.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(newMarker);
// Offset the drawing by 25x25
canvas.drawBitmap(bitmap, 25, 25, null);
// Add the new marker to the map
mMap.addMarker(new MarkerOptions()
.position(position)
.title(title)
.snippet(snippetText)
.icon(BitmapDescriptorFactory.fromBitmap(newMarker)));
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Init your map here
new ImageTask().execute();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句