2015-10-15 94 views
2

我在应用程序中有一个简单的VectorDrawable。它来自Android SDK和看起来像这样:绘制VectorDrawable在不同的尺寸

<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> 

我想把它画成400×400位,使得其扩展到填充位图。目前,当我绘制它时,它只在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); 
+0

你在'metrics'中有什么?当我离开它时,你想要的东西适合我。 – Anthony

回答

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();