Android-模拟电视屏幕开关机特效
效果图:
布局代码:
activity_main.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"
- android:background="#000000"
- tools:context=".MainActivity">
- <LinearLayout
- android:id="@+id/LL"
- android:layout_width="wrap_content"
- android:orientation="horizontal"
- android:layout_centerHorizontal="true"
- android:layout_height="wrap_content">
- <Button
- android:background="#ffffff"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:onClick="off"
- android:text="关" />
- <Button
- android:background="#ffffff"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:onClick="on"
- android:text="开" />
- </LinearLayout>
- <LinearLayout
- android:layout_below="@+id/LL"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <ImageView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:id="@+id/imageView"
- android:scaleType="centerCrop"
- android:src="@drawable/aa"/>
- </LinearLayout>
- </RelativeLayout>
MyAnimation.java
- package com.example.yu_longji.android21;
- import android.graphics.Matrix;
- import android.view.animation.AccelerateDecelerateInterpolator;
- import android.view.animation.AccelerateInterpolator;
- import android.view.animation.Animation;
- import android.view.animation.Transformation;
- /**
- * Created by yu_longji on 2015/8/30.
- */
- public class MyAnimation {
- OffAnimation offanim;
- OnAnimation onanim;
- MyAnimation() {
- offanim = new OffAnimation();
- onanim = new OnAnimation();
- }
- public class OffAnimation extends Animation {
- int halfWidth;
- int halfHeight;
- @Override
- public void initialize(int width, int height, int parentWidth, int parentHeight) {
- super.initialize(width, height, parentWidth, parentHeight);
- //设置动画时间为800毫秒
- setDuration(800);
- //设置动画结束后就结束在动画结束的时刻
- setFillAfter(true);
- //保存View的中心点
- halfWidth = width / 2;
- halfHeight = height / 2;
- //设置动画先加速后减速
- setInterpolator(new AccelerateDecelerateInterpolator());
- }
- @Override
- protected void applyTransformation(float interpolatedTime, Transformation t) {
- super.applyTransformation(interpolatedTime, t);
- final Matrix matrix = t.getMatrix();
- //interpolatedTime是从0~1的一个变化,所以我们前80%让动画缩小成一个线,后20%保持线的高度缩小线的宽度
- if (interpolatedTime < 0.8) {
- matrix.preScale(1 + 0.625f * interpolatedTime, 1 - interpolatedTime / 0.8f + 0.01f, halfWidth, halfHeight);
- } else {
- matrix.setScale(7.5f * (1 - interpolatedTime), 0.01f, halfWidth, halfHeight);
- }
- }
- }
- public class OnAnimation extends Animation {
- int halfWidth;
- int halfHeight;
- @Override
- public void initialize(int width, int height, int parentWidth, int parentHeight) {
- super.initialize(width, height, parentWidth, parentHeight);
- //设置动画时间为900毫秒
- setDuration(900);
- //设置动画结束后就结束在动画结束的时刻
- setFillAfter(true);
- //保存View的中心点
- halfWidth = width / 2;
- halfHeight = height / 2;
- setInterpolator(new AccelerateInterpolator());
- }
- @Override
- protected void applyTransformation(float interpolatedTime, Transformation t) {
- super.applyTransformation(interpolatedTime, t);
- final Matrix matrix = t.getMatrix();
- if (interpolatedTime < 0.2) {
- matrix.setScale(0.01f, interpolatedTime * 5.0f, halfWidth, halfHeight);
- } else {
- matrix.setScale((float) Math.pow(interpolatedTime, 4), 1, halfWidth, halfHeight * 2);
- }
- }
- }
- //上下收缩
- // matrix.preScale(1,1-interpolatedTime,halfWidth*2,halfHeight);
- //中间一条线拉伸
- // matrix.setScale(interpolatedTime,0.01f,halfWidth,halfHeight);
- //上下伸展开
- // matrix.preScale(1,-(interpolatedTime),halfWidth*2,halfHeight);
- //左右向中间收缩
- // matrix.preScale(1-interpolatedTime,1,halfWidth,halfHeight*2);
- //上下伸展开
- // matrix.setScale(1, interpolatedTime, width, halfHeight);
- }
- package com.example.yu_longji.android21;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.ImageView;
- public class MainActivity extends Activity {
- ImageView imageView;
- MyAnimation myAnimation;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- imageView = (ImageView) findViewById(R.id.imageView);
- myAnimation = new MyAnimation();
- }
- //关
- public void off(View view) {
- imageView.startAnimation(myAnimation.offanim);
- }
- //开
- public void on(View view) {
- imageView.startAnimation(myAnimation.onanim);
- }
- }