如何在CircleImageView(hdodenhof/CircleImageView)中设置drawable?

问题描述:

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)'。 –

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