2016-12-12 4 views
1

내가 다음 ImageView의 배경으로이 bitmap을 설정 bitmaplayer-list을 변환하려고하지만이 테스트 프로젝트입니다변환 레이어 목록은 아마

작동하지 작동하지 비트 맵. 나는 이미지 뷰에 직접 그릴 수를 설정할 수 있습니다 알지만, 여기
내 레이어 목록입니다 (ic_circle, 그것은 다른 모양 내가 Bitmap-LayerDrawable 다음 다음 BitmapBitmapDrawable에 변환 이미지 뷰의 배경으로 설정 변환 왜 때 알고 싶어요 .XML)

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" 
    > 
    <item> 
     <shape 
      android:shape="oval"> 
      <solid android:color="#f00"></solid> 

     </shape> 
    </item> 
    <item 
     android:bottom="3dp" android:left="3dp" android:right="3dp" android:top="3dp"> 
     <shape 
      android:shape="oval"> 
      <solid android:color="#ff0"></solid> 

     </shape> 
    </item> 
    <item 
     android:bottom="6dp" android:left="6dp" android:right="6dp" android:top="6dp"> 
     <shape 
      android:shape="oval"> 
      <solid android:color="#0ff"></solid> 
     </shape> 
    </item> 
</layer-list> 

활동 코드

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     ImageView img = (ImageView) findViewById(R.id.img); 
     Bitmap bm = drawableToBitmap(ContextCompat.getDrawable(this, R.drawable.ic_circle)); 
     img.setBackground(new BitmapDrawable(bm)); 
    } 

    public Bitmap drawableToBitmap (Drawable drawable) { 
     int width = drawable.getIntrinsicWidth(); 
     width = width > 0 ? width : 1; 
     int height = drawable.getIntrinsicHeight(); 
     height = height > 0 ? height : 1; 

     Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 
     Canvas canvas = new Canvas(bitmap); 
     drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); 
     drawable.draw(canvas); 

     return bitmap; 
    } 
} 

XML 코드

에뮬레이터에서

(3210), 당신은 두 번째 이미지는 첫 번째 이미지에 다른 볼

당신의 당김에 enter image description here

+0

'레이어 목록을 비트 맵으로 변환 한 다음이 비트 맵을 ImageView의 배경으로 설정하려고합니다. 이것은 나에게 어떤 의미가 없습니다. 설명하는 마음 ** ** 당신은 왜하고 싶습니까? 즉, 레이어 목록을 직접 사용하지 않고 귀중한 기계주기를 절약하는 이유는 무엇입니까? –

+0

@Rotwang 이것은 테스트 일 뿐이므로 레이어 맵을 비트 맵으로 변환하는 데 문제가있어서이 프로젝트를 만들 수 있습니다. –

+1

왜 비트 맵으로 변환하고 싶습니까? 직접 이미지 뷰로 드로어 블을 설정할 수 있습니다 .. – Meenal

답변

2

당신이, 당신은 (노란색과 빨간색)이 외부 작은 원의 경계를 정의해야하고 파란색은 나머지 모든 공간을으로 가져 가야합니다. 그러나 정의 된 크기가 없으므로 캔버스에 그려지면 크기는 0이됩니다.

비트 맵에 직접 그릴 때 크기를 정의해야합니다.