绘制VectorDrawable在不同的大小

时间:2015-10-15 14:51:10

标签: android android-vectordrawable

我在应用中有一个简单的VectorDrawable。它来自android SDK,看起来像这样:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="24dp"
        android:height="24dp"
        android:viewportWidth="24.0"
        android:viewportHeight="24.0">
    <path
        android:fillColor="#FF000000"
        android:pathData="M12,17.27L18.18,21l-1.64,-7.03L22,9.24l-7.19,-0.61L12,2 9.19,8.63 2,9.24l5.46,4.73L5.82,21z"/>
</vector>

我想将它绘制成400x400位图,以便缩放以填充位图。目前,当我绘制它时,它只显示24x24。这是代码:

    ImageView imageview = (ImageView) findViewById(R.id.image_view);
    VectorDrawable drawable = (VectorDrawable) getResources().getDrawable(
            R.drawable.testvector, null);
//  drawable.mutate();

    int width = 400
    int height = 400;
    Bitmap mutableImage = Bitmap.createBitmap(
            metrics, width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(mutableImage);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);
    imageview.setImageDrawable(drawable);

1 个答案:

答案 0 :(得分:0)

我只检查过VectorDrawableCompat,让我知道它是否适合你。

只要我没有旋转包含VectorDrawableCompat的视图,就可以为我工作。

ImageView view;
...
ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
layoutParams.width = 400;
layoutParams.height = 400;
view.setLayoutParams(layoutParams);
Drawable drawable = view.getDrawable().mutate();//Vector
view.setImageDrawable(drawable);// <- that's a hack
view.setDrawingCacheEnabled(true);
Bitmap bitmap = container.getDrawingCache();