这次是需求有点无语,不过还是做出来了,效果如下,这回要讲讲,有点复杂,以后自己估计都忘了,记录一下--!
动画类AnimalUtul:
public class AnimalUtul { private AnimatorSet mAnimatorSet; RelativeLayout rl_parent; private ImageView iv_logo; private float mLogoY; private Context context; private long starttime = 1500; public long getStarttime() { return starttime; } public void setStarttime(long starttime) { this.starttime = starttime; } public AnimalUtul(Context context, RelativeLayout layout, ImageView logo) { iv_logo = logo; rl_parent = layout; this.context = context; iv_logo.postDelayed(new Runnable() { @Override public void run() { if (mLogoY == 0) { mLogoY = ViewHelper.getY(iv_logo); } playLogoInAnim(); } }, starttime); iv_logo.postDelayed(new Runnable() { @Override public void run() { playInAnim(); } }, starttime); } public void playInAnim() { rl_parent.setVisibility(View.VISIBLE); AnimatorSet mAnimatorSet; ObjectAnimator anim3 = ObjectAnimator.ofFloat(rl_parent, "y", DisplayUtil.getDisplayheightPixels(context), DisplayUtil.dip2px(context, 160)); mAnimatorSet = new AnimatorSet(); mAnimatorSet.play(anim3); mAnimatorSet.setDuration(1000); mAnimatorSet.start(); } private void playLogoInAnim() { ObjectAnimator anim1 = ObjectAnimator.ofFloat(iv_logo, "scaleX", 1.0f, 0.5f); ObjectAnimator anim2 = ObjectAnimator.ofFloat(iv_logo, "scaleY", 1.0f, 0.5f); ObjectAnimator anim3 = ObjectAnimator.ofFloat(iv_logo, "y", mLogoY, DisplayUtil.dip2px(context, 15)); if (mAnimatorSet != null && mAnimatorSet.isRunning()) { mAnimatorSet.cancel(); mAnimatorSet = null; } mAnimatorSet = new AnimatorSet(); mAnimatorSet.play(anim1).with(anim2); mAnimatorSet.play(anim2).with(anim3); mAnimatorSet.setDuration(1000); mAnimatorSet.start(); } }
适配屏幕高度类DisplayUtil:
public class DisplayUtil { /** 屏幕宽度 */ private static int DisplayWidthPixels = 0; /** 屏幕高度 */ private static int DisplayheightPixels = 0; /** * 获取屏幕参数 * @param context */ private static void getDisplayMetrics(Context context) { DisplayMetrics dm = new DisplayMetrics(); ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(dm); // 宽度 DisplayWidthPixels = dm.widthPixels; // 高度 DisplayheightPixels = dm.heightPixels; } /** * 获取屏幕宽度 * @param context * @return */ public static int getDisplayWidthPixels(Context context) { if (context == null) { return -1; } if (DisplayWidthPixels == 0) { getDisplayMetrics(context); } return DisplayWidthPixels; } /** * 获取屏幕高度 * @param context * @return */ public static int getDisplayheightPixels(Context context) { if (context == null) { return -1; } if (DisplayheightPixels == 0) { getDisplayMetrics(context); } return DisplayheightPixels; } /** * 将px值转换为dip或dp值 * * @param pxValue * @return */ public static int px2dip(Context context, float pxValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (pxValue / scale + 0.5f); } /** * 将dip或dp值转换为px值 * * @param dipValue * @return */ public static int dip2px(Context context, float dipValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dipValue * scale + 0.5f); } /** * 将px值转换为sp值 * * @param pxValue * @return */ public static int px2sp(Context context, float pxValue) { final float fontScale = context.getResources().getDisplayMetrics().scaledDensity; return (int) (pxValue / fontScale + 0.5f); } /** * 将sp值转换为px值 * @param spValue * @return */ public static int sp2px(Context context, float spValue) { final float fontScale = context.getResources().getDisplayMetrics().scaledDensity; return (int) (spValue * fontScale + 0.5f); } }
调用方法:new AnimalUtul(this,出现的layout,logo图片);
唉~好累~天天写这些--!
时间: 2024-10-13 08:44:58