Android:圈子个人资料图片

Sye*_*Ali 1 android androiddesignsupport

我正在尝试为我的应用程序创建一个配置文件页面.这是我想要创建的示例.

一

我想知道如何在封面底部对齐带圆圈的图像.我糊涂了.

谢谢.

BOU*_*lid 15

你可以轻松地在你的库中添加这个库build.gradle:

compile 'de.hdodenhof:circleimageview:1.2.1'.

用法

<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/profile_image"
android:layout_width="96dp"
android:layout_height="96dp"
android:src="@drawable/profile"
app:civ_border_width="2dp"
app:civ_border_color="#FF000000"/>
Run Code Online (Sandbox Code Playgroud)


Mat*_*ter 6

这是一个循环类,ImageView无需拉入库.

public class CircularImageView extends ImageView
{

public CircularImageView( Context context )
{
    super( context );
}

public CircularImageView( Context context, AttributeSet attrs )
{
    super( context, attrs );
}

public CircularImageView( Context context, AttributeSet attrs, int defStyle )
{
    super( context, attrs, defStyle );
}

@Override
protected void onDraw( @NonNull Canvas canvas )
{

    Drawable drawable = getDrawable( );

    if ( drawable == null )
    {
        return;
    }

    if ( getWidth( ) == 0 || getHeight( ) == 0 )
    {
        return;
    }
    Bitmap b = ( ( BitmapDrawable ) drawable ).getBitmap( );
    Bitmap bitmap = b.copy( Bitmap.Config.ARGB_8888, true );

    int w = getWidth( )/*, h = getHeight( )*/;

    Bitmap roundBitmap = getCroppedBitmap( bitmap, w );
    canvas.drawBitmap( roundBitmap, 0, 0, null );

}

private static Bitmap getCroppedBitmap( @NonNull Bitmap bmp, int radius )
{
    Bitmap bitmap;

    if ( bmp.getWidth( ) != radius || bmp.getHeight( ) != radius )
    {
        float smallest = Math.min( bmp.getWidth( ), bmp.getHeight( ) );
        float factor = smallest / radius;
        bitmap = Bitmap.createScaledBitmap( bmp, ( int ) ( bmp.getWidth( ) / factor ), ( int ) ( bmp.getHeight( ) / factor ), false );
    }
    else
    {
        bitmap = bmp;
    }

    Bitmap output = Bitmap.createBitmap( radius, radius,
            Bitmap.Config.ARGB_8888 );
    Canvas canvas = new Canvas( output );

    final Paint paint = new Paint( );
    final Rect rect = new Rect( 0, 0, radius, radius );

    paint.setAntiAlias( true );
    paint.setFilterBitmap( true );
    paint.setDither( true );
    canvas.drawARGB( 0, 0, 0, 0 );
    paint.setColor( Color.parseColor( "#BAB399" ) );
    canvas.drawCircle( radius / 2 + 0.7f,
            radius / 2 + 0.7f, radius / 2 + 0.1f, paint );
    paint.setXfermode( new PorterDuffXfermode( PorterDuff.Mode.SRC_IN ) );
    canvas.drawBitmap( bitmap, rect, rect, paint );

    return output;
}

}
Run Code Online (Sandbox Code Playgroud)

使用示例:

<your.package.name.CircularImageView
                    android:id="@+id/circleImage"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"/>
Run Code Online (Sandbox Code Playgroud)