Android 實(shí)現(xiàn)圓角圖片的簡(jiǎn)單實(shí)例
Android 實(shí)現(xiàn)圓角圖片的簡(jiǎn)單實(shí)例
實(shí)現(xiàn)效果圖:
本來想在網(wǎng)上找個(gè)圓角的例子看一看,不盡人意啊,基本都是官方的Demo的那張?jiān)韴D,稍后會(huì)貼出。于是自己自定義了個(gè)View,實(shí)現(xiàn)圖片的圓角以及圓形效果。效果圖:
Android 圓角圖片的實(shí)現(xiàn)形式,包括用第三方、也有系統(tǒng)的。比如makeramen:roundedimageview,系統(tǒng)的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);
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- android自定義imageview實(shí)現(xiàn)圓角圖片
- Android實(shí)現(xiàn)圓形圖片或者圓角圖片
- Android實(shí)現(xiàn)帶描邊的圓角圖片
- Android中實(shí)現(xiàn)圓角圖片的幾種方法
- Android中Glide加載圓形圖片和圓角圖片實(shí)例代碼
- Android如何設(shè)置圓角圖片
- Android關(guān)于Glide的使用(高斯模糊、加載監(jiān)聽、圓角圖片)
- android 實(shí)現(xiàn)圓角圖片解決方案
- android 設(shè)置圓角圖片實(shí)現(xiàn)代碼
- Android實(shí)現(xiàn)圓角圖片的方法
相關(guān)文章
舉例講解Android應(yīng)用開發(fā)中OTTO框架的基本使用
這篇文章主要介紹了Android應(yīng)用開發(fā)中OTTO框架的基本使用講解,文中舉了創(chuàng)建一個(gè)單例模式的應(yīng)用例子,需要的朋友可以參考下2016-02-02Android簡(jiǎn)單使用PopupWindow的方法
這篇文章主要為大家詳細(xì)介紹了Android簡(jiǎn)單使用PopupWindow的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-03-03Android使用ViewDragHelper實(shí)現(xiàn)QQ聊天氣泡拖動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Android使用ViewDragHelper實(shí)現(xiàn)QQ聊天氣泡拖動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01Android 源碼淺析RecyclerView ItemAnimator
這篇文章主要為大家介紹了Android 源碼淺析RecyclerView ItemAnimator,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12