Android 毕加索的圆形图像
Android round image with picasso
我想用 5px 将图像的角圆化,以便在 Picasso 的 imageview 上显示。我创建了简单的 class 作为 ImageRoundCorners
,其中我使用简单的方法来圆角图像角,但我的代码不起作用,角不是 rounded.Below 是我的代码:
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
class:
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.
您可以将此图像视图用于圆角
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"/>
结果
步骤 1 compile 'de.hdodenhof:circleimageview:1.2.1'
在您的 Build.Gradle(Module:App)
步骤 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" />
Activity中的步骤 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);
您可以像这样使用 picasso 的 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);
您可以使用以下库。
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"/>
Picasso.with(MainActivity.this)
.load(url)
.transform(new PRoundedCornersTransformation(30, 0, PRoundedCornerTransformation.CornerType.ALL))
.into(imageView);
错误信息很清楚。你只是忘了回收原始位图。
....
canvas.drawBitmap(source, rect, rect, paint);
source.recycle();
return output;
您的代码只少了一行! (我对所有这些告诉你做各种不相关的、连根拔起的解决方案的答案感到惊讶。)
我想用 5px 将图像的角圆化,以便在 Picasso 的 imageview 上显示。我创建了简单的 class 作为 ImageRoundCorners
,其中我使用简单的方法来圆角图像角,但我的代码不起作用,角不是 rounded.Below 是我的代码:
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
class:
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.
您可以将此图像视图用于圆角
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"/>
结果
步骤 1 compile 'de.hdodenhof:circleimageview:1.2.1'
在您的 Build.Gradle(Module:App)
步骤 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" />
Activity中的步骤 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);
您可以像这样使用 picasso 的 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);
您可以使用以下库。 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"/>
Picasso.with(MainActivity.this)
.load(url)
.transform(new PRoundedCornersTransformation(30, 0, PRoundedCornerTransformation.CornerType.ALL))
.into(imageView);
错误信息很清楚。你只是忘了回收原始位图。
....
canvas.drawBitmap(source, rect, rect, paint);
source.recycle();
return output;
您的代码只少了一行! (我对所有这些告诉你做各种不相关的、连根拔起的解决方案的答案感到惊讶。)