android:用毕加索创建圆形图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26112150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
android: create circular image with picasso
提问by Katedral Pillon
The question had been asked and there had been a promise made for the very version of Picasso that I am using: How do I send a circular bitmap to an ImageView using Picasso? I am new to Picasso and only thing I have used is
有人问过这个问题,并且对我正在使用的 Picasso 版本做出了承诺:如何使用 Picasso 将圆形位图发送到 ImageView?我是毕加索的新手,我唯一使用过的是
Picasso.with(context).load(url).resize(w, h).into(imageview);
I have already found https://gist.github.com/julianshen/5829333but I am not sure how to combine it with the line above in a non-awkward way.
我已经找到了https://gist.github.com/julianshen/5829333,但我不确定如何以一种不尴尬的方式将它与上面的行结合起来。
回答by Anirudh Sharma
Research a bit before as there are answers available. Anyhow, follow This Linkand read it carefully to know how to use it.
因为有可用的答案,所以先研究一下。无论如何,请遵循此链接并仔细阅读以了解如何使用它。
try this:
尝试这个:
import com.squareup.picasso.Transformation;
public class CircleTransform implements Transformation {
@Override
public Bitmap transform(Bitmap source) {
int size = Math.min(source.getWidth(), source.getHeight());
int x = (source.getWidth() - size) / 2;
int y = (source.getHeight() - size) / 2;
Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);
if (squaredBitmap != source) {
source.recycle();
}
Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig());
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
BitmapShader shader = new BitmapShader(squaredBitmap,
Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
paint.setShader(shader);
paint.setAntiAlias(true);
float r = size / 2f;
canvas.drawCircle(r, r, r, paint);
squaredBitmap.recycle();
return bitmap;
}
@Override
public String key() {
return "circle";
}
}
then simply apply it like:
然后简单地应用它:
Picasso.with(activity).load(mayorShipImageLink).transform(new CircleTransform()).into(ImageView);
回答by goodKode
here is something that's provided by the support-v4 library! Look into RoundedBitmapDrawable. No need to roll your own:
这是support-v4 库提供的内容!查看 RoundedBitmapDrawable。无需自己动手:
Picasso.with(context).load(url)
.resize(w, h)
.into(myImageView, new Callback() {
@Override
public void onSuccess() {
Bitmap imageBitmap = ((BitmapDrawable) myImageView.getDrawable()).getBitmap();
RoundedBitmapDrawable imageDrawable = RoundedBitmapDrawableFactory.create(getResources(), imageBitmap);
imageDrawable.setCircular(true);
imageDrawable.setCornerRadius(Math.max(imageBitmap.getWidth(), imageBitmap.getHeight()) / 2.0f);
myImageView.setImageDrawable(imageDrawable);
}
@Override
public void onError() {
myImageView.setImageResource(R.drawable.default_image);
}
});
Note: Picasso also has a .transform(customTransformation)call that you could theoretically use, however, I had issues with that. This above works. Good luck!
注意:毕加索还有一个理论上可以使用的.transform(customTransformation)调用,但是,我遇到了问题。这上面的工作。祝你好运!
回答by Breeno
Another alternative I found was this guys library. It works standalone, or in conjunction with Picasso. I chose the Picasso route, like below:
我发现的另一个选择是这个人图书馆。它可以独立工作,也可以与毕加索结合使用。我选择了毕加索路线,如下图:
https://github.com/vinc3m1/RoundedImageView
https://github.com/vinc3m1/RoundedImageView
Transformation transformation = new RoundedTransformationBuilder()
.borderColor(Color.BLACK)
.borderWidthDp(3)
.cornerRadiusDp(30)
.oval(false)
.build();
Picasso.with(context)
.load(url)
.fit()
.transform(transformation)
.into(imageView);
Worked for me!
为我工作!
回答by amit
I have tried all solutions above but none of them gives me circle transform without cropping picture..those solution will work only for images with same width and height..this is my solution on above
我已经尝试了上面的所有解决方案,但没有一个给我圆形变换而不裁剪图片..这些解决方案仅适用于具有相同宽度和高度的图像..这是我上面的解决方案
first ------
第一的 - - -
Picasso.with(getActivity())
.load(url)
.error(R.drawable.image2)
.placeholder(R.drawable.ic_drawer)
.resize(200, 200)
.transform(new ImageTrans_CircleTransform())
.into(imageView1);
then do this --------
然后这样做--------
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Shader.TileMode;
import com.squareup.picasso.Transformation;
public class ImageTrans_CircleTransform implements Transformation {
@Override
public Bitmap transform(Bitmap source) {
if (source == null || source.isRecycled()) {
return null;
}
final int width = source.getWidth() + borderwidth;
final int height = source.getHeight() + borderwidth;
Bitmap canvasBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
BitmapShader shader = new BitmapShader(source, TileMode.CLAMP, TileMode.CLAMP);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setShader(shader);
Canvas canvas = new Canvas(canvasBitmap);
float radius = width > height ? ((float) height) / 2f : ((float) width) / 2f;
canvas.drawCircle(width / 2, height / 2, radius, paint);
//border code
paint.setShader(null);
paint.setStyle(Paint.Style.STROKE);
paint.setColor(bordercolor);
paint.setStrokeWidth(borderwidth);
canvas.drawCircle(width / 2, height / 2, radius - borderwidth / 2, paint);
//--------------------------------------
if (canvasBitmap != source) {
source.recycle();
}
return canvasBitmap;
}
@Override
public String key() {
return "circle";
}
}
回答by Nikolay Krasilnikov
There is a transformation library for Picasso.
Picasso 有一个转换库。
Just add gradle dependency
只需添加gradle依赖
implementation 'jp.wasabeef:picasso-transformations:2.2.1'
End use it
结束使用它
Picasso.with(context)
.load(url)
.resize(w, h)
.transform(new CropCircleTransformation())
.into(imageview);
Wiki: Picasso Transformations
维基:毕加索变换
回答by NIPHIN
Use this library to create a circular imageview. To make a circular ImageView, add this CircularImageView library to your projectand add CircularImageView in your layout XML
使用这个库来创建一个圆形的图像视图。 要制作圆形 ImageView,请将此 CircularImageView 库添加到您的项目中,并在您的布局 XML 中添加 CircularImageView
<com.pkmmte.view.CircularImageView
android:layout_width="250dp"
android:layout_height="250dp"
android:src="@drawable/image"
app:border_color="#EEEEEE"
app:border_width="4dp"
app:shadow="true" />`
Then use picasso to load required image into this imageView. Picasso does all the caching you dont need to worry about it
然后使用 picasso 将所需的图像加载到此 imageView 中。Picasso 做了所有你不需要担心的缓存
回答by Marco Aurelio
Include the xml drawable of type Layer- list with the code below
在下面的代码中包含 Layer-list 类型的 xml drawable
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/shape_status">
<shape android:shape="oval">
<solid android:color="@android:color/black"/>
</shape>
</item>
<item android:drawable="@drawable/ic_status_content"/></layer-list>
then use the xml to your ImageView in the android.src
然后在 android.src 中使用 xml 到你的 ImageView
<ImageView
android:id="@+id/iconStatus"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_gravity="right"
android:src="@drawable/ic_circle_status"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"/>
回答by Aditya Vyas-Lakhan
This one worked for me
这个对我有用
<com.androidhub4you.crop.RoundedImageView
android:id="@+id/imageView_round"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginTop="15dp"
android:src="@drawable/ic_launcher" />
http://www.androidhub4you.com/2014/10/android-custom-shape-imageview-rounded.html
http://www.androidhub4you.com/2014/10/android-custom-shape-imageview-rounded.html
回答by G00fY
This one is working with the current Picasso 3 snapshot:
这个正在使用当前的 Picasso 3 快照:
class CircleTransformation : Transformation {
override fun transform(source: RequestHandler.Result): RequestHandler.Result {
if (source.bitmap == null) {
return source
}
var bitmap: Bitmap
// since we cant transform hardware bitmaps create a software copy first
if (VERSION.SDK_INT >= VERSION_CODES.O && source.bitmap!!.config == Config.HARDWARE) {
val softwareCopy = source.bitmap!!.copy(Config.ARGB_8888, true)
if (softwareCopy == null) {
return source
} else {
bitmap = softwareCopy
source.bitmap!!.recycle()
}
} else {
bitmap = source.bitmap!!
}
var size = bitmap.width
// if bitmap is non-square first create square one
if (size != bitmap.height) {
var sizeX = size
var sizeY = bitmap.height
size = Math.min(sizeY, sizeX)
sizeX = (sizeX - size) / 2
sizeY = (sizeY - size) / 2
val squareSource = Bitmap.createBitmap(bitmap, sizeX, sizeY, size, size)
bitmap.recycle()
bitmap = squareSource
}
val circleBitmap = Bitmap.createBitmap(size, size, Config.ARGB_8888)
val canvas = Canvas(circleBitmap)
val paint = Paint()
val shader = BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP)
paint.shader = shader
paint.isAntiAlias = true
val centerAndRadius = size / 2f
canvas.drawCircle(centerAndRadius, centerAndRadius, centerAndRadius, paint)
bitmap.recycle()
return RequestHandler.Result(circleBitmap, source.loadedFrom, source.exifRotation)
}
override fun key(): String {
return "circleTransformation()"
}
}
Picasso3 gist: https://gist.github.com/G00fY2/f3fbc468570024930c1fd9eb4cec85a1
Picasso3 要点:https://gist.github.com/G00fY2/f3fbc468570024930c1fd9eb4cec85a1
回答by chabislav
Here is what worked for me with Picasso v2.71828
这是毕加索 v2.71828 对我有用的东西
class CircleTransform : Transformation {
override fun transform(source: Bitmap?): Bitmap? {
if (source == null) {
return source
}
var bitmap: Bitmap
// since we cant transform hardware bitmaps create a software copy first
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && source.config == Bitmap.Config.HARDWARE) {
val softwareCopy = source.copy(Bitmap.Config.ARGB_8888, true)
if (softwareCopy == null) {
return source
} else {
bitmap = softwareCopy
source.recycle()
}
} else {
bitmap = source
}
var size = bitmap.width
// if bitmap is non-square first create square one
if (size != bitmap.height) {
var sizeX = size
var sizeY = bitmap.height
size = Math.min(sizeY, sizeX)
sizeX = (sizeX - size) / 2
sizeY = (sizeY - size) / 2
val squareSource = Bitmap.createBitmap(bitmap, sizeX, sizeY, size, size)
bitmap.recycle()
bitmap = squareSource
}
val circleBitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888)
val canvas = Canvas(circleBitmap)
val paint = Paint()
val shader = BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP)
paint.shader = shader
paint.isAntiAlias = true
val centerAndRadius = size / 2f
canvas.drawCircle(centerAndRadius, centerAndRadius, centerAndRadius, paint)
bitmap.recycle()
return circleBitmap
}
override fun key(): String {
return "circleTransformation()"
}
}
}