TV端:通过遥控器的点击实现图片的上下左右抖动的效果
做TV端有一段时间了,我看到别的TV上有一个通过遥控器的触摸板来控制一张图片的相应方向的抖动,感觉听新奇的,就试着做了一个分享一下:
转载注明出处:http://www.cnblogs.com/hyylog/p/5666806.html
其实很简单,就是通过旋转动画来控制:
直接上代码:
public class ImageSew extends ImageView {
private int state = 0;// 0正常 1上倾斜2下倾斜3做倾斜4右倾斜
private int degress ;
private ValueAnimator startAnimator;
private ValueAnimator endAnimator;
public ImageSew(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
public void initeAnimation(int member, int duration) {
degress = member;
startAnimator = ValueAnimator.ofInt(0,member);
endAnimator = ValueAnimator.ofInt(0,member);
startAnimator.setDuration(duration);
endAnimator.setDuration(duration);
startAnimator.addListener(new AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
endAnimator.start();
endAnimator.addListener(new AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
setRotationX(0);
setRotationX(0);
}
@Override
public void onAnimationCancel(Animator animation) {
}
});
}
@Override
public void onAnimationCancel(Animator animation) {
}
});
startAnimator.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int value = (Integer) animation.getAnimatedValue();
if(state==1)
setRotationX(value);
else if(state==2)
setRotationX(0-value);
else if(state == 3)
setRotationY(0-value);
else if(state == 4)
setRotationY(value);
}
});
endAnimator.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int value = (Integer) animation.getAnimatedValue();
if(state==1)
setRotationX(degress-value);
else if(state==2)
setRotationX(value - degress);
else if(state == 3)
setRotationY(value - degress);
else if(state == 4)
setRotationY(degress-value);
}
});
}
@Override
protected void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
}
public void upBitmap() {
state = 1;
start();
}
public void downBitmap() {
state = 2;
start();
}
public void leftBitmap() {
state = 3;
start();
}
public void rightBitmap() {
state = 4;
start();
}
public void start(){
cancelAnimation();
startAnimator.start();
}
public void cancelAnimation(){
startAnimator.cancel();
endAnimator.cancel();
setRotationX(0);
setRotationY(0);
}
}