Round ImageView Android -
i working on android application in doing rounded image view. working fine images, images 160x120 resolution shows oval shaped. code custom imageview given below, please me out here:
public class roundimage extends drawable { private final bitmap mbitmap; private final paint mpaint; private final rectf mrectf; private final int mbitmapwidth; private final int mbitmapheight; public roundimage(bitmap bitmap) { mbitmap = bitmap; mrectf = new rectf(); mpaint = new paint(); mpaint.setantialias(true); mpaint.setdither(true); final bitmapshader shader = new bitmapshader(bitmap, shader.tilemode.clamp, shader.tilemode.clamp); mpaint.setshader(shader); mbitmapwidth = mbitmap.getwidth(); mbitmapheight = mbitmap.getheight(); } @override public void draw(canvas canvas) { canvas.drawoval(mrectf, mpaint); } @override protected void onboundschange(rect bounds) { super.onboundschange(bounds); mrectf.set(bounds); } @override public void setalpha(int alpha) { if (mpaint.getalpha() != alpha) { mpaint.setalpha(alpha); invalidateself(); } } @override public void setcolorfilter(colorfilter cf) { mpaint.setcolorfilter(cf); } @override public int getopacity() { return pixelformat.translucent; } @override public int getintrinsicwidth() { return mbitmapwidth; } @override public int getintrinsicheight() { return mbitmapheight; } public void setantialias(boolean aa) { mpaint.setantialias(aa); invalidateself(); } @override public void setfilterbitmap(boolean filter) { mpaint.setfilterbitmap(filter); invalidateself(); } @override public void setdither(boolean dither) { mpaint.setdither(dither); invalidateself(); } public bitmap getbitmap() { return mbitmap; } } // main activity class public class mainactivity extends activity { imageview imageview1, imageview2; roundimage roundedimage, roundedimage1; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); imageview1 = (imageview) findviewbyid(r.id.imageview1); bitmap bm = bitmapfactory.decoderesource(getresources(),r.drawable.tt); roundedimage = new roundimage(bm); imageview1.setimagedrawable(roundedimage); } } // xml <?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white" android:orientation="vertical" > <imageview android:id="@+id/imageview1" android:layout_width="100dp" android:layout_height="100dp" android:scaletype="centercrop" android:layout_gravity="center" android:background="@color/brown" android:src="@drawable/image" /> </linearlayout>
add gradle in android project
compile 'com.mikhaellopez:circularimageview:3.0.2'
and added line in xml file (layout files)
<com.mikhaellopez.circularimageview.circularimageview android:layout_width="250dp" android:layout_height="250dp" android:src="@drawable/image" app:civ_border_color="#eeeeee" app:civ_border_width="4dp" app:civ_shadow="true" app:civ_shadow_radius="10" app:civ_shadow_color="#8bc34a"/>
in java code
circularimageview circularimageview = (circularimageview)findviewbyid(r.id.yourcircularimageview); // set border circularimageview.setbordercolor(getresources().getcolor(r.color.graylight)); circularimageview.setborderwidth(10); // add shadow default param circularimageview.addshadow(); // or custom param circularimageview.setshadowradius(15); circularimageview.setshadowcolor(color.red);
Comments
Post a Comment