将图像更改为菜单内的圆圈

时间:2017-12-26 18:38:23

标签: android menu

如何将此图像更改为圆圈

Click here

毕加索

navigationDrawerHome.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                if(item.getItemId() == R.id.menu_facebook)
                {
                    navigationDrawerHome.getMenu().findItem(R.id.menu_login).setVisible(false);
                    navigationDrawerHome.getMenu().findItem(R.id.menu_logout).setVisible(true);
                    Picasso.with(Home.this).load("http-----").into(new Target() {
                        @Override
                        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                            navigationDrawerHome.getMenu().findItem(R.id.menu_logout).setIcon(new BitmapDrawable(bitmap));
                            navigationDrawerHome.getMenu().findItem(R.id.menu_logout).setTitle("Taha Sami");
                        }

                        @Override
                        public void onBitmapFailed(Drawable errorDrawable) {

                        }

                        @Override
                        public void onPrepareLoad(Drawable placeHolderDrawable) {

                        }
                    });

                }
                return true;
            }
        });

如何将图片更改为圆圈?

2 个答案:

答案 0 :(得分:-1)

以下是最简单的方法之一,请使用以下代码:

<强>依赖关系

dependencies {
    ...
    compile 'de.hdodenhof:circleimageview:2.1.0'
}

XML代码

<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"/>

<强>截图:

Screenshot

来源:ImageView in circular through xml

答案 1 :(得分:-1)

创建CircleImageView课程并展开ImageView

public class CircleImageView extends ImageView {

//you can change the radius to modify the circlur shape into oval or rounded rectangle

    public static float radius = 1000.0f;

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

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

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

    @Override
    protected void onDraw(Canvas canvas) {
//float radius = 36.0f;
        Path clipPath = new Path();
        RectF rect = new RectF(0, 0, this.getWidth(), this.getHeight());
        clipPath.addRoundRect(rect, radius, radius, Path.Direction.CW);
        canvas.clipPath(clipPath);
        super.onDraw(canvas);
    }
}

xml布局

中使用它
<com.example.project.CircleImageView 
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:src="@drawable/you_image" />