zoukankan      html  css  js  c++  java
  • 弹跳加载中自定义简单控件实现

    package com.loaderman.customviewdemo;
    
    import android.animation.Animator;
    import android.animation.ValueAnimator;
    import android.content.Context;
    import android.util.AttributeSet;
    import android.view.animation.AccelerateDecelerateInterpolator;
    
    
    public class LoadingImageView extends android.support.v7.widget.AppCompatImageView {
        private int mTop;
        //当前动画图片索引
        private int mCurImgIndex = 0;
        //动画图片总张数
        private static int mImgCount = 3;
    
        public LoadingImageView(Context context) {
            super(context);
            init();
        }
    
        public LoadingImageView(Context context, AttributeSet attrs) {
            super(context, attrs);
            init();
        }
    
        public LoadingImageView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            init();
        }
    
        @Override
        protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
            super.onLayout(changed, left, top, right, bottom);
    
            mTop = top;
        }
    
        private void init() {
            ValueAnimator valueAnimator = ValueAnimator.ofInt(0, 100, 0);
            valueAnimator.setRepeatMode(ValueAnimator.RESTART);
            valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
            valueAnimator.setDuration(2000);//动画时长
            valueAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
    
            valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {//监听动画值得实时变化
                public void onAnimationUpdate(ValueAnimator animation) {
                    Integer dx = (Integer) animation.getAnimatedValue();
                    setTop(mTop - dx);//将控件移动到当前位置,注意都是相对父控件坐标位置
                }
            });
    
            valueAnimator.addListener(new Animator.AnimatorListener() {
                public void onAnimationStart(Animator animation) {
                    setImageDrawable(getResources().getDrawable(R.drawable.pic_1));
                }
    
                public void onAnimationRepeat(Animator animation) {
                    mCurImgIndex++;
                    switch (mCurImgIndex % mImgCount) {
                        case 0:
                            setImageDrawable(getResources().getDrawable(R.drawable.pic_1));
                            break;
                        case 1:
                            setImageDrawable(getResources().getDrawable(R.drawable.pic_2));
                            break;
                        case 2:
                            setImageDrawable(getResources().getDrawable(R.drawable.pic_3));
                            break;
                    }
                }
    
                public void onAnimationEnd(Animator animation) {
    
                }
    
                public void onAnimationCancel(Animator animation) {
    
                }
            });
    
    
            valueAnimator.start();
        }
    }
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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="wrap_content"
        android:orientation="horizontal"
        android:layout_gravity="center"
        tools:context="com.loaderman.customviewdemo.MainActivity">
    
        <com.loaderman.customviewdemo.LoadingImageView
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:src="@drawable/pic_1"/>
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="加载中……"/>
    
    
    </LinearLayout>

    效果:

  • 相关阅读:
    Expression Blend4经验分享:自适应布局浅析
    Expression Blend4经验分享:制作一个简单的图片按钮样式
    Expression Blend4经验分享:制作一个简单的文字按钮样式
    24.Unity手势操作(转自宣雨松博客)一
    1.关于Unity -Vuforia -Android 开发 ,平台的搭建(极品菜鸟完整版)
    26. Kinect + Unity 体感及增强现实开发历程一
    25.Unity3D手机中Input类touch详解-Unity触屏事件解析到底(Twisted Fate)
    23.Unity+Metaio SDK 开发笔记
    徐CC , Unity +C# 自学笔记2
    unity3d中的http通信
  • 原文地址:https://www.cnblogs.com/loaderman/p/10195514.html
Copyright © 2011-2022 走看看