Android 实现圆角图片的简单实例
程序员文章站
2022-04-13 22:46:53
android 实现圆角图片的简单实例
实现效果图:
本来想在网上找个圆角的例子看一看,不尽人意啊,基本都是官方的demo的那张原理图,稍后会贴出。于是自己自定...
android 实现圆角图片的简单实例
实现效果图:
本来想在网上找个圆角的例子看一看,不尽人意啊,基本都是官方的demo的那张原理图,稍后会贴出。于是自己自定义了个view,实现图片的圆角以及圆形效果。效果图:
android 圆角图片的实现形式,包括用第三方、也有系统的。比如makeramen:roundedimageview,系统的cardview , glide .fresco 。
compile 'com.android.support:appcompat-v7:24.0.0' compile 'com.makeramen:roundedimageview:2.2.1' compile 'com.android.support:cardview-v7:24.0.0' compile 'com.github.bumptech.glide:glide:3.7.0' compile 'com.facebook.fresco:fresco:0.12.0'
<android.support.v7.widget.cardview xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/id_cardview" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" app:cardbackgroundcolor="@color/bg_light_gray" app:cardcornerradius="3dp" app:cardusecompatpadding="false" app:cardpreventcorneroverlap="true" > <imageview android:id="@+id/iv_subject" android:gravity="center" android:scaletype="centercrop" android:layout_width="match_parent" android:layout_height="200dp" /> <textview android:paddingleft="5dp" android:paddingbottom="5dp" android:background="@drawable/bg_biaoti" android:id="@+id/tv_subject" android:gravity="center_vertical" android:text="" android:ellipsize="end" android:singleline="true" android:textsize="13sp" android:textcolor="@color/white" android:layout_gravity="bottom" android:layout_width="match_parent" android:layout_height="wrap_content" /> </android.support.v7.widget.cardview>
iv_round=(roundedimageview) findviewbyid(r.id.iv_round); glide.with(this).load(url).into(iv_round);
iv_cardview=(imageview)findviewbyid(r.id.iv_cardview); glide.with(this).load(url).into(iv_cardview);
iv_fresco=(simpledraweeview)findviewbyid(r.id.iv_fresco); glide.with(this).load(url).into(iv_round); glide.with(this).load(url).into(iv_cardview); uri uri = uri.parse(url); iv_fresco.setimageuri(uri);
package roundimageview.forezp.com.roundimageview; import android.content.context; import android.content.res.resources; import android.graphics.bitmap; import android.graphics.bitmapshader; import android.graphics.canvas; import android.graphics.paint; import android.graphics.rectf; import com.bumptech.glide.load.engine.bitmap_recycle.bitmappool; import com.bumptech.glide.load.resource.bitmap.bitmaptransformation; /** * created by administrator on 2016/8/19 0019. */ public class glideroundtransform extends bitmaptransformation { private static float radius = 0f; public glideroundtransform(context context) { this(context, 4); } public glideroundtransform(context context, int dp) { super(context); this.radius = resources.getsystem().getdisplaymetrics().density * dp; } @override protected bitmap transform(bitmappool pool, bitmap totransform, int outwidth, int outheight) { return roundcrop(pool, totransform); } private static bitmap roundcrop(bitmappool pool, bitmap source) { if (source == null) return null; bitmap result = pool.get(source.getwidth(), source.getheight(), bitmap.config.argb_8888); if (result == null) { result = bitmap.createbitmap(source.getwidth(), source.getheight(), bitmap.config.argb_8888); } canvas canvas = new canvas(result); paint paint = new paint(); paint.setshader(new bitmapshader(source, bitmapshader.tilemode.clamp, bitmapshader.tilemode.clamp)); paint.setantialias(true); rectf rectf = new rectf(0f, 0f, source.getwidth(), source.getheight()); canvas.drawroundrect(rectf, radius, radius, paint); return result; } @override public string getid() { return getclass().getname() + math.round(radius); } }
glide.with(this).load(url).transform(new glideroundtransform(this,6)).into(iv_glide);
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!