本文实例讲述了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程序设计有所帮助。

点赞(67)

评论列表共有 0 条评论

立即
投稿
返回
顶部