我正在尝试使ImageView变圆.我已经编写了以下代码,使它看起来像圆形,但不知何故它仍显示方形ImageView. [使用毕加索获取图像]
Java代码:
ImageView iv = (ImageView) addLinkDialog.findViewById(R.id.group_icon_jsoup);
Picasso.with(getBaseContext()).load(GroupImageUrl).into(iv);
iv.setBackgroundResource(R.drawable.icon_img);
ImageView代码:
<ImageView
android:id="@+id/group_icon_jsoup"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_gravity="center"
android:layout_margin="8dp"
android:background="@drawable/icon_img" />
@绘制/ icon_img.xml:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/circle"/>
</layer-list>
@绘制/ circle.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadius="0dp"
android:shape="ring"
android:thicknessRatio="1.9"
android:useLevel="false" >
<solid android:color="@android:color/transparent" />
<stroke
android:width="10dp"
android:color="@android:color/white" />
</shape>
解决方法:
主要问题将是当您再次使用Picasso设置图像以将其设置为imageView视图边界而不是其创建的背景时.
如果您以编程方式设置了一个,它将覆盖您的背景!
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false">
<shape android:shape="oval">
<solid android:color="@color/colorPrimary"/>
</shape>
</item>
</selector>
您可以将其设置为视图的背景.然后尝试使用view.setBackgroundResource(R.drawable.icon_img); .您会注意到变化!
你可以通过Add a background image to shape in xml Android
Mask ImageView with round corner background
检查人们在这里尝试的各种方式!
但是,使用毕加索,您可以直接与其他第三方合作.
final ImageView imageView = (ImageView) findViewById(R.id.group_icon_jsoup);
Picasso.with(YourActivity.this).load("http://i.imgur.com/DvpvklR.png")
.resize(100, 100)
.into(imageView, new Callback() {
@Override
public void onSuccess() {
Bitmap imageBitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
RoundedBitmapDrawable imageDrawable = RoundedBitmapDrawableFactory.create(getResources(), imageBitmap);
imageDrawable.setCircular(true);
imageDrawable.setCornerRadius(Math.max(imageBitmap.getWidth(), imageBitmap.getHeight()) / 2.0f);
imageView.setImageDrawable(imageDrawable);
}
@Override
public void one rror() {
imageView.setImageResource(R.drawable.amanda);
}
});