Glideと組み合わせてRoundedCornerTransformationを使用してください。イメージが丸くないので、あなたのカードビューの丸い角が見えません。画像ビューの幅と高さを指定します。 .FitCenter()を削除します。
同じ変換でもサイズの異なる同じ画像を持つ2番目のリストをお持ちの場合は、.diskCacheStrategy(DiskCacheStrategy.ALL)
で注意してください。また、変換もキャッシュされます。次に、.diskCacheStrategy(DiskCacheStrategy.SOURCE)
を使用します。
以下のコードはXamarin Androidのコードですが、簡単にjavaに変換できます。
あなたはこのようにそれを呼び出すことができます。
[Flags]
public enum CornerTransformType
{
TopLeftCut = 0x1,
TopRightCut = 0x2,
BottomLeftCut = 0x4,
BottomRightCut = 0x8,
TopLeftRounded = 0x10,
TopRightRounded = 0x20,
BottomLeftRounded = 0x40,
BottomRightRounded = 0x80,
AllCut = TopLeftCut | TopRightCut | BottomLeftCut | BottomRightCut,
LeftCut = TopLeftCut | BottomLeftCut,
RightCut = TopRightCut | BottomRightCut,
TopCut = TopLeftCut | TopRightCut,
BottomCut = BottomLeftCut | BottomRightCut,
AllRounded = TopLeftRounded | TopRightRounded | BottomLeftRounded | BottomRightRounded,
LeftRounded = TopLeftRounded | BottomLeftRounded,
RightRounded = TopRightRounded | BottomRightRounded,
TopRounded = TopLeftRounded | TopRightRounded,
BottomRounded = BottomLeftRounded | BottomRightRounded,
}
グライドのBitmapTransformationから継承CornersTransformationを使用します。
.transform(new CornersTransformation(context, _radius, _radius, _radius, _radius, CornerTransformType.AllRounded, preferredImageWidth, preferredImageHeight))
は、丸みを帯びた角を定義する列挙クラスを使用します。
public class CornersTransformation : BitmapTransformation
{
#region properties
public double TopLeftCornerSize { get; set; }
public double TopRightCornerSize { get; set; }
public double BottomLeftCornerSize { get; set; }
public double BottomRightCornerSize { get; set; }
public int DesiredWidht { get; set; }
public int DesiredHeight { get; set; }
public CornerTransformType CornersTransformType { get; set; }
public bool SetForcedSize { get; set; }
public override string Id
{
get
{
return string.Format("CornersTransformation,cornersSizes={0},{1},{2},{3},cornersTransformType={4},cropWidthRatio={5},cropHeightRatio={6},",
TopLeftCornerSize, TopRightCornerSize, BottomRightCornerSize, BottomLeftCornerSize, CornersTransformType, DesiredWidht, DesiredHeight);
}
}
#endregion
#region constructor
public CornersTransformation(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
{
}
public CornersTransformation(IBitmapPool p0) : base(p0)
{
}
public CornersTransformation(Context context, double topLeftCornerSize, double topRightCornerSize, double bottomLeftCornerSize, double bottomRightCornerSize,
CornerTransformType cornersTransformType, int desiredWidht, int desiredHeight) : base(context)
{
TopLeftCornerSize = topLeftCornerSize;
TopRightCornerSize = topRightCornerSize;
BottomLeftCornerSize = bottomLeftCornerSize;
BottomRightCornerSize = bottomRightCornerSize;
CornersTransformType = cornersTransformType;
DesiredWidht = desiredWidht;
DesiredHeight = desiredHeight;
}
#endregion
#region public methods
protected override Bitmap Transform(IBitmapPool p0, Bitmap bitmap, int p2, int p3)
{
return ToTransformedCorners(bitmap, TopLeftCornerSize, TopRightCornerSize, BottomLeftCornerSize, BottomRightCornerSize, CornerTransformType.AllRounded, 1, 1);
}
public Bitmap ToTransformedCorners(Bitmap source, double topLeftCornerSize, double topRightCornerSize, double bottomLeftCornerSize, double bottomRightCornerSize,
CornerTransformType cornersTransformType, double cropWidthRatio, double cropHeightRatio)
{
if (DesiredWidht == 0 || DesiredHeight == 0)
{
return source;
}
float cropX = 0, cropY = 0;
double desiredWidth = 0;
double desiredHeight = 0;
if (forcedSize)
{
BzLogging.Log("DesiredWidht: " + DesiredWidht + ", DesiredHeight: " + DesiredHeight + " in transformation");
if (DesiredWidht > source.Width)
{
source = Bitmap.CreateScaledBitmap(source, DesiredWidht, source.Height * (DesiredWidht/source.Width), true);
}
double sourceWidth = source.Width;
double sourceHeight = source.Height;
desiredWidth = DesiredWidht;
desiredHeight = DesiredHeight;
topLeftCornerSize = topLeftCornerSize * (desiredWidth + desiredHeight)/2/100;
topRightCornerSize = topRightCornerSize * (desiredWidth + desiredHeight)/2/100;
bottomLeftCornerSize = bottomLeftCornerSize * (desiredWidth + desiredHeight)/2/100;
bottomRightCornerSize = bottomRightCornerSize * (desiredWidth + desiredHeight)/2/100;
cropX = (float)((sourceWidth - desiredWidth)/2);
cropY = (float)((sourceHeight - desiredHeight)/2);
}
else
{
cropX = 0;
cropY = 0;
desiredWidth = source.Width;
desiredHeight = source.Height;
topLeftCornerSize = topLeftCornerSize * (desiredWidth + desiredHeight)/2/100;
topRightCornerSize = topRightCornerSize * (desiredWidth + desiredHeight)/2/100;
bottomLeftCornerSize = bottomLeftCornerSize * (desiredWidth + desiredHeight)/2/100;
bottomRightCornerSize = bottomRightCornerSize * (desiredWidth + desiredHeight)/2/100;
}
Bitmap bitmap = Bitmap.CreateBitmap((int)desiredWidth, (int)desiredHeight, Bitmap.Config.Argb8888);
using (Canvas canvas = new Canvas(bitmap))
using (Paint paint = new Paint())
using (BitmapShader shader = new BitmapShader(source, Shader.TileMode.Clamp, Shader.TileMode.Clamp))
using (Matrix matrix = new Matrix())
using (var path = new Path())
{
if (Math.Abs(cropX) > 0.0001 || Math.Abs(cropY) > 0.0001)
{
matrix.SetTranslate(-cropX, -cropY);
shader.SetLocalMatrix(matrix);
}
paint.SetShader(shader);
paint.AntiAlias = true;
// TopLeft
if (cornersTransformType.HasFlag(CornerTransformType.TopLeftCut))
{
path.MoveTo(0, (float)topLeftCornerSize);
path.LineTo((float)topLeftCornerSize, 0);
}
else if (cornersTransformType.HasFlag(CornerTransformType.TopLeftRounded))
{
path.MoveTo(0, (float)topLeftCornerSize);
path.QuadTo(0, 0, (float)topLeftCornerSize, 0);
}
else
{
path.MoveTo(0, 0);
}
// TopRight
if (cornersTransformType.HasFlag(CornerTransformType.TopRightCut))
{
path.LineTo((float)(desiredWidth - topRightCornerSize), 0);
path.LineTo((float)desiredWidth, (float)topRightCornerSize);
}
else if (cornersTransformType.HasFlag(CornerTransformType.TopRightRounded))
{
path.LineTo((float)(desiredWidth - topRightCornerSize), 0);
path.QuadTo((float)desiredWidth, 0, (float)desiredWidth, (float)topRightCornerSize);
}
else
{
path.LineTo((float)desiredWidth, 0);
}
// BottomRight
if (cornersTransformType.HasFlag(CornerTransformType.BottomRightCut))
{
path.LineTo((float)desiredWidth, (float)(desiredHeight - bottomRightCornerSize));
path.LineTo((float)(desiredWidth - bottomRightCornerSize), (float)desiredHeight);
}
else if (cornersTransformType.HasFlag(CornerTransformType.BottomRightRounded))
{
path.LineTo((float)desiredWidth, (float)(desiredHeight - bottomRightCornerSize));
path.QuadTo((float)desiredWidth, (float)desiredHeight, (float)(desiredWidth - bottomRightCornerSize), (float)desiredHeight);
}
else
{
path.LineTo((float)desiredWidth, (float)desiredHeight);
}
// BottomLeft
if (cornersTransformType.HasFlag(CornerTransformType.BottomLeftCut))
{
path.LineTo((float)bottomLeftCornerSize, (float)desiredHeight);
path.LineTo(0, (float)(desiredHeight - bottomLeftCornerSize));
}
else if (cornersTransformType.HasFlag(CornerTransformType.BottomLeftRounded))
{
path.LineTo((float)bottomLeftCornerSize, (float)desiredHeight);
path.QuadTo(0, (float)desiredHeight, 0, (float)(desiredHeight - bottomLeftCornerSize));
}
else
{
path.LineTo(0, (float)desiredHeight);
}
path.Close();
canvas.DrawPath(path, paint);
return bitmap;
}
}
#endregion
}
}上のカスタム変換について
詳細情報:https://futurestud.io/tutorials/glide-custom-transformation
期待どおりの画像と実際の画像を共有できますか? –
投稿をスクリーンショットで更新しました。 – r3dm4n
グライドはイメージの4つのコーナーすべてに対してimage ** cornerradius **を設定できますが、あなたの要件はそれに適合しないかもしれません。 –