D球是一个基于区块链的行业生态价值共享平台。那这个D球到底是干什么的呢???如果你感兴趣的话 可以点击D球进行详细了解。
不谈这个了,我们说说接下来,D球首页这个动画如何实现呢??效果图如下:
一脸恍然!!!我们都知道Android有4种动画:
AlphaAnimation 透明度动画效果
ScaleAnimation 缩放动画效果
TranslateAnimation 位移动画效果
RotateAnimation 旋转动画效果
那我们先分析一下这个动画效果,既不是左右摇摆,也不是上下移动,在这里我们发现他它以某一个角度重复摆动, 如下图:
4种动画要实现这种挖矿的效果,好像~ 仿佛~~~RotateAnimation 可以实现,那么如何实现呢????我就开始翻着看源码啊!!!!
/**
* Constructor to use when building a RotateAnimation from code
*
* @param fromDegrees Rotation offset to apply at the start of the
* animation.
*
* @param toDegrees Rotation offset to apply at the end of the animation.
*
* @param pivotXType Specifies how pivotXValue should be interpreted. One of
* Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or
* Animation.RELATIVE_TO_PARENT.
* @param pivotXValue The X coordinate of the point about which the object
* is being rotated, specified as an absolute number where 0 is the
* left edge. This value can either be an absolute number if
* pivotXType is ABSOLUTE, or a percentage (where 1.0 is 100%)
* otherwise.
* @param pivotYType Specifies how pivotYValue should be interpreted. One of
* Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or
* Animation.RELATIVE_TO_PARENT.
* @param pivotYValue The Y coordinate of the point about which the object
* is being rotated, specified as an absolute number where 0 is the
* top edge. This value can either be an absolute number if
* pivotYType is ABSOLUTE, or a percentage (where 1.0 is 100%)
* otherwise.
物体所在点的Y坐标
* 正在旋转,指定为绝对值,其中0是
* 顶部边缘。如果。这个值可以是一个绝对值
* pivotYType是绝对类型,或百分比(其中1.0是100%)
* 否则。
*/
public RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue,
int pivotYType, float pivotYValue) {
mFromDegrees = fromDegrees;
mToDegrees = toDegrees;
mPivotXValue = pivotXValue;
mPivotXType = pivotXType;
mPivotYValue = pivotYValue;
mPivotYType = pivotYType;
initializePivotPoint();
}
/**
int pivotXType, 动画在X轴相对于物件位置类型,与下面的pivotXValue结合,确定X轴上旋转中心。
可能值为:Animation.ABSOLUTE,Animation.RELATIVE_TO_SELF, Animation.RELATIVE_TO_PARENT
pivotXType=Animation.ABSOLUTE,则此参数为旋转中心在屏幕上X轴的值;
pivotXType=Animation.RELATIVE_TO_PARENT,则参数pivotXValue为旋转中心在父控件水平位置百分比,如0.5表示在父控件水平方向中间位置;
pivotXType=Animation.RELATIVE_TO_SELF,则参数pivotXValue为旋转中心在控件自身水平位置百分比;
X和Y的Value都设置为0.5,则该控件以自身中心旋转。
参数:
fromDegrees 旋转的起始点(旋转开始的角度)
toDegrees 旋转的结束点(旋转最终角度)
pivotX 旋转点的X值(距离左侧的偏移量)
ivotY旋转点的Y值(距离顶部的偏移量)
public class MiningAnimalView extends RelativeLayout {
private int childImgId;
private float childValueSize;
private ImageView defaulImgtView;
private TextView defaulTitletView;
private TextView defaulValuetView;
private String defaultTitleText;
private String defaultValueText;
public LinearLayout defaultView;
private PathMeasure mPathMeasure;
private Context mcontext;
private int parentImgId;
private int textColor;
private int valueColor;
public MiningAnimalView(Context paramContext, AttributeSet paramAttributeSet) {
super(paramContext, paramAttributeSet);
this.mcontext = paramContext;
TypedArray typedArray = getContext().obtainStyledAttributes(paramAttributeSet, R.styleable.myFloatView);
this.valueColor = typedArray.getColor(R.styleable.myFloatView_valueColor, Color.BLUE);
this.textColor = typedArray.getColor(R.styleable.myFloatView_valueColor, Color.WHITE);
this.childValueSize = typedArray.getDimension(R.styleable.myFloatView_childValueSize, 12.0F);
this.childImgId = typedArray.getResourceId(R.styleable.myFloatView_childValueSize, 80);
this.parentImgId = typedArray.getResourceId(R.styleable.myFloatView_parentImgId, 80);
this.defaultTitleText = typedArray.getString(R.styleable.myFloatView_defaultTitleText);
this.defaultValueText = typedArray.getString(R.styleable.myFloatView_defaultValueText);
typedArray.recycle();
}
private void init() {
setDefaultView();
}
private void initAnim(View paramView) {
paramView.animate().alpha(1.0F).scaleX(1.0F).scaleY(1.0F).setDuration(1000L).start();
}
private void setDefaultView() {
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(-1, -1);
this.defaultView = (LinearLayout) LayoutInflater.from(this.mcontext).inflate(R.layout.mini, this, false);
this.defaulImgtView = (ImageView) this.defaultView.findViewById(R.id.img_drag);
this.defaulTitletView = (TextView) this.defaultView.findViewById(R.id.tv_producting);
this.defaulTitletView.setTextColor(this.textColor);
this.defaulTitletView.setText("生产中...");
this.defaulImgtView.setImageResource(R.mipmap.icon_drag);
defaultView.setGravity(Gravity.CENTER);
addView(this.defaultView, layoutParams);
initAnim(this.defaultView);
RotateAnimation rotateAnimation = new RotateAnimation(-40.0F, 20.0F, Animation.RELATIVE_TO_SELF, 0.5F, 1, 0.5F);
rotateAnimation.setInterpolator(new LinearInterpolator());
rotateAnimation.setDuration(1000L);
rotateAnimation.setRepeatCount(-1);
rotateAnimation.setFillAfter(true);
rotateAnimation.setStartOffset(10L);
rotateAnimation.setRepeatMode(2);
this.defaulImgtView.setAnimation(rotateAnimation);
}
public void setList() {
post(new Runnable() {
public void run() {
MiningAnimalView.this.init();
}
});
}
}
public class MiningActivity extends BaseActivity {
private MiningAnimalView mFloatView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mainmmm);
initView();
}
private void initView() {
mFloatView = (MiningAnimalView) findViewById(R.id.float_view);
mFloatView.setList();
}
}
解释:
rotateAnimation.setDuration(1000L) | 设置动画持续时间 |
rotateAnimation.setRepeatCount(-1) | 设置重复次数 |
rotateAnimation.setFillAfter(true) | 动画执行完后是否停留在执行完的状态 |
rotateAnimation.setStartOffset(10L) | 执行前的等待时间 |
原文链接 https://blog.csdn.net/u014133119/article/details/101207120