设计思路:
利用接口View::onConfigurationChanged,来捕获横竖屏幕切换事件,然后对控件使用动画。
实施步骤:
1),对Activity设置属性:android:configChanges="orientation"。
2),控件代码:
package xuxing.view.customimageview;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.animation.Animation;
import android.view.animation.OvershootInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.ViewParent;
import android.view.WindowManager;
public class DigitImageView extends ImageView {
protected Paint mTextPaint;
protected static int FONT_SIZE = 24;
protected int mTextBase;
protected int mPageNo = 1;
protected Context mContext;
private final float TO_DEGREES = 0.0f;
private final float PIVOT_X_VALUE = 0.5f;
private final float PIVOT_Y_VALUE = 0.5f;
private final float OVERSHOOT_TENSION = 3.0f;
private final long ANIMATION_DURATION = 1000;
// smooth rotate
private int mOrientation;
/*
* public DigitImageView(Context context) { super(context); // TODO
* Auto-generated constructor stub initPaint(); }
*/
public DigitImageView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
String str = context.getClass().getName();
Log.i("DigitImageView", " " + str);
initPaint();
}
public void setPageNo(int pageno) {
mPageNo = pageno;
}
private void initPaint() {
mTextPaint = new Paint();
mTextPaint.setAntiAlias(true);
mTextPaint.setTextAlign(Paint.Align.CENTER);
mTextPaint.setTextSize(FONT_SIZE
* getResources().getDisplayMetrics().density);
mTextPaint.setColor(Color.WHITE);
final Paint.FontMetricsInt textMetrics = new Paint.FontMetricsInt();
mTextPaint.getFontMetricsInt(textMetrics);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int restoreId = canvas.save();
// canvas.drawRect(0,0,10,10,mTextPaint);
// mTextPaint.setColor(Color.WHITE);
canvas.drawText(String.valueOf(mPageNo), getWidth() / 2,
getHeight() / 2 + 5, mTextPaint);
canvas.restoreToCount(restoreId);
}
private class SmoothRotateAnimationListener implements
Animation.AnimationListener {
public void onAnimationStart(Animation animation) {
}
public void onAnimationEnd(Animation animation) {
}
public void onAnimationRepeat(Animation animation) {
}
}
protected void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.i("DigitImageView", " onConfigurationChanged");
int preRotateWidth;
int preRotateHeight;
Display display = ((WindowManager) mContext
.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
preRotateWidth = display.getWidth();
preRotateHeight = display.getHeight();
int orientation = display.getRotation();
orientation = orientation * 90;
if (orientation != mOrientation) {
float angle = (float) (mOrientation - orientation);
if (angle > 180.0f) {
angle -= 360.0f;
} else if (angle < -180.0f) {
angle += 360.0f;
}
// smooth rotate
RotateAnimation anim = new RotateAnimation(angle, TO_DEGREES,
Animation.RELATIVE_TO_PARENT, PIVOT_X_VALUE,
Animation.RELATIVE_TO_PARENT, PIVOT_Y_VALUE);
OvershootInterpolator interp = new OvershootInterpolator(
OVERSHOOT_TENSION);
anim.setDuration(ANIMATION_DURATION);
anim.setFillEnabled(true);
anim.initialize(preRotateWidth, preRotateHeight, preRotateWidth,
preRotateHeight);
anim.setInterpolator(interp);
anim.setAnimationListener(new SmoothRotateAnimationListener());
ViewParent parent = getParent();
/*
if (parent instanceof View) {
Log.i("DigitImageView", " onConfigurationChanged parent instanceof View");
((View) parent).startAnimation(anim);
}
else
{
Log.i("DigitImageView", " onConfigurationChanged parent not instanceof View");
startAnimation(anim);
}
*/
startAnimation(anim);
mOrientation = orientation;
}
}
}