Android自定义View实现不断旋转的圆形图片

本文介绍如何在Android中通过自定义View创建一个圆形图片,并使其不断匀速旋转。通过四个步骤实现:定义自定义属性、构造方法、重写onMeasure和onDraw方法。在实践中遇到Bitmap尺寸获取问题,作者对此提出疑问。
摘要由CSDN通过智能技术生成

自定义View是android开发的一个重要技能,用android提供的2/3D绘制相关类可以实现非常多炫酷的效果,需要实打实的编程基础。(吧)。

但是自定义View又是我的弱项,所以最近都在摸索、练习自定义View。今天我写了一个圆形图片,同时不断匀速旋转的RotateCircleImageView。实现方法是自己想的,但肯定不是最好的实现方法。

自定义View分四步。

一:自定义属性;

二:创建自定义View,在构造方法中拿到自定义属性;

三:重写onMeasure方法;

四:重写onDraw方法

先来个效果图


先在res/values/下新建attrs.xml

自定义属性

<declare-styleable name="RotateCircleImageView">
        <attr name="image" format="reference" />
        <attr name="rotate_sd" format="float" />
        <attr name="rotate_fx" format="integer" />
        <attr name="isRotate" format="boolean" />
        <attr name="circle_back_width" format="dimension" />
        <attr name="circle_back_color" format="color" />
    </declare-styleable>



创建RotateCircleImageView

public RotateCircleImageView(Context context) {
        this(context, null);
    }

    public RotateCircleImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RotateCircleImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initData();
    }

重写View的三个构造函数,用一参的调用二参的,用二参的调用三参的。在三参的构造里初始化参数。


private Bitmap image;
private Bitmap tempImage;
private Paint paint;
private int bkWidth;//黑色圆边框的宽度
private int rotate_fx=0;//旋转方向  0=顺时针 1=逆时针
private float rotateSD = 0.8f;//每次旋转的角度--建议范围0.1f-1,否则会抖动
private boolean isRotate = false;//控制是否旋转
  private void initData() {
	  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值