android 游戏 动画
16lz
2021-01-26
public class MyView extends View implements AnimationListener { public static MyView mv; private Paint paint; private Bitmap bmp; private int x = 50; private Animation mAlphaAnimation; private Animation mScaleAnimation; private Animation mTranslateAnimation; private Animation mRotateAnimation; public MyView(Context context) { super(context); mv = this; paint = new Paint(); paint.setColor(Color.WHITE); paint.setAntiAlias(true); bmp = BitmapFactory.decodeResource(getResources(), R.drawable.icon); this.setFocusable(true); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //黑色背景 canvas.drawColor(Color.BLACK); canvas.drawText("方向键↑ 渐变透明度动画效果", 80, this.getHeight() - 80, paint); canvas.drawText("方向键↓ 渐变尺寸伸缩动画效果", 80, this.getHeight() - 60, paint); canvas.drawText("方向键← 画面转换位置移动动画效果", 80, this.getHeight() - 40, paint); canvas.drawText("方向键→ 画面转移旋转动画效果", 80, this.getHeight() - 20, paint); //绘制位图icon canvas.drawBitmap(bmp, this.getWidth() / 2 - bmp.getWidth() / 2, this.getHeight() / 2 - bmp.getHeight() / 2, paint); x += 1; } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {// 渐变透明度动画效果 mAlphaAnimation = new AlphaAnimation(0.1f, 1.0f); mAlphaAnimation.setAnimationListener(this); mAlphaAnimation.setDuration(3000); // //设置时间持续时间为3000 毫秒=3秒 this.startAnimation(mAlphaAnimation); } else if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {// 渐变尺寸伸缩动画效果 mScaleAnimation = new ScaleAnimation(0.0f, 2.0f, 1.5f, 1.5f, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.0f); mScaleAnimation.setAnimationListener(this); mScaleAnimation.setDuration(2000); this.startAnimation(mScaleAnimation); } else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {// 画面转换位置移动动画效果 mTranslateAnimation = new TranslateAnimation(0, 100, 0, 100); mTranslateAnimation.setAnimationListener(this); mTranslateAnimation.setDuration(2000); this.startAnimation(mTranslateAnimation); } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {// 画面转移旋转动画效果 mRotateAnimation = new RotateAnimation(0.0f, 360.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); mRotateAnimation.setAnimationListener(this); mRotateAnimation.setDuration(3000); this.startAnimation(mRotateAnimation); } return super.onKeyDown(keyCode, event); } @Override public void onAnimationStart(Animation animation) { Log.e("", "AnimationStart!"); } @Override public void onAnimationEnd(Animation animation) { Log.e("", "AnimationEnd!"); } @Override public void onAnimationRepeat(Animation animation) { Log.e("", "AnimationRepeat!"); }}
更多相关文章
- toast的五种效果
- android Tween Animations(动画效果-代码实现)的使用
- Android(安卓)OpenGL ES和OpenGL一起学(二)------理解Viewport(视口
- Android中使用NavigationView实现抽屉侧滑效果中Menu菜单栏设置
- Android(安卓)左右滑动切换页面或Activity的效果实现
- VectorDrawable兼容笔记
- Android(安卓)自定义View实现动画形式加载环形图
- Android实现镜花水月的效果
- [Android(安卓)Studio]设置Button的圆角、点击效果、按钮颜色