2012-04-11 31 views
25

Làm cách nào tôi có thể lấy bitmap từ hình dạng xml có thể vẽ được. Tôi đang làm gì sai?Làm thế nào để có được một Bitmap từ một drawable được định nghĩa trong một xml?

shadow.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle" > 

    <gradient 
     android:angle="270.0" 
     android:endColor="@android:color/transparent" 
     android:startColor="#33000000" 
     android:type="linear" /> 

    <size android:height="7.0dip" /> 

</shape> 

Phương pháp của tôi để lấy bitmap từ drawable:

private Bitmap getBitmap(int id) { 
    return BitmapFactory.decodeResource(getContext().getResources(), id); 
} 

getBitmap() đang trở lại null khi id thông qua vào là shadow.xml id có thể vẽ.

Trả lời

13

một ShapeDrawable không có bitmap được liên kết với nó - mục đích duy nhất của nó là được vẽ trên canvas. Cho đến khi phương thức vẽ của nó được gọi, nó không có hình ảnh. Nếu bạn có thể lấy phần tử canvas ở nơi bạn cần vẽ bóng, bạn có thể vẽ hình dạng đó thành hình dạng Có thể vẽ được, nếu không bạn có thể cần một chế độ xem riêng, trống trong bố cục của bạn với bóng làm nền.

+0

cảm ơn vì lời giải thích tốt. Tôi vẽ nó trực tiếp trên vải và làm việc tốt. – kaneda

+0

@kaneda bạn có thể hiển thị mã cuối cùng đã hoạt động không? Tôi đang đối mặt với cùng một vấn đề ở đây. Cảm ơn rất nhiều. –

+0

Đó sẽ là một cái gì đó dọc theo dòng 'Drawable d = getContext(). GetResources(). GetDrawable (R.drawable.id);' 'd.draw (canvas);' – Punksmurf

27

Đây là một giải pháp hoàn toàn làm việc:

private Bitmap getBitmap(int drawableRes) { 
    Drawable drawable = getResources().getDrawable(drawableRes); 
    Canvas canvas = new Canvas(); 
    Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); 
    canvas.setBitmap(bitmap); 
    drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); 
    drawable.draw(canvas); 

    return bitmap; 
} 

Và đây là một ví dụ:

Bitmap drawableBitmap = getBitmap(R.drawable.circle_shape); 

circle_shape.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="oval"> 
    <size 
     android:width="15dp" 
     android:height="15dp" /> 
    <solid 
     android:color="#94f5b6" /> 
    <stroke 
     android:width="2dp" 
     android:color="#487b5a"/> 
</shape> 
+1

Đây phải là câu trả lời được chấp nhận – blueware

+0

Nó ném java.lang.IllegalArgumentException: chiều rộng và chiều cao phải> 0 – BMU

+0

Chỉ cần cập nhật tương thích nhỏ: thay đổi "getResources(). GetDrawable (drawableRes);" bởi "ContextCompat.getDrawable (context, drawableRes);" – Tobliug

0

Bạn nên thêm kích thước thuộc tính đến sha của bạn pe drawable để ngăn chặn "java.lang.IllegalArgumentException: width và height phải> 0".

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
     android:shape="oval"> 
     <solid android:color="@color/colorAccent" /> 
     <stroke 
      android:width="1.3dp" 
      android:color="@color/white" /> 

     <size android:height="24dp" android:width="24dp"/> 
</shape> 
Các vấn đề liên quan