怎么在Android中实现一个圆形图片功能

发布时间:2021-04-07 15:47:22 作者:Leah
来源:亿速云 阅读:214

怎么在Android中实现一个圆形图片功能?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

注:在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]);
      }
    });
  }
}

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

推荐阅读:
  1. 怎么在Android中实现相机圆形预览
  2. 怎么在Android中实现一个图片点击切换功能

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android

上一篇:使用OpenGL怎么实现一个Bezier曲线

下一篇:如何在Python中应用匿名函数

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》