android: create circular image with picasso
Research a bit before as there are answers available. Anyhow, follow This Link and read it carefully to know how to use it.
try this:
import com.squareup.picasso.Transformation;
public class CircleTransform 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,
Shader.TileMode.CLAMP, Shader.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";
}
}
then simply apply it like:
Picasso.with(activity).load(mayorShipImageLink).transform(new CircleTransform()).into(ImageView);
Android round image with picasso
The error message is pretty clear. You simply forgot to recycle the original Bitmap.
....
canvas.drawBitmap(source, rect, rect, paint);
source.recycle();
return output;
Just one line missing from your code! (I'm amazed at all these answers telling you to do all sorts of unrelated, uprooting solutions.)
Create circular image using picasso
I have Created a class CircleTransform.java
to make the image to circle and modified the code to load image from like,
Picasso.with(getApplicationContext()).load(R.drawable.profile_sample).placeholder(setCircularImage(R.drawable.profile_sample)).transform(new CircleTransform()).into(imageView_ProfilePic);
also from the url like,
Picasso.with(getApplicationContext()).load("http://shidhints.com").placeholder(setCircularImage(R.drawable.profile_sample)).transform(new CircleTransform()).into(imageView_ProfilePic);
CircleTransform.java
public class CircleTransform 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";
}
}
Using picasso library with a circle image view
Use This
Activity Class
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String imageUrl = "https://www.baby-connect.com/images/baby2.gif";
CircleImageView imageView = (CircleImageView) findViewById(R.id.image);
Picasso.with(getApplicationContext()).load(imageUrl)
.placeholder(R.drawable.images).error(R.drawable.ic_launcher)
.into(imageView);
}
}
Layout File
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/image"
android:layout_width="160dp"
android:layout_height="160dp"
android:layout_centerInParent="true"
android:src="@drawable/images"
app:border_color="#ffffff"
app:border_width="2dp" />
This is Working fine.
Make ImageView with Round Corner Using picasso
I am using this transformation:
https://gist.github.com/julianshen/5829333
Picasso.with(activity).load(url).transform(new CircleTransform()).into(imageView);
Adding borders for image rounded image android
you can use drawCircle
with another Paint object
. For instance:
Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setStyle(Style.STROKE);
paint.setAntiAlias(true);
paint.setStrokeWidth(2);
canvas.drawCircle((source.getWidth() - margin)/2, (source.getHeight() - margin)/2, radius-2, paint);
Also, instead of using a drawRoundRect
to draw a circle, you can use drawCircle
ImageView in circular through XML
You can make a simple circle with white border and transparent content with shape.
// res/drawable/circle.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadius="0dp"
android:shape="ring"
android:thicknessRatio="1.9"
android:useLevel="false" >
<solid android:color="@android:color/transparent" />
<stroke
android:width="10dp"
android:color="@android:color/white" />
</shape>
Then make a layerlist drawable and put it as background to your imageview.
// res/drawable/img.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/circle"/>
<item android:drawable="@drawable/ic_launcher"/>
</layer-list>
and put it as background to your imageview.
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/img"/>
You'll have something like that.
Related Topics
Access Pictures from Pictures App in My Android App
How to Avoid Delay in Android Gcm Messages/Change Heartbeat
Difference Between a Clickable Imageview and Imagebutton
Android Hello, Gallery Tutorial -- "R.Styleable Cannot Be Resolved"
How to Add Action Bar Options Menu in Android Fragments
Enabling Wifi on Android Emulator
Sending Mail in Android Without Intents Using Smtp
Running Task Periodicaly(Once a Day/Once a Week)
Android Pagerview Between Activities
Android:Split the Screen in 2 Equals Parts with 2 Listviews
Fileprovider Crash - Npe Attempting to Invoke Xmlresourceparser on a Null String
Android: Adb Pull File on Desktop
Ontouchevent Not Working on Child Views
Understanding Android's <Layer-List>
Make a Hyperlink Textview in Android
How to Customize List Preference Radio Button
Bitmap Is Returning Null from Bitmapfactory.Decodefile(Filename)