gpt4 book ai didi

android - 在从 url 获取的资源可绘制对象中显示标记

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:06:17 25 4
gpt4 key购买 nike

map showing uers

这是我的 map 页面,它显示了我的应用程序的所有用户。此外,图像(标记)是从我的服务器提供的 URL 中获得的。这些标记必须放在 Drawable 中(如图所示的圆圈)。我使用 Canvas 从 url 创建了一个像 Bitmap 这样的圆圈。

    public Drawable showMe(String url)
{
Bitmap bitmap=null;
try {
URL newurl = new URL(url);
bitmap = BitmapFactory.decodeStream(newurl.openConnection().getInputStream());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
bitmap=getBitmap(url);
Paint paint = new Paint();
paint.setFilterBitmap(true);
int targetWidth = 30;
int targetHeight = 30;
Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, targetHeight,Bitmap.Config.ARGB_8888);
RectF rectf = new RectF(0, 0, 30, 30);
Canvas canvas = new Canvas(targetBitmap);
Path path = new Path();
path.addRoundRect(rectf, targetWidth, targetHeight, Path.Direction.CW);
canvas.clipPath(path);
canvas.drawBitmap( bitmap, new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()),
new Rect(0, 0, targetWidth, targetHeight), paint);

Matrix matrix = new Matrix();
matrix.postScale(1f, 1f);
Bitmap resizedBitmap = Bitmap.createBitmap(targetBitmap, 0, 0, 30, 30, matrix, true);
Bitmap bitmap_circle=mergeBitmaps(resizedBitmap);
BitmapDrawable bd = new BitmapDrawable(bitmap_circle);



return bd;
}

上述函数将为标记创建最终的可绘制对象。此外,mergeBitmaps() 函数将资源可绘制对象和位图合并在一起。

public Bitmap mergeBitmaps(Bitmap manBitmap){

try{

Bitmap markerBitmap = BitmapFactory.decodeResource( this.getResources(), R.drawable.circle_bg);
Bitmap bmOverlay = Bitmap.createBitmap(markerBitmap.getWidth(), markerBitmap.getHeight(), markerBitmap.getConfig());
Canvas canvas = new Canvas(bmOverlay);
Matrix matrix = new Matrix();
matrix.postScale(1f, 1f);
canvas.drawBitmap(markerBitmap, matrix, null);
canvas.drawBitmap(manBitmap, 5, 5, null);
return bmOverlay;

}
catch(Exception ex){
ex.printStackTrace();
return null;
}

}

但问题是,这个位图并不是最适合放在背景 Drawable 中,以获得两者结合在一起会产生单一图像的感觉。

谁能帮帮我?

最佳答案

改变

canvas.drawBitmap(manBitmap, 5, 5, null);

喜欢smtg

canvas.drawBitmap(manBitmap, (markerBitmap.getWidth()-manbitmap.getWidth())/2, 
(markerBitmap.getHeight()-manbitmap.getHeight())/2, null);

关于android - 在从 url 获取的资源可绘制对象中显示标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11447146/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com