我想用5px环绕图像的角落,用Picasso在imageview上显示。我创建了一个简单的类ImageRoundCorners
,其中我使用简单的方法来舍入图像角,但是我的代码不起作用,角不是圆角。下面是我的代码:
file = new File(APP.DIR_APP + APP.IMAGE + "/ok.jpg");
if (file.isFile() && file.exists()) {
Uri uri = Uri.fromFile(file);
Picasso.with(this).load(uri).transform(new ImageRoundCorners()).into(fiv_image_view);
}
和ImageRoundCorners
类:
import com.squareup.picasso.Transformation;
public class ImageRoundCorners implements Transformation {
@Override
public Bitmap transform(Bitmap source) {
Bitmap output = Bitmap.createBitmap(source.getWidth(), source
.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, source.getWidth(), source.getHeight());
final RectF rectF = new RectF(rect);
final float roundPx = 50;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(source, rect, rect, paint);
return output;
}
@Override
public String key() {
return "RoundImage";
}
}
此代码中的问题是什么,我该如何解决?
我收到此错误:
java.lang.IllegalStateException: Transformation RoundImage mutated input Bitmap but failed to recycle the original.
答案 0 :(得分:2)
错误信息非常清楚。你只是忘了回收原始的Bitmap。
....
canvas.drawBitmap(source, rect, rect, paint);
source.recycle();
return output;
您的代码中只丢失了一行! (我对所有这些答案感到惊讶,这些答案告诉你要做各种不相关的,连根拔起的解决方案。)
答案 1 :(得分:1)
您可以将此图片视图用于圆角
https://github.com/siyamed/android-shape-imageview
<com.github.siyamed.shapeimageview.RoundedImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/neo"
app:siRadius="6dp"
app:siBorderWidth="6dp"
app:siBorderColor="@color/darkgray"
app:siSquare="true"/>
结果
答案 2 :(得分:0)
第1步 compile 'de.hdodenhof:circleimageview:1.2.1'
(模块:应用)
步骤2在XML文件中
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/imageView"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_centerVertical="true"
android:src="@drawable/chetankambale" />
活动
中的第3步// get image from drawble with rounded corner converted image
Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.yogeshborhade);
Bitmap circularBitmap = ImageConverter.getRoundedCornerBitmap(bitmap, 100);
// set rounded corner image to imageview
ImageView circularImageView = (ImageView) layout.findViewById(R.id.imageView);
circularImageView.setImageBitmap(circularBitmap);
答案 3 :(得分:0)
您可以使用毕加索的RoundedCornerTansformation,例如::
final int radius = 5;
final int margin = 5;
final Transformation transformation = new RoundedCornersTransformation(radius,margin);
Picasso.with(activity).load(uri).transform(transformation).into(fiv_image_view);
答案 4 :(得分:0)
您可以使用以下库。 https://github.com/lopspower/CircularImageView
<com.mikhaellopez.circularimageview.CircularImageView
android:layout_width="250dp"
android:layout_height="250dp"
android:src="@drawable/image"
app:civ_border_color="#EEEEEE"
app:civ_border_width="4dp"
app:civ_shadow="true"
app:civ_shadow_radius="10"
app:civ_shadow_color="#8BC34A"/>
答案 5 :(得分:0)