设计思路:

利用接口View::onConfigurationChanged,来捕获横竖屏幕切换事件,然后对控件使用动画。

实施步骤:

1),对Activity设置属性:android:configChanges="orientation"。

2),控件代码:

package test.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;
}
}
}

 

 

更多相关文章

  1. android用户界面之按钮(Button)教程实例汇
  2. 【EditText】Android(安卓)中设置 EditText 光标颜色
  3. android五种布局模式
  4. Android(安卓)UI Material Design交互设计(一)--BottomNavigatio
  5. Android在layout xml中使用include
  6. 动画学习笔记-Android(安卓)Animation
  7. Android(安卓)Layout_weight 属性
  8. 了解使用Android(安卓)ConstraintLayout
  9. Android学习笔记:androidmanifest.xml 高级属性

随机推荐

  1. android之helloworld详解
  2. Android学习之RatingBar评分组件
  3. android中退出整个app应用程序
  4. android booting 启动过程
  5. Android全屏(包含3种隐藏顶部状态栏及标题
  6. android 2.2 apidemos 赏析笔记 6
  7. ant android
  8. Android学习笔记(6)——Android——Login
  9. Error:Cause: buildToolsVersion is not
  10. 如何在一个声卡添加pa codec设备