2

我在应用程序中有一个简单的 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);
4

1 回答 1

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();
于 2016-05-19T10:14:20.023 回答