ImageView in circular through xml

前端 未结 27 1004
死守一世寂寞
死守一世寂寞 2020-11-22 10:35

I\'d Like to make any image from my ImageView to be circular with a border.

I searched but couldn\'t find any useful information (anything that I tried

相关标签:
27条回答
  • 2020-11-22 10:56

    Best Solution courtesy https://www.youtube.com/watch?v=0MHoNU7ytaw the width and height of the card view determine the size of the images it contains set up is as follows:

    1. Add Dependency to Gradle(Module)
    2. Add the xml code to activity.xml or fragment.xml file
        implementation 'androidx.cardview:cardview:1.0.0'
    
       <androidx.cardview.widget.CardView
          android:layout_width="300dp"
          android:layout_height="270dp"
          android:layout_gravity="center"
          app:cardCornerRadius="150dp"
          app:cardBackgroundColor="@color/trans"
          >
        <ImageView
            android:id="@+id/resultImage"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/congrats"
            android:layout_gravity="center">
    
        </ImageView>
    
    
      </androidx.cardview.widget.CardView>```
    
    
    0 讨论(0)
  • 2020-11-22 10:56

    I did it like that, I used my background color in my vector image

    ic_bg_picture.xml

     <vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="100dp"
        android:height="100dp"
        android:viewportWidth="100"
        android:viewportHeight="100">
      <path
          android:pathData="M100.6,95.5c0,-0.4 -0.1,-0.7 0,-1.1c-0.2,-0.7 -0.2,-1.4 -0.1,-2.1c0,-0.1 0,-0.2 0,-0.3c-0.1,-0.6 -0.1,-1.2 0,-1.8c-1,-1.3 -0.3,-2.9 -0.3,-4.3c-0.1,-28.7 -0.1,-57.3 -0.1,-86C68,-0.1 35.9,-0.1 3.8,-0.2C0.7,-0.2 0,0.5 0,3.6c0.1,32.1 0.1,64.2 0.1,96.2c31,0 62,-0.1 92.9,0.1c3.6,0 6.3,-0.2 7.5,-3.2C100.5,96.4 100.5,95.9 100.6,95.5zM46.3,95.2C26.4,94 2,74.4 3.8,46.8C5.1,27.2 24.4,2.7 52.6,4.6c20.2,1.4 43,21.3 41.5,45.1C96.1,72.4 73,96.8 46.3,95.2z"
          android:fillColor="#6200EE"/>
    </vector>
    

    in my case I created a vector and changed the android:fillColor="#6200EE"

    by the color of my background

      <ImageView
        android:id="@+id/iv_profile_image"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:contentDescription="@string/app_name"
        app:srcCompat="@color/colorPrimaryDark" />
    
    <ImageView
        android:id="@+id/container_profile_image"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:contentDescription="@string/app_name"
        app:srcCompat="@drawable/ic_bg_picture"/>
    

    0 讨论(0)
  • 2020-11-22 11:01

    I use shape = "oval" instead of the "ring" below. It has worked for me. To keep the image within bounds, I use <padding> and set <adjustViewBounds> to true in my <ImageView>. I have tried with images of size between 50 x 50 px upto 200x200 px .

    0 讨论(0)
  • 2020-11-22 11:01

    Just use these lines of code and you are done :

    <de.hdodenhof.circleimageview.CircleImageView
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:clickable="true"
                app:civ_border_width="3dp"
                app:civ_border_color="#FFFFFFFF"
                android:id="@+id/profile"
                android:layout_width="200dp"
                android:layout_height="200dp"
                android:layout_below="@+id/header_cover_image"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="-130dp"
                android:elevation="5dp"
                android:padding="20dp"
                android:scaleType="centerCrop"
                android:src="@drawable/profilemain" />
    

    Don't forget to import :

    import de.hdodenhof.circleimageview.CircleImageView;
    

    Add this library in build.gradle :

    compile 'de.hdodenhof:circleimageview:2.1.0'
    
    0 讨论(0)
  • 2020-11-22 11:01

    This Class is Custom Circular Imageview with shadow, Stroke,saturation and using this Custom Circular ImageView you can make your image in Circular Shape with Radius. Guys for Circular Shadow ImageView No need Github this class is enough.

    Adding CircularImageView to your layout

    CircularImageView c=new CircularImageView(this,screen width,screen height,Bitmap myimage);
    yourLayout.addView(c);**
    
    
    public class CircularImageView extends android.support.v7.widget.AppCompatImageView  
    {
        private final Context context;
        private final int width, height;
        private final Paint paint;
        private final Paint paintBorder,imagePaint;
        private final Bitmap bitmap2;
        private final Paint paint3;
        private Bitmap bitmap;
        private BitmapShader shader;
        private float radius = 4.0f;
        float x = 0.0f;
        float y = 8.0f;
        private float stroke;
        private float strokeWidth = 0.0f;
        private Bitmap bitmap3;
        private int corner_radius=50;
    
    
        public CircularImageView(Context context, int width, int height, Bitmap bitmap)     {
            super(context);
            this.context = context;
            this.width = width;
            this.height = height;
    
       //here "bitmap" is the square shape(width* width) scaled bitmap ..
    
            this.bitmap = bitmap;
    
    
            paint = new Paint(Paint.ANTI_ALIAS_FLAG);
            paint.setAntiAlias(true);
            paint.setFilterBitmap(true);
            paint.setDither(true);
    
    
            paint3=new Paint();
            paint3.setStyle(Paint.Style.STROKE);
            paint3.setColor(Color.WHITE);
            paint3.setAntiAlias(true);
    
            paintBorder = new Paint();
            imagePaint= new Paint();
    
            paintBorder.setColor(Color.WHITE);
            paintBorder.setAntiAlias(true);
            this.setLayerType(LAYER_TYPE_SOFTWARE, paintBorder);
    
    
            this.bitmap2 = Bitmap.createScaledBitmap(bitmap, (bitmap.getWidth() - 40), (bitmap.getHeight() - 40), true);
    
    
            imagePaint.setAntiAlias(true);
    
    
    
    
            invalidate();
        }
    
        @Override
        protected void onDraw(Canvas canvas) 
        {
            super.onDraw(canvas);
            Shader b;
             if (bitmap3 != null)
                b = new BitmapShader(bitmap3, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
             else
                b = new BitmapShader(bitmap2, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
            imagePaint.setShader(b);
            canvas.drawBitmap(maskedBitmap(), 20, 20, null);
        }
    
        private Bitmap maskedBitmap()
        {
            Bitmap l1 = Bitmap.createBitmap(width,width, Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(l1);
            paintBorder.setShadowLayer(radius, x, y, Color.parseColor("#454645"));
            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
            final RectF rect = new RectF();
            rect.set(20, 20, bitmap2.getWidth(), bitmap2.getHeight());
    
            canvas.drawRoundRect(rect, corner_radius, corner_radius, paintBorder);
    
            canvas.drawRoundRect(rect, corner_radius, corner_radius, imagePaint);
    
            if (strokeWidth!=0.0f)
            {
                paint3.setStrokeWidth(strokeWidth);
                canvas.drawRoundRect(rect, corner_radius, corner_radius, paint3);
            }
    
             paint.setXfermode(null);
            return l1;
        }
    
    
    
    
         // use seekbar here, here you have to pass  "0 -- 250"  here corner radius will change 
    
        public void setCornerRadius(int corner_radius)
        {
            this.corner_radius = corner_radius;
            invalidate();
        }
    
    
    
        -------->use seekbar here, here you have to pass  "0 -- 10.0f"  here shadow radius will change 
    
        public void setShadow(float radius)
        {
            this.radius = radius;
            invalidate();
        }
    
       // use seekbar here, here you have to pass  "0 -- 10.0f"  here stroke size  will change 
    
        public void setStroke(float stroke)
        {
            this.strokeWidth = stroke;
            invalidate();
        }
    
        private Bitmap updateSat(Bitmap src, float settingSat)
        {
    
            int w = src.getWidth();
            int h = src.getHeight();
    
            Bitmap bitmapResult =
                    Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
            Canvas canvasResult = new Canvas(bitmapResult);
            Paint paint = new Paint();
            ColorMatrix colorMatrix = new ColorMatrix();
            colorMatrix.setSaturation(settingSat);
            ColorMatrixColorFilter filter = new ColorMatrixColorFilter(colorMatrix);
            paint.setColorFilter(filter);
            canvasResult.drawBitmap(src, 0, 0, paint);
    
            return bitmapResult;
        }
    
    
    
    
      // use seekbar here, here you have to pass  "0 -- 2.0f"  here saturation  will change 
    
        public void setSaturation(float sat)
        {
            System.out.println("qqqqqqqqqq            "+sat);
            bitmap3=updateSat(bitmap2, sat);
    
            invalidate();
        } 
    
    
    }
    
    
    
    
    
    
            // Seekbar to change radius
    
                      radius_seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                            @Override
                            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
                            {
                                text_radius.setText(""+progress);
                                circularImageView.setCornerRadius(progress);
                            }
    
                            @Override
                            public void onStartTrackingTouch(SeekBar seekBar) {
    
                            }
    
                            @Override
                            public void onStopTrackingTouch(SeekBar seekBar) {
    
                            }
                        });
    
    
         // Seekbar to change shadow
    
                        shadow_seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                            @Override
                            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
                            {
                                float f= 4+progress/10.0f;
                                text_shadow.setText(""+progress);
                                circularImageView.setShadow(f);
                            }
    
                            @Override
                            public void onStartTrackingTouch(SeekBar seekBar) {
    
                            }
    
                            @Override
                            public void onStopTrackingTouch(SeekBar seekBar) {
    
                            }
                        });
    
    
               // Seekbar to change saturation
    
                        saturation_seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                            @Override
                            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
                            {
                                int progressSat = saturation_seekbar.getProgress();
                                float sat = (float) ((progressSat*4 / 100.0f)-1.0f);
                                circularImageView.setSaturation(sat);
    
                                text_saturation.setText(""+progressSat);
                            }
    
                            @Override
                            public void onStartTrackingTouch(SeekBar seekBar) {
    
                            }
    
                            @Override
                            public void onStopTrackingTouch(SeekBar seekBar) {
    
                            }
                        });
    
    
        // Seekbar to change stroke
    
                        stroke_seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                            @Override
                            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
                            {
                                if (progress==0)
                                {
                                    float f=(progress*10.0f/100.0f);
                                    circularImageView.setStroke(f);
                                }
                                else
                                {
                                    float f=(progress*10.0f/100.0f);
                                    circularImageView.setStroke(f);
                                }
    
                                text_stroke.setText(""+progress);
                            }
    
                            @Override
                            public void onStartTrackingTouch(SeekBar seekBar) {
    
                            }
    
                            @Override
                            public void onStopTrackingTouch(SeekBar seekBar) {
    
                            }
                        });
    
    
    
    
                 //radius seekbar in xml file
    
                 <SeekBar
                    android:layout_width="match_parent"
                    android:layout_gravity="center" 
                    android:progress="50"
                    android:max="250"
                    android:id="@+id/radius_seekbar"
                    android:layout_height="wrap_content" />
    
    
    
    
    
              //saturation seekbar in xml file
    
                 <SeekBar
                    android:layout_width="match_parent"
                    android:layout_gravity="center" 
                    android:progress="50"
                    android:max="100"
                    android:id="@+id/saturation_seekbar"
                    android:layout_height="wrap_content" />
    
    
    
    
    
        //shadow seekbar in xml file
    
                 <SeekBar
                    android:layout_width="match_parent"
                    android:layout_gravity="center" 
                    android:progress="0"
                    android:max="100"
                    android:id="@+id/shadow_seekbar"
                    android:layout_height="wrap_content" />
    
    
    
    
             //stroke seekbar in xml file
    
                 <SeekBar
                    android:layout_width="match_parent"
                    android:layout_gravity="center" 
                    android:progress="0"
                    android:max="100"
                    android:id="@+id/stroke _seekbar"
                    android:layout_height="wrap_content" />
    
    0 讨论(0)
  • 2020-11-22 11:01

    if you'd rather cut the image to display in circular, here you go

    public static Bitmap getCircularBitmap(Bitmap bitmap) {
            Bitmap output;
    
            if (bitmap.getWidth() > bitmap.getHeight()) {
                output = Bitmap.createBitmap(bitmap.getHeight(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
            } else {
                output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getWidth(), Bitmap.Config.ARGB_8888);
            }
    
            Canvas canvas = new Canvas(output);
    
            final int color = 0xff424242;
            final Paint paint = new Paint();
            final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    
            float r = 0;
    
            if (bitmap.getWidth() > bitmap.getHeight()) {
                r = bitmap.getHeight() / 2;
            } else {
                r = bitmap.getWidth() / 2;
            }
    
            paint.setAntiAlias(true);
            canvas.drawARGB(0, 0, 0, 0);
            paint.setColor(color);
            canvas.drawCircle(r, r, r, paint);
            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
            canvas.drawBitmap(bitmap, rect, rect, paint);
            return output;
        }
    
    0 讨论(0)
提交回复
热议问题