分享一個(gè)Android設(shè)置圓形圖片的特別方法
Cardview配合ImageView顯示圓形圖效果圖:

剛在看自定義View的知識(shí)點(diǎn)時(shí),突然想起來,如果CardView寬高相等,CardView設(shè)置圓角的半徑為寬高的一半時(shí),不就是一個(gè)圓形嘛?!
1.布局文件
<android.support.v7.widget.CardView android:id="@+id/cv_img_activity" android:layout_width="200dp" android:layout_height="200dp" app:cardCornerRadius="100dp" app:cardElevation="10dp" app:cardPreventCornerOverlap="true"> <ImageView android:id="@+id/iv_cv_img_activity" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="centerCrop"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/colorAccent" android:textSize="30sp" android:layout_gravity="bottom|center_horizontal" android:text="圓"/> </android.support.v7.widget.CardView>
CardView繼承的FramLayout。寬和高都為200dp,設(shè)置圓角半徑為100dp
2.Aciticy中代碼
使用的Glide進(jìn)行圖片加載
public class ImgActivity extends AppCompatActivity {
private static final String url = "https://timgsa.baidu.com/timg?image&quality=80&size=b10000_10000&sec=1473082549776&di=cb9749bd976beb119da065ee56ebbc60&imgtype=jpg&src=http%3A%2F%2Fstatic.oneplus.cn%2Fdata%2Fattachment%2Fforum%2F201410%2F18%2F111437v9ll9869qqmq76n7.jpg";
private CardView cardView;
private ImageView iv_cv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_img);
init();
}
private void init() {
cardView = (CardView) findViewById(R.id.cv_img_activity);
iv_cv = (ImageView) findViewById(R.id.iv_cv_img_activity);
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus){
Glide.with(ImgActivity.this).load(url).override(cardView.getWidth(),cardView.getHeight()).into(iv_cv);
}
}
}
onWindowFocusChanged(boolean hasFocus)這個(gè)方法可以用來判斷Activity是否已經(jīng)獲得焦點(diǎn),這個(gè)時(shí)候可以拿到控件的寬和高,是拿到控件寬高的一種辦法。
3.總結(jié)
以前都是使用一個(gè)自定義的CircleImageView,如果只是想要一個(gè)圓形圖,這種方法也可以考慮使用。不曉得這種方法有沒有人使用過。圖上添加文字,也蠻方便。感覺這種方式還比較簡單一些。以上就是這篇文章的全部內(nèi)容,希望對(duì)大家的開發(fā)能有所幫助。
- Android裁剪圖片為圓形圖片的實(shí)現(xiàn)原理與代碼
- Android中Glide加載圓形圖片和圓角圖片實(shí)例代碼
- Android實(shí)現(xiàn)圓形圖片的兩種方式
- 詳解Android中Glide與CircleImageView加載圓形圖片的問題
- Android編程繪制圓形圖片的方法
- Android自定義View實(shí)現(xiàn)旋轉(zhuǎn)的圓形圖片
- android繪制圓形圖片的兩種方式示例
- Android實(shí)現(xiàn)圓形圖片或者圓角圖片
- Android使用自定義ImageView實(shí)現(xiàn)圓形圖片效果
- Android實(shí)現(xiàn)圓形圖片效果
相關(guān)文章
Android中自定義ContentProvider實(shí)例
應(yīng)用A(TestBaidu)調(diào)用另外一個(gè)應(yīng)用(TestContentProvider)即自定義ContentProvider的使用,其它應(yīng)用調(diào)用該ContentProvider,具體如下,感興趣的朋友可以參考下哈2013-06-06
Android 中WallpaperManager用法實(shí)例
這篇文章主要介紹了Android 中WallpaperManager用法實(shí)例的相關(guān)資料,希望通過本文能幫助到大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下2017-09-09
flutter實(shí)現(xiàn)掃碼槍獲取數(shù)據(jù)源禁止系統(tǒng)鍵盤彈窗示例詳解
這篇文章主要為大家介紹了flutter實(shí)現(xiàn)掃碼槍獲取數(shù)據(jù)源禁止系統(tǒng)鍵盤彈窗示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
Android基于google Zxing實(shí)現(xiàn)各類二維碼掃描效果
這篇文章主要介紹了Android基于google Zxing實(shí)現(xiàn)各類二維碼掃描效果的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-02-02
Android Handler之消息循環(huán)的深入解析
本篇文章是對(duì)Handler消息循環(huán)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
Android基于CountDownTimer實(shí)現(xiàn)倒計(jì)時(shí)功能
這篇文章主要介紹了Android基于CountDownTimer實(shí)現(xiàn)倒計(jì)時(shí)功能,簡單分析了基于CountDownTimer類實(shí)現(xiàn)倒計(jì)時(shí)功能的技巧,需要的朋友可以參考下2015-12-12
Android實(shí)現(xiàn)右邊抽屜Drawerlayout效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)右邊抽屜Drawerlayout效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11

