Android開發實現圓形圖片功能示例

這篇文章主要介紹了Android開發實現圓形圖片功能,涉及Android實現圓形圖片的界面佈局與CirImageView組件相關使用操作技巧,需要的朋友可以參考下

本文實例講述了Android開發實現圓形圖片功能。分享給大家供大家參考,具體如下:

**絕對佈局:通過直接給定控件起始座標 ( x , y ) 和 ( w , l ) ,來生成控件。

圓形頭像:CircleImageView的使用 **

注:在build.gradle中添加:

implementation 'de.hdodenhof:circleimageview:1.3.0'

XML佈局文件:

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
  android:id="@+id/root"
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".Home"
  android:layout_gravity="center">
  <!--定義一個文本框用於存放頭像,使用絕對佈局-->
  <de.hdodenhof.circleimageview.CircleImageView
    android:id="@+id/imageview"
    android:layout_x="150dp"
    android:layout_y="75dp"
    android:layout_width="100dp"
    android:layout_height="100dp"/>
  <!--定義一個文本框,使用絕對定位-->
  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_x="20dp"
    android:layout_y="225dp"
    android:text="用戶名:"/>
  <!--定義一個文本編輯框,使用絕對定位-->
  <EditText
    android:layout_x="80dp"
    android:layout_y="215dp"
    android:hint="郵箱/手機/用戶名"
    android:layout_width="wrap_content"
    android:width="275dp"
    android:layout_height="wrap_content"
    android:singleLine="true" />
  <!--定義一個文本框使用絕對定位-->
  <TextView
    android:layout_x="20dp"
    android:layout_y="285dp"
    android:text=" 密 碼 :"
    android:gravity="center"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
  <!--定義一個文本編輯框,使用絕對定位-->
  <EditText
    android:layout_x="80dp"
    android:layout_y="275dp"
    android:hint="密碼/驗證碼"
    android:layout_width="wrap_content"
    android:width="275dp"
    android:layout_height="wrap_content"
    android:password="true"
    android:singleLine="true" />
  <!--定義一個按鈕,使用絕對定位-->
  <Button
    android:layout_x="100dp"
    android:layout_y="350dp"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:text=" 登 錄 "/>
</AbsoluteLayout>

Java代碼 動態設置頭像:

//點擊 切換圖片
public class Home extends AppCompatActivity {
  private LinearLayout mainLayout=null;
  private ImageView iv=null;
  //定義一個訪問圖片的數組
  int[] images = new int[]{//放置你的圖片
      R.drawable.gass,
      R.drawable.gonzhixiaochou
  };
  //用於圖片切換
  int currenImg = 0;
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);//顯示manLayout
    //創建CirImageView組件
    final CircleImageView circleimageView01 = (CircleImageView) findViewById(R.id.imageview);
    //設置CirImageView背景
    circleimageView01.setImageResource(images[0]);
    circleimageView01.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        //切換背景
        circleimageView01.setImageResource(images[++currenImg % images.length]);
      }
    });
  }
}

效果:

更多關於Android相關內容感興趣的讀者可查看本站專題:《Android圖形與圖像處理技巧總結》、《Android開發入門與進階教程》、《Android調試技巧與常見問題解決方法彙總》、《Android基本組件用法總結》、《Android視圖View技巧總結》、《Android佈局layout技巧總結》及《Android控件用法總結

希望本文所述對大家Android程序設計有所幫助。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章