中心打开效果 - 新闻资讯 - 云南小程序开发|云南软件开发|云南网站建设-昆明葵宇信息科技有限公司

159-8711-8523

云南网建设/小程序开发/软件开发

知识

不管是网站,软件还是小程序,都要直接或间接能为您产生价值,我们在追求其视觉表现的同时,更侧重于功能的便捷,营销的便利,运营的高效,让网站成为营销工具,让软件能切实提升企业内部管理水平和效率。优秀的程序为后期升级提供便捷的支持!

您当前位置>首页 » 新闻资讯 » 技术分享 >

中心打开效果

发表时间:2020-10-19

发布人:葵宇科技

浏览次数:18


效不雅图:
如今给加大年夜分享一下打开Activity时中间打开的效不雅。
思路:
1.在设置打开目标Aitivity的时,先获取当前Acitivty,设置瓜分的背景的坐标。
2.当目标Activity打开之前,根据瓜分的坐标,创建ImageView,高低两部分,并且添加到目标Activity中。
3.最后设置高低两部分IMageView的动画,是他们一路开启动画。
4.当动画停止后,要把目标Activity中的添加的两个ImageView移除掉落。


下面经由过程代率攀来实现:
设置开启目标Activity


public static void startActivity(Activity currActivity, Intent intent,
			int splitYCoord) {

		// 预备一个BItMap图片,是当前的Activity界面作为的背景
		prepare(currActivity, splitYCoord);

		currActivity.startActivity(intent);
		currActivity.overridePendingTransition(0, 0);
	}
currActivity:当前的Activity
intent:开启新的Activity的Intent
splitYCoord:想去瓜分Activity的纵坐标,-1表示平等的大年夜中心瓜分
在这个办法里,最重要的功能获取当前Acitivity界面作为一个Bitmap
private static void prepare(Activity currActivity, int splitYCoord) {

		// 获取当前的Activity,作为一个BItMap
		View root = currActivity.getWindow().getDecorView()
				.findViewById(android.R.id.content);
		root.setDrawingCacheEnabled(true);
		mBitmap = root.getDrawingCache();

		// If the split Y coordinate is -1 - We'll split the activity equally
		splitYCoord = (splitYCoord != -1 ? splitYCoord
				: mBitmap.getHeight() / 2);

		if (splitYCoord > mBitmap.getHeight())
			throw new IllegalArgumentException("Split Y coordinate ["
					+ splitYCoord + "] exceeds the activity's height ["
					+ mBitmap.getHeight() + "]");

		// 设置给两个高低BitMap的坐标,是给目标Activity制造的动画的ImageView供给的坐标
		mLoc1 = new int[] { 0, splitYCoord, root.getTop() };
		mLoc2 = new int[] { splitYCoord, mBitmap.getHeight(), root.getTop() };
	}

主如果获取当前的Activity的内容,生成一个BitMap,然后根据瓜分标记,把瓜分的高低两部分ImageView的坐标划搀扶来

在目标的Activity中oncreate()的Activity的onCreate()中的setContentView()前面调用预备动画
public static void prepareAnimation(final Activity destActivity) {
		mTopImage = createImageView(destActivity, mBitmap, mLoc1);
		mBottomImage = createImageView(destActivity, mBitmap, mLoc2);
	}
创建瓜分的ImageView,并且添加到目标Activity的窗体上

private static ImageView createImageView(Activity destActivity, Bitmap bmp,
			int loc[]) {
		MyImageView imageView = new MyImageView(destActivity);
		imageView.setImageBitmap(bmp);
		imageView.setImageOffsets(bmp.getWidth(), loc[0], loc[1]);

		WindowManager.LayoutParams windowParams = new WindowManager.LayoutParams();
		windowParams.gravity = Gravity.TOP;
		windowParams.x = 0;
		windowParams.y = loc[2] + loc[0];
		windowParams.height = loc[1] - loc[0];
		windowParams.width = bmp.getWidth();
		windowParams.flags = WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
		windowParams.format = PixelFormat.TRANSLUCENT;
		windowParams.windowAnimations = 0;
		destActivity.getWindowManager().addView(imageView, windowParams);

		return imageView;
	}

public static void animate(final Activity destActivity, final int duration,
			final TimeInterpolator interpolator) {

		// Post this on the UI thread's message queue. It's needed for the items
		// to be already measured
		new Handler().post(new Runnable() {

			@Override
			public void run() {
				mSetAnim = new AnimatorSet();
				mTopImage.setLayerType(View.LAYER_TYPE_HARDWARE, null);
				mBottomImage.setLayerType(View.LAYER_TYPE_HARDWARE, null);
				mSetAnim.addListener(new Animator.AnimatorListener() {
					@Override
					public void onAnimationStart(Animator animation) {
					}

					@Override
					public void onAnimationEnd(Animator animation) {
						clean(destActivity);
					}

					@Override
					public void onAnimationCancel(Animator animation) {
						clean(destActivity);
					}

					@Override
					public void onAnimationRepeat(Animator animation) {

					}
				});

				// Animating the 2 parts away from each other
				Animator anim1 = ObjectAnimator.ofFloat(mTopImage,
						"translationY", mTopImage.getHeight() * -1);
				Animator anim2 = ObjectAnimator.ofFloat(mBottomImage,
						"translationY", mBottomImage.getHeight());

				if (interpolator != null) {
					anim1.setInterpolator(interpolator);
					anim2.setInterpolator(interpolator);
				}

				mSetAnim.setDuration(duration);
				mSetAnim.playTogether(anim1, anim2);
				mSetAnim.start();
			}
		});
	}

当动画停止,移除目标Activity的ImageView。

private static void clean(Activity activity) {
		if (mTopImage != null) {
			mTopImage.setLayerType(View.LAYER_TYPE_NONE, null);
			try {
				// If we use the regular removeView() we'll get a small UI
				// glitch
				activity.getWindowManager().removeViewImmediate(mBottomImage);
			} catch (Exception ignored) {
			}
		}
		if (mBottomImage != null) {
			mBottomImage.setLayerType(View.LAYER_TYPE_NONE, null);
			try {
				activity.getWindowManager().removeViewImmediate(mTopImage);
			} catch (Exception ignored) {
			}
		}

		mBitmap = null;
	}






履行次序:startActivity-->prepareAnimation----->animate
如不雅想给其他View设置中间打开效不雅,那么就按照这三步履行即可


应用步调:
我这是创建了一个BaseActivity,把预备动画和开启动画放在了BaseAcitivity中
activity01.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.yzk.centersplit.activity.Activity2" >

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="@drawable/num1"
        android:gravity="center"
        android:text="我是美男一号"
        android:textColor="@android:color/holo_blue_light"
        android:textSize="30sp" />

</RelativeLayout>
activity02.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.yzk.centersplit.activity.Activity2" >

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:background="@drawable/num2"
        android:gravity="center"
        android:text="我是不是很萌"
        android:textColor="@android:color/holo_green_dark"
        android:textSize="30sp" />

</RelativeLayout>
activity03.xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.yzk.centersplit.activity.Activity2" >

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:background="@drawable/bg3"
        android:gravity="center"
        android:text="风景是不是很美"
        android:textColor="@android:color/holo_orange_dark"
        android:textSize="30sp" />

</RelativeLayout>
activity04.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.yzk.centersplit.activity.Activity2" >

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:background="@drawable/bg4"
        android:gravity="center"
        android:text="想不想拥有一个豪车"
        android:textColor="@android:color/holo_red_dark"
        android:textSize="30sp" />

</RelativeLayout>

BaseActivity.java

public class BaseActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// 预备好动画,创建了上一个Activity的以中心为线
		ActivitySplitAnimationUtil.prepareAnimation(this);
		super.onCreate(savedInstanceState);
		// 开启动画
		ActivitySplitAnimationUtil.animate(this, 2000);
	}
}

Activity1.java
public class Activity1 extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity01);
		findViewById(R.id.button).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 此时就会把当前的activity作为中心瓜分动画的背景了
				ActivitySplitAnimationUtil.startActivity(Activity1.this,
						new Intent(Activity1.this, Activity2.class));
			}
		});
	}
}
Activity2.java


public class Activity2 extends BaseActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity02);
		findViewById(R.id.button).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				ActivitySplitAnimationUtil.startActivity(Activity2.this,
						new Intent(Activity2.this, Activity3.class));
			}
		});
	}

}
Activity3.java
public class Activity3 extends BaseActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity03);
		findViewById(R.id.button).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				ActivitySplitAnimationUtil.startActivity(Activity3.this,
						new Intent(Activity3.this, Activity4.class));
			}
		});
	}

}
Activity4.java
public class Activity4 extends BaseActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity04);
	}

}



源码下载:http://download.csdn.net/detail/forwardyzk/8332101
如不雅效不雅图看不到,请应用360安然浏览器查看。不知道为什么在谷歌浏览器上gif图片显示不出来。
知道如何解决的请给你建议。
[img]http://img.blog.csdn.net/20150106144714639
private static class MyImageView extends ImageView {
		private Rect mSrcRect;
		private Rect mDstRect;
		private Paint mPaint;

		public MyImageView(Context context) {
			super(context);
			mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		}

		/**
		 * Setting the bitmap offests to control the visible area
		 * 
		 * @param width
		 *            The bitmap image
		 * @param bmp
		 *            The start Y position
		 * @param loc
		 *            The end Y position
		 * @return
		 */
		public void setImageOffsets(int width, int startY, int endY) {
			mSrcRect = new Rect(0, startY, width, endY);
			mDstRect = new Rect(0, 0, width, endY - startY);
		}

		@Override
		protected void onDraw(Canvas canvas) {
			Bitmap bm = null;
			Drawable drawable = getDrawable();
			if (null != drawable && drawable instanceof BitmapDrawable) {
				bm = ((BitmapDrawable) drawable).getBitmap();
			}

			if (null == bm) {
				super.onDraw(canvas);
			} else {
				canvas.drawBitmap(bm, mSrcRect, mDstRect, mPaint);
			}
		}
	}
开启动画

相关案例查看更多