How to set an image on ImageView in android?

Total Post:24

Points:170

 1046  View(s)
Ratings:
Rate this:

How to set a circular image on ImageView, coming from the server.

  1. Re: How to set an image on ImageView in android?

    /* Create an object of ImageView */

     ImageView  myImage= (ImageView) findViewById(R.id.edit_image);

    /* Picasso is a powerful image downloading and caching library.*/

            Picasso.with(getApplicationContext())

                    .load("https://www.mysite.com//Contents/images/User%20Image/"+SaveSharedPreference.getPrefUserId(this) + ".png")

                    .transform(new CircleTransformation())

                    .into(myImage);


    CircleTransformation.java

    package com.example.msclient010.mindstickqna.Utils;

    import android.graphics.Bitmap;

    import android.graphics.BitmapShader;

    import android.graphics.Canvas;

    import android.graphics.Paint;

    import com.squareup.picasso.Transformation;

    /**

     * Created by msclient010 on 2/2/2018.

     */

    public class CircleTransformation implements Transformation {

        @Override

        public Bitmap transform(Bitmap source) {

            int size = Math.min(source.getWidth(), source.getHeight());

            int x = (source.getWidth() - size) / 2;

            int y = (source.getHeight() - size) / 2;

            Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);

            if (squaredBitmap != source) {

                source.recycle();

            }

            Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig());

            Canvas canvas = new Canvas(bitmap);

            Paint paint = new Paint();

            BitmapShader shader = new BitmapShader(squaredBitmap,

                    BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);

            paint.setShader(shader);

            paint.setAntiAlias(true);

            float r = size / 2f;

            canvas.drawCircle(r, r, r, paint);

            squaredBitmap.recycle();

            return bitmap;

        }

        @Override

        public String key() {

            return "circle";

        }

    }


      Modified On Aug-22-2018 11:28:52 PM

Answer

Please check, If you want to make this post sponsored

You are not a Sponsored Member. Click Here to Subscribe the Membership.