Android 實(shí)現(xiàn)圓角圖片的簡(jiǎn)單實(shí)例
Android 實(shí)現(xiàn)圓角圖片的簡(jiǎn)單實(shí)例
實(shí)現(xiàn)效果圖:

本來(lái)想在網(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)聽(tīng)、圓角圖片)
- android 實(shí)現(xiàn)圓角圖片解決方案
- android 設(shè)置圓角圖片實(shí)現(xiàn)代碼
- Android實(shí)現(xiàn)圓角圖片的方法
相關(guān)文章
Android開(kāi)發(fā)筆記之:返回鍵的復(fù)寫(xiě)onBackPressed()介紹
本篇文章是對(duì)Android中返回鍵的復(fù)寫(xiě)onBackPressed()進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
android viewpager實(shí)現(xiàn)豎直滑動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了android viewpager實(shí)現(xiàn)豎直滑動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
AlertDialog點(diǎn)擊按鈕不消失的實(shí)現(xiàn)方法
我有一個(gè)文本輸入對(duì)話框,當(dāng)我點(diǎn)擊對(duì)話框上的“是”按鈕,它會(huì)驗(yàn)證輸入,然后關(guān)閉對(duì)話框,但是,如果輸入錯(cuò)誤,我想停留在同一個(gè)對(duì)話框中。怎么實(shí)現(xiàn)此功能呢?下面通過(guò)本文給大家分享下2017-01-01
Android RecyclerView添加FootView和HeadView
這篇文章主要介紹了Android RecyclerView添加FootView和HeadView的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
Android開(kāi)發(fā)之merge結(jié)合include優(yōu)化布局
這篇文章主要為大家詳細(xì)介紹了Android開(kāi)發(fā)之merge結(jié)合include優(yōu)化布局,感興趣的朋友可以參考一下2016-06-06
Android自定義Gradle插件的詳細(xì)過(guò)程
Groovy語(yǔ)言是一種jvm語(yǔ)言,最終也是編譯成class文件然后在jvm上執(zhí)行,所以所有的Java語(yǔ)言的特性Groovy都支持,我們可以完全混寫(xiě)Java和Groovy,對(duì)Android自定義Gradle插件相關(guān)知識(shí),感興趣的朋友跟隨小編一起看看吧2021-07-07
Android開(kāi)發(fā)中amera2 Preview使用詳解
這篇文章主要介紹了Android開(kāi)發(fā)中amera2 Preview使用詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09
Android 添加TextView刪除線(代碼簡(jiǎn)單)
最近接了個(gè)項(xiàng)目,其中有項(xiàng)目需求是這樣的,有這么個(gè)需求,就是一個(gè)產(chǎn)品下有兩個(gè)價(jià)格,一個(gè)是市場(chǎng)價(jià),一個(gè)是銷(xiāo)售價(jià),這時(shí)要把市場(chǎng)價(jià)添加個(gè)刪除線;怎么實(shí)現(xiàn)呢?下面小編給大家分享一段簡(jiǎn)單的代碼實(shí)現(xiàn)Android 添加TextView刪除線2016-02-02

