2017-10-10 66 views
2
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 

    profilephoto.setImageDrawable(
     getResources().getDrawable(
      R.drawable.profile, 
      getApplicationContext().getTheme())); 
} 
else {  
    profilephoto.setImageDrawable(
     getResources().getDrawable(
      R.drawable.profile)); 
} 

我想動態地將圖像設置爲circleImageView。我已使用setImageDrawable,但CircleImageView不顯示任何圖像。如何在CircleImageView(hdodenhof/CircleImageView)中設置drawable?

+0

您的方法看起來正確。 'R.drawable.profile'是一個'BitmapDrawable'還是另一種類型的'Drawable'? LogCat有輸出嗎?如果你用一個普通的'ImageView'代替'CircleImageView',它會起作用嗎?在更一般的說明中,如果項目中沒有對'setImageDrawable'的特定要求,則可以使用'setImageResource(R.drawable.profile)'。 –

回答

0
  1. 添加依賴庫在gradle這個文件

    compile 'de.hdodenhof:circleimageview:2.2.0' 
    
  2. XML文件

    < de.hdodenhof.circleimageview.CircleImageView 
         xmlns:app="http://schemas.android.com/apk/res-auto" 
         android:id="@+id/profile_image" 
         android:layout_width="96dp" 
         android:layout_height="96dp" 
         app:civ_border_width="2dp" 
         app:civ_border_color="#FF000000"/> 
    
  3. Activity類

    ImageView imageView = (ImageView) findViewById(R.id.profile_image); 
    imageView.setImageDrawable(getResources().getDrawable(R.drawable.sa)); 
    
+1

我創建了動態的圓形圖像視圖: CircleImageView profilephoto = newCircleImageView(getApplicationContext());當我使用方法setImageBitmap它工作正常,但是當我使用SetImageDrawable它不顯示drawable。 –

相關問題