ImageView上的Vectordrawable模糊位图

时间:2017-04-14 15:11:23

标签: android android-support-library android-vectordrawable android-support-design

我创建了一个扩展ImageView的自定义视图。当我设置其视口较小的矢量drawable时,我注意到图像模糊。 如果我只是使用ImageView,我看到矢量图像很清晰,因为它应该是。

在我的自定义视图中,我覆盖“setImageDrawable”,我在其中调用super,然后从drawable中获取位图以便稍后在[Activity(Label = "Map")] public class Map : Activity { protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); SetContentView(Resource.Layout.Map); ImageView map = FindViewById<ImageView>(Resource.Id.imageView1); Bitmap bitmap = Bitmap.CreateBitmap(500, 500, Bitmap.Config.Rgb565); Paint paint = new Paint(); paint.SetARGB(255, 255, 0, 0); Canvas canvas = new Canvas(bitmap); //Draw the image bitmap into the canvas canvas.DrawBitmap(bitmap, 5, 5, paint); //Draw everything you want into the canvas canvas.DrawCircle(50, 50, 10, paint); //Attach the canvas to the ImageView map.SetImageDrawable((new BitmapDrawable(bitmap))); Button item = FindViewById<Button>(Resource.Id.button1); //Declare item button item.Click += delegate { StartActivity(typeof(Item)); }; } } 中绘制。

以下是我如何转换为位图

onDraw

2 个答案:

答案 0 :(得分:1)

您需要获取视图宽度和高度,而不是drawable,并将它们用作可绘制边界。我假设您的视图维度可能会随着时间而变化,因此这不是一次性任务。我会将drawable存储在一个成员字段中:

   private Drawable drawable;

   @Override
   public void setImageDrawable(Drawable drawable) {
       this.drawable = drawable;
       this.drawable.setBounds(0, 0, getMeasuredWidth(), getMeasuredHeight());
       // invalidate if needed
   }

   @Override
   protected void onMeasure(int wms, int hms) {
       super.onMeasure(wms, hms);
       if (drawable != null) drawable.setBounds(0, 0, getMeasuredWidth(), getMeasuredHeight());
   }

然后onDraw,只需绘制drawable。

答案 1 :(得分:0)

只需执行drawable.getIntrinsicWidth()* 5(任何数字),drawable.getIntrinsicHeight()* 5并在第一个参数中传递DisplayMatrices,它将提高质量。


public Bitmap getBitmapFromDrawable(Drawable drawable) {
    Bitmap bitmap = Bitmap.createBitmap(context.getResources().getDisplayMetrics(), drawable.getIntrinsicWidth()*5,
            drawable.getIntrinsicHeight()*5,
            Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);
    return bitmap;
}