Android动画特效之自定义View

  Android动画特效之Animator属性动画实现_Angel-杭州的博客-CSDN博客

  我在百忙之中抽出宝贵时间来实现Android动画特效,也就是Android Animator动画效果,使用Animator属性动画来实现平移、缩放、透明度、旋转等动画效果,采用ValueAnimatorObjectAnimator类来满足动画特效,以及ValueAnimatorObjectAnimator类的使用。

  要实现Android动画特效,首先要掌握如何自定义View。因为不管实现Android动画特效,还是工作当中业务需求功能实现,都会经常接触到自定义View,实现自定义View也是重中之重,作为Android开发者,需要自定义View是必不可少的。

自定义view使用步骤如下:

1. 编写布局文件

2. 实现构造方法

3. 初始化UI

4. 提供对外的方法

5. 在布局当中引用该控件

6. activity中使用

因此通过示例来详解如何自定义view。

1.首先编写布局文件layout_center_view.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/rlDeviceView"
    >
    <ImageView
        android:id="@+id/ivDeviceView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        />
</RelativeLayout>

2. 实现构造方法

3. 初始化UI

4. 提供对外的方法

CenterView.java

// 因为我们的布局采用RelativeLayout,所以这里继承RelativeLayout
public class CenterView extends RelativeLayout {
    private static final String TAG = "CenterView";

    private ImageView ivDeviceView;

    private int deviceDpWith;

    private int devicePxWith;

    private Context context;

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

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

    public CenterView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    // 初始化UI,可根据业务需求设置默认值
    private void initView(Context context) {
        this.context = context;
        View view = LayoutInflater.from(context).inflate(R
                .layout.layout_center_view, null, false);
        ivDeviceView = view.findViewById(R.id.ivDeviceView);
        devicePxWith = Constants.CENTER_LAYOUT_PIX_SIZE;
        // 先添加addView后,再获取ivDeviceView.getLayoutParams(),否则会报空
        refreshing();
        addView(view);
    }

    private void refreshing() {
        ivDeviceView.setImageResource(R.mipmap.wifi_icon);
        // 拿到的是ivDeviceView父布局的参数,也就是RelativeLayout。
        LayoutParams params= (LayoutParams) ivDeviceView.getLayoutParams();
        params.width = devicePxWith;
        params.height = devicePxWith;
        ivDeviceView.setLayoutParams(params);
    }

    public void setDeviceWith(int deviceWith) {
        this.deviceDpWith = deviceWith;
        devicePxWith = ScreenUtil.dp2px(context, deviceDpWith);
        refreshing();
    }

    public void setDeivceImageResource(int resId) {
        ivDeviceView.setImageResource(resId);
    }

    // 重写onMeasure方法进行测量
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(measureWith(widthMeasureSpec), measureHeight(heightMeasureSpec));
    }

    private int measureWith(int measureSpec) {
       int specMode = MeasureSpec.getMode(measureSpec);
       int specSize = MeasureSpec.getSize(measureSpec);
        // 设置一个默认值,就是这个View的默认宽度为xxx,
       int result = Constants.CENTER_LAYOUT_PIX_SIZE;
       if (specMode == MeasureSpec.AT_MOST) { // 相当于我们设置成wrap_content
           result = specSize;
       } else if (specMode == MeasureSpec.EXACTLY) { // 相当于我们设置成match_content或者一个具体的值
           result = specSize;
       }
       return result;
    }

    private int measureHeight(int measureSpec) {
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        // 设置一个默认值,就是这个View的默认宽度为xxx,
        int result = Constants.CENTER_LAYOUT_PIX_SIZE;
        if (specMode == MeasureSpec.AT_MOST) { // 相当于我们设置成wrap_content
            result = specSize;
        } else if (specMode == MeasureSpec.EXACTLY) { // 相当于我们设置成match_content或者一个具体的值
            result = specSize;
        }
        return result;
    }
}

上面并不难,都有注释,重要的是通过onMeasure方法需要重新测量自身的view。

  1. 在布局当中引用该控件本示例暂不引用,通过代码去引用它。

在编写一个layout_base_view.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

从上面可以看出,里面没有任何子view,声明RelativeLayout即可。

BaseLayoutView.java

// 因为我们的布局采用RelativeLayout,所以这里继承RelativeLayout
public class BaseLayoutView extends RelativeLayout {
    private static final String TAG = "BaseLayoutView";

    private float centerX = 0f;

    private float centerY = 0f;

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

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

    public BaseLayoutView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    // 初始化UI,可根据业务需求设置默认值
    private void initView(Context context) {
        View view = LayoutInflater.from(context).inflate(R
                .layout.layout_base_view, null, false);
        addView(view);
        centerX = Constants.SCREEN_WIDTH / Constants.FLOAT_TWO;
        centerY = Constants.SCREEN_HEIGHT / Constants.FLOAT_TWO - ScreenUtil.getStatusBarHeight();
        Log.i(TAG, "initView getStatusBarHeight= " + ScreenUtil.getStatusBarHeight());
        Log.i(TAG, "initView centerX= " + centerX + " centerY= " + centerY);
    }

    // 返回中心X坐标
    public float getCenterX() {
        return centerX;
    }

    // 返回中心Y坐标
    public float getCenterY() {
        return centerY;
    }
}

BaseLayoutView是一个基础视图,初始化的时候计算下view的中心坐标,注意:需要减去状态栏的高度。这样我们就可以拿到该BaseLayoutView的中心坐标,并对外提供方法可以访问到BaseLayoutView的x、y坐标。

  这样上面的自定义View已经编好了,分别是CenterView和BaseLayoutView类。接下来我们看activity里面怎么实现呢。

在编写一个activity的activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rl_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

上面可以看出,里面没有任何的子view,仅声明RelativeLayout和id即可。

看下Activity代码怎么写呢。

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private RelativeLayout relativeLayout;

    private SolarControl solarControl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        relativeLayout = findViewById(R.id.rl_layout);
        solarControl = new SolarControl(relativeLayout);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (solarControl != null) {
            solarControl.release();
            solarControl = null;
        }
    }
}

上面可以看出,RelativeLayout实例化一个对象,拿到布局里面的id即可,将RelativeLayout对象传递给SolarControl类即可,剩下的事交给SolarControl类来处理。SolarControl类主要是负责UI显示和业务交互的地方,当前在activity的onDestroy方法内部调用SolarControl类的release()来释放资源,为什么这么做呢?

我们来看下SolarControl类里面做了什么事情。

SolarControl.java

public class SolarControl {
    private static final String TAG = "SolarControl";

    private ViewGroup parentView;

    private BaseLayoutView baseView;

    private CenterView centerView;

    public SolarControl(ViewGroup parent) {
        parentView = parent;
        baseView = new BaseLayoutView(parentView.getContext());
        initView();
    }

    private void initView() {
        centerView = new CenterView(baseView.getContext());
        float centerX = baseView.getCenterX() - Constants.CENTER_LAYOUT_PIX_SIZE / Constants.FLOAT_TWO;
        float centerY = baseView.getCenterY() - Constants.CENTER_LAYOUT_PIX_SIZE / Constants.FLOAT_TWO;
        centerView.setX(centerX);
        centerView.setY(centerY);
        Log.i(TAG, "initView centerX= " + centerX + " centerY= " + centerY);
        baseView.addView(centerView);
        parentView.addView(baseView);
    }

    public void release() {
        if (baseView != null) {
            baseView.removeView(centerView);
            parentView.removeView(baseView);
            baseView = null;
            parentView = null;
        }
        if (centerView != null) {
            centerView = null;
        }
    }
}

  首先SolarControl类的构造方法传入的是ViewGroup对象,而ViewGroup是容器,RelativeLayout、LinearLayoutConstraintLayout等等都继承ViewGroup。分别实例化CenterView和BaseLayoutView对象,并计算CenterView的x、y坐标,CenterView坐标拿到的是view左上角的坐标,因此想让view居中,需要重新计算坐标。x、y分别减去CenterView宽度的一半,计算出来的坐标就是让CenterView在整个屏幕居中显示。然后将CenterView对象通过addView方法添加到BaseLayoutView对象里面,也可以理解CenterView是BaseLayoutView的子view、子视图。同样通过addView方法添加到ViewGroup容器中,也就是需要把BaseLayoutView视图添加到activity_main.xml布局里面。声明release方法提供外部调用,方法内部就是移除view,释放引用,目的就是activity退出的时候释放内存,这就是编码过程中需要考虑到优化内存的地方。

  接下来运行一下项目,看看效果。

自定义的CenterView在整个屏幕居中了,基本功能就实现了,通过例子,我们需要掌握如何自定义View,在开发过程中,根据需求难免会需要使用自定义View来实现。上面例子已经实现了,为了后面的Android动画特效项目开发做铺垫,搜索并关注公众号“Android技术迷”关注后可阅读更多文章,感谢各位关注。

  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值