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

剛在看自定義View的知識點時,突然想起來,如果CardView寬高相等,CardView設置圓角的半徑為寬高的一半時,不就是一個圓形嘛?!
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,設置圓角半徑為100dp
2.Aciticy中代碼
使用的Glide進行圖片加載
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)這個方法可以用來判斷Activity是否已經(jīng)獲得焦點,這個時候可以拿到控件的寬和高,是拿到控件寬高的一種辦法。
3.總結
以前都是使用一個自定義的CircleImageView,如果只是想要一個圓形圖,這種方法也可以考慮使用。不曉得這種方法有沒有人使用過。圖上添加文字,也蠻方便。感覺這種方式還比較簡單一些。以上就是這篇文章的全部內容,希望對大家的開發(fā)能有所幫助。
相關文章
flutter實現(xiàn)掃碼槍獲取數(shù)據(jù)源禁止系統(tǒng)鍵盤彈窗示例詳解
這篇文章主要為大家介紹了flutter實現(xiàn)掃碼槍獲取數(shù)據(jù)源禁止系統(tǒng)鍵盤彈窗示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-01-01
Android基于google Zxing實現(xiàn)各類二維碼掃描效果
這篇文章主要介紹了Android基于google Zxing實現(xiàn)各類二維碼掃描效果的相關資料,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-02-02
Android Handler之消息循環(huán)的深入解析
本篇文章是對Handler消息循環(huán)進行了詳細的分析介紹,需要的朋友參考下2013-05-05
Android基于CountDownTimer實現(xiàn)倒計時功能
這篇文章主要介紹了Android基于CountDownTimer實現(xiàn)倒計時功能,簡單分析了基于CountDownTimer類實現(xiàn)倒計時功能的技巧,需要的朋友可以參考下2015-12-12
Android實現(xiàn)右邊抽屜Drawerlayout效果
這篇文章主要為大家詳細介紹了Android實現(xiàn)右邊抽屜Drawerlayout效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11

