Xamarin上的圆形ImageView

时间:2015-08-19 14:28:28

标签: android xamarin imageview xamarin.android

我正在开发一个Xamarin Android应用程序,我需要制作一个循环ImageView

如何实现这一目标?

5 个答案:

答案 0 :(得分:6)

我使用RoundedImageView库。它是用Java编写的,但是你可以write a binding没有太多问题。完成后,您只需将其添加到.axml

即可
<RoundedImageView
    local:riv_corner_radius="15dp"
    local:riv_oval="false"
    android:scaleType="centerCrop"
    android:layout_width="30dp"
    android:layout_height="30dp" />

为未来的读者编辑:我为Xamarin.Android编写了一个RoundedImageView的端口,基于此帖子上链接的库。可以找到源代码here和NuGet包here。还包括MvxRoundedImageView以用于MvvmCross。

答案 1 :(得分:1)

参考链接:https://github.com/jamesmontemagno/CircleImageView-Xamarin.Android/blob/master/CircleImageSample/Resources/layout/Main.axml

  <refractored.controls.CircleImageView 
android:id="@+id/ImageProfile"
   android:layout_width="80dp" 
android:layout_height="80dp"
   android:scaleType="fitCenter" 
android:src="@drawable/app_icon"
   android:layout_gravity="center_horizontal"/>

=============================================== ============================    从nuget包中将Refractored.controls.CircleImageView的引用添加到您的项目中。

答案 2 :(得分:1)

Xamarin组件也可用,您可以查看here

答案 3 :(得分:0)

public class CircleDrawable : Drawable
{
    Bitmap bmp;
    BitmapShader bmpShader;
    Paint paint;
    RectF oval;

    public CircleDrawable (Bitmap bmp)
    {
        this.bmp = bmp;
        this.bmpShader = new BitmapShader (bmp, Shader.TileMode.Clamp, Shader.TileMode.Clamp);
        this.paint = new Paint () { AntiAlias = true };
        this.paint.SetShader (bmpShader);
        this.oval = new RectF ();
    }

    public override void Draw (Canvas canvas)
    {
        canvas.DrawOval (oval, paint);
    }

    protected override void OnBoundsChange (Rect bounds)
    {
        base.OnBoundsChange (bounds);
        oval.Set (0, 0, bounds.Width (), bounds.Height ());
    }

    public override int IntrinsicWidth {
        get {
            return bmp.Width;
        }
    }

    public override int IntrinsicHeight {
        get {
            return bmp.Height;
        }
    }

    public override void SetAlpha (int alpha)
    {

    }

    public override int Opacity {
        get {
            return (int)Format.Opaque;
        }
    }

    public override void SetColorFilter (ColorFilter cf)
    {

    }
}

来源:https://github.com/xamarin/xamarin-store-app/blob/master/XamarinStore.Droid/Views/CircleDrawable.cs

答案 4 :(得分:0)

请参阅以下链接:

  

How to create a circular ImageView in Android?

     

How to make an ImageView with rounded corners?

以上代码适用于原生android。您需要调整代码以转换为c#语法并采用xamarin android。为方便起见,我已将代码更改为c#。

public class ImageHelper
{
    public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
    Bitmap output = Bitmap.CreateBitmap(bitmap.Width, bitmap.Height
            , Android.Graphics.Bitmap.Config.Argb8888);
    Canvas canvas = new Canvas(output);
    Color color = Color.DodgerBlue;
    Paint paint = new Paint();
    Rect rect = new Rect(0, 0, bitmap.Width, bitmap.Height);
    RectF rectF = new RectF(rect);
    float roundPx = pixels;

    paint.AntiAlias = true;
    canvas.DrawARGB(0, 0, 0, 0);
    paint.Color = color;
    canvas.DrawRoundRect(rectF, roundPx, roundPx, paint);

    paint.SetXfermode(new PorterDuffXfermode(Android.Graphics.PorterDuff.Mode.SrcIn));
    canvas.DrawBitmap(bitmap, rect, rect, paint);

    return output;
}