1、TranslateAnimation

平移动画,大概是我们最容易想到的实现方式,但并非能满足所有需求。这种方式不能控制进度,设置好动画持续时间后,就会一直到结束。

  int screenWidth = ScreenUtils.getScreenWidth();//获取屏幕宽度
  Animation translateAnimation = new TranslateAnimation(0, screenWidth - 50, 0, 0);//设置平移的起点和终点
  translateAnimation.setDuration(10000);//动画持续的时间为10s
  translateAnimation.setFillEnabled(true);//使其可以填充效果从而不回到原地
  translateAnimation.setFillAfter(true);//不回到起始位置
  //如果不添加setFillEnabled和setFillAfter则动画执行结束后会自动回到远点
  translateAnimation.setAnimationListener(this);
  mEvaluateProgress.setAnimation(translateAnimation);//给imageView添加的动画效果
  translateAnimation.startNow();//动画开始执行 放在最后即可

2、通过drawBitmap

通过drawBitmap在不同的位置画出图片,适合图片作为平移动画的需求。经测试,使用Matrix方式对部分待透明度以及过大的图片无法绘制,通过计算位置直接绘制正常。

  Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.icon_risk_scan_line);
  int width = ScreenUtils.getScreenWidth() - bitmap.getWidth();
  //int height = bitmap.getHeight();
  //绘制原图
  //canvas.drawBitmap(bitmap, 0, 0, paint);
  canvas.drawBitmap(bitmap, progress * width / 100, 0, null);
  //平移图片
  Matrix matrix = new Matrix();
  matrix.postTranslate(progress * width / 100, height);
  canvas.drawBitmap(bitmap, matrix, null);

3、改变View长度

改变长度和改变位置是一个道理。获取View的位置,然后通过进度计算出View的宽度,再通过setLayoutParams改变View大小。这个方式满足我们的需求,采用的此方式。

  private void setViewLocation(int progress) {
      BitmapDrawable drawable = (BitmapDrawable) mEvaluateImage.getDrawable();
      int width = ScreenUtils.getScreenWidth();
      RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) mLayoutScan.getLayoutParams();
      layoutParams.width = width * progress / 100 + drawable.getBitmap().getWidth();
      layoutParams.height = LinearLayout.LayoutParams.MATCH_PARENT;
      mLayoutScan.setLayoutParams(layoutParams);
  }

以上。如有错误,欢迎指正!