前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android开发实现圆形图片功能示例

Android开发实现圆形图片功能示例

作者头像
砸漏
发布2020-11-05 10:21:30
5220
发布2020-11-05 10:21:30
举报
文章被收录于专栏:恩蓝脚本恩蓝脚本

本文实例讲述了Android开发实现圆形图片功能。分享给大家供大家参考,具体如下:

**绝对布局:通过直接给定控件起始坐标 ( x , y ) 和 ( w , l ) ,来生成控件。 圆形头像:CircleImageView的使用 **

注:在build.gradle中添加:

代码语言:javascript
复制
implementation 'de.hdodenhof:circleimageview:1.3.0'

XML布局文件:

代码语言:javascript
复制
<?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代码 动态设置头像:

代码语言:javascript
复制
//点击 切换图片
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程序设计有所帮助。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
验证码
腾讯云新一代行为验证码(Captcha),基于十道安全栅栏, 为网页、App、小程序开发者打造立体、全面的人机验证。最大程度保护注册登录、活动秒杀、点赞发帖、数据保护等各大场景下业务安全的同时,提供更精细化的用户体验。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档