当你们看到这标题时,多少觉得好牛逼哦,其实我想说的是,这只是一个思路,只是简单的三维空间旋转.为什么要写这个博客呢,最近在面试时,面试官突然问这样的效果怎么实现的,当时我说用动画,他说具体点,我说用RoateAnimation,结果你们也知道.他呵呵一笑,然后说:”你确定”,我说:”我不能确定,您能告诉我吗?”,他说用:”camera”.当时我激动的说:”我知道了”,结果……

下面是实现代码:

自定义的Animation:

public class MyAnimation extends Animation{

int width,height,duration;

Camera camera=new Camera();

public MyAnimation(int width,int height,int duration){

this.width=width;

this.height=height;

this.duration=duration;

}

@Override

public void initialize(int width, int height, int parentWidth, int parentHeight) {

super.initialize(width, height, parentWidth, parentHeight);

//动画持续时间

setDuration(duration);

//设置动画结束时的效果

setFillAfter(true);

setInterpolator(new LinearInterpolator());

}

@Override

protected void applyTransformation(float interpolatedTime, Transformation t) {

super.applyTransformation(interpolatedTime, t);

camera.save();

// camera.translate(100.0f-100.0f*interpolatedTime,150.0f

-150.0f*interpolatedTime,80.0f-80.0f*interpolatedTime);

// camera.rotateX(180 * interpolatedTime);

camera.rotateY(180 * (1+interpolatedTime));

Matrix matrix=t.getMatrix();

camera.getMatrix(matrix);

// matrix.preTranslate(-width,-height);

matrix.postTranslate(width, height);

camera.restore();

}

}

public class MainActivity extends AppCompatActivity {

ImageView imageView;

AnimationDrawable animationDrawable;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

imageView= (ImageView) findViewById(R.id.my_imageview);

imageView.setAnimation(new

MyAnimation(imageView.getWidth(),imageView.getHeight(),6000));

}

}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐