Android——动画效果1.帧动画2.补间动画3.跳转画面
插值器类 | xml属性值 | 说明 |
LinearInterpolator | @android:anim/linear_interpolatorr | 动画以均匀的速度改变。 |
AccelerateInterpolator | @android:anim/accelerate_interpolator | 在动画开始时改变速度较慢,然后开始加速。 |
AccelerateDecelerateInterpolator | @android:anim/accelerate_decelerate_interpolator | 在动画开始、结束的时改变速度较慢,中间时加速。 |
CycleInterpolator | @android:anim/cycle_interpolator | 动画循环播放特定次数,变化速度按正弦曲线改变。 |
DecelerateInterpolator | @android:anim/decelerate_interpolator | 在动画开始的是改变速度较快,然后开始减速。 |
AnticipateInterpolator | @android:anim/accelerate_interpolator | 先向相反方向改变一段再加速播放。 |
AnticipateOvershootInterpolator | @android:anim/anticipate_overshoot_interpolator | 开始的时向后然后向前甩一定值后返回到达最后的值。 |
BounceInterpolator | @android:anim/bounce_interpolator | 跳跃,快到目的值时值会跳跃。 |
OvershottInterpolator | @android:anim/overshoot_interpolator | 回弹,超出目的值然后缓慢改变到目的值。 |
补间动画amin——amin1 帧动画drawable——amin2
if(id == R.id.action_settings)
{
//补间动画 实例
Animation animation= AnimationUtils.loadAnimation(getBaseContext(),R.anim.anim1);
an1.startAnimation(animation);
}else if(id == R.id.action_settings1)
{
//帧动画
an2.setBackgroundResource(R.drawable.anim2);
animation=(AnimationDrawable)an2.getBackground();
animation.start();
}else if(id == R.id.action_settings2)
{
//属性动画
ObjectAnimator oja=ObjectAnimator.ofFloat(an3,"rotation",0,360);
oja.setDuration(3000);
oja.setRepeatCount(1);
//设置插补器
oja.setInterpolator(newAccelerateDecelerateInterpolator());
oja.start();
}else if(id == R.id.action_settings3)
{
ObjectAnimator oja=ObjectAnimator.ofFloat(an4,"alpha",1,0);
oja.setDuration(4000);
oja.setRepeatCount(1);
oja.setRepeatMode(ObjectAnimator.REVERSE);
oja.setInterpolator(newLinearInterpolator());
oja.start();
}
渐进出现
点击 交回 正常
点击图片动画效果
1.动画效果
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context="com.example.jreduch728.Tool_barActivity"> <item android:id="@+id/home" android:title="主页" android:icon="@mipmap/ic_launcher" app:showAsAction="ifRoom|withText"> </item> <item android:id="@+id/home1" android:title="查找" android:icon="@mipmap/ic_launcher" app:showAsAction="ifRoom|withText"> </item> <group> <item android:id="@+id/action_settings" android:orderInCategory="100" android:title="动画效果0" android:icon="@mipmap/ic_launcher" app:showAsAction="never" /> <item android:id="@+id/action_settings1" android:orderInCategory="100" android:title="动画效果1" android:icon="@mipmap/qqq" app:showAsAction="never"> <menu> <item android:title="动画效果。。。"></item> </menu> </item> <item android:id="@+id/action_settings2" android:orderInCategory="100" android:title="动画效果2" android:icon="@mipmap/ic_launcher" app:showAsAction="never" /> <item android:id="@+id/action_settings3" android:orderInCategory="100" android:title="动画效果3" android:icon="@mipmap/ic_launcher" app:showAsAction="never" /> </group> </menu>
package com.example.jreduch728; import android.animation.ObjectAnimator; import android.content.Intent; import android.graphics.drawable.AnimationDrawable; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.support.v7.view.menu.MenuBuilder; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.view.animation.BounceInterpolator; import android.view.animation.LinearInterpolator; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; import java.lang.reflect.Method; public class Tool_barActivity extends AppCompatActivity { private TextView an1; private ImageView an2; private AnimationDrawable animation; private TextView an3; private TextView an4; private Button bt2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_tool_bar); an1=(TextView)findViewById(R.id.an1) ; an2=(ImageView)findViewById(R.id.an2) ; an3=(TextView)findViewById(R.id.an3) ; an4=(TextView)findViewById(R.id.an4) ; bt2=(Button)findViewById(R.id.button2); bt2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(Tool_barActivity.this,MainActivity.class); startActivity(intent); //系统自带动画样式左边进右边出 //overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right); //overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out); overridePendingTransition(R.anim.pop_enter,R.anim.pop_exit); } }); an2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (animation!=null){ if (animation.isRunning()){ animation.stop(); }} } }); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); // toolbar.setNavigationIcon(R.mipmap.ic_launcher); // toolbar.setLogo(R.mipmap.qqq); // toolbar.setTitle("TOOBAR"); // toolbar.setSubtitle("使用"); setSupportActionBar(toolbar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { //创建菜单 // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_tool_bar, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { //补间动画 实例 Animation animation= AnimationUtils.loadAnimation(getBaseContext(),R.anim.anim1); an1.startAnimation(animation); }else if (id == R.id.action_settings1) { //帧动画 an2.setBackgroundResource(R.drawable.anim2); animation=(AnimationDrawable)an2.getBackground(); animation.start(); }else if (id == R.id.action_settings2) { //属性动画 ObjectAnimator oja=ObjectAnimator.ofFloat(an3,"rotation",0,360); oja.setDuration(3000); oja.setRepeatCount(1); //设置插补器 oja.setInterpolator(new BounceInterpolator()); oja.start(); }else if (id == R.id.action_settings3) { ObjectAnimator oja=ObjectAnimator.ofFloat(an4,"alpha",1,0); oja.setDuration(4000); oja.setRepeatCount(1); oja.setRepeatMode(ObjectAnimator.REVERSE); oja.setInterpolator(new LinearInterpolator()); oja.start(); } return super.onOptionsItemSelected(item); } @Override protected boolean onPrepareOptionsPanel(View view, Menu menu) { if (menu != null) { if (menu.getClass() == MenuBuilder.class) { try { Method m = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE); m.setAccessible(true); m.invoke(menu, true); } catch (Exception e) { System.out.print(getClass().getSimpleName() + "onMenuOpened...unable to set icons for overflow menu" + e); } } } return super.onPrepareOptionsPanel(view, menu); }; }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.example.jreduch728.Tool_barActivity" tools:showIn="@layout/activity_tool_bar"> <TextView android:layout_width="150dp" android:layout_height="50dp" android:id="@+id/an1" android:text="动画效果1" android:textSize="30sp" android:textColor="#111111" android:gravity="center" android:background="#131aa3" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="5dp" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/an1" android:background="@mipmap/qqq" android:id="@+id/an2" /> <TextView android:layout_width="150dp" android:layout_height="50dp" android:id="@+id/an3" android:text="动画效果2" android:textSize="30sp" android:textColor="#111111" android:gravity="center" android:background="#131aa3" android:layout_below="@+id/an1" android:layout_alignStart="@+id/an1" /> <TextView android:layout_width="150dp" android:layout_height="50dp" android:id="@+id/an4" android:text="动画效果3" android:textSize="30sp" android:textColor="#111111" android:gravity="center" android:background="#131aa3" android:layout_below="@+id/an3" android:layout_centerHorizontal="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="文字颜色" android:id="@+id/button" android:textSize="20dp" android:textColor="@color/bt_text_color" android:layout_below="@+id/an4" android:layout_alignStart="@+id/an4" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/button" android:background="@drawable/img_bg" android:clickable="true" android:id="@+id/an5" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:clickable="true" android:layout_alignTop="@+id/an5" android:layout_alignStart="@+id/button"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/img_bg_setting" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="个人设置" android:gravity="center" android:textSize="30dp" android:textColor="@color/text_color" /> </LinearLayout> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="跳转画面" android:textSize="20dp" android:id="@+id/button2" android:layout_below="@+id/an5" android:layout_alignParentStart="true" /> </RelativeLayout>
2帧动画+布局
<?xml version="1.0" encoding="utf-8"?> <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="#a6a0a0" tools:context="com.example.jreduch728.Text1Activity"> <TextView android:layout_width="match_parent" android:layout_height="100dp" android:gravity="center" android:background="#9f9696" android:layout_marginTop="5dp" android:text="请登录" android:textSize="40dp" android:clickable="true" android:textColor="@color/bt_text_color" android:id="@+id/tx1" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:layout_below="@+id/tx1" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:clickable="true" android:id="@+id/ll1" android:layout_marginLeft="10dp" android:layout_weight="1" android:layout_marginTop="20dp" > <ImageView android:layout_width="40dp" android:layout_height="40dp" android:background="@drawable/lx1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="信息平台" android:gravity="center" android:layout_marginLeft="30dp" android:textSize="30dp" android:textColor="@color/bt_text_color" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#5b5858" ></View> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:clickable="true" android:id="@+id/l2" android:layout_marginLeft="10dp" android:layout_weight="1" > <ImageView android:layout_width="40dp" android:layout_height="40dp" android:background="@drawable/lx2" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="校园活动" android:layout_marginLeft="30dp" android:gravity="center" android:textSize="30dp" android:textColor="@color/bt_text_color" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#5b5858" ></View> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:clickable="true" android:layout_marginLeft="10dp" android:id="@+id/l3" android:layout_weight="1" > <ImageView android:layout_width="40dp" android:layout_height="40dp" android:background="@drawable/lx3" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="思想引领" android:gravity="center" android:layout_marginLeft="30dp" android:textSize="30dp" android:textColor="@color/bt_text_color" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#5b5858" ></View> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:clickable="true" android:id="@+id/l4" android:layout_marginLeft="10dp" android:layout_weight="1" > <ImageView android:layout_width="40dp" android:layout_height="40dp" android:background="@drawable/lx4" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="就业招聘" android:gravity="center" android:layout_marginLeft="30dp" android:textSize="30dp" android:textColor="@color/bt_text_color" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#5b5858" ></View> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:orientation="horizontal" android:clickable="true" android:id="@+id/l5" android:layout_weight="1" > <ImageView android:layout_width="40dp" android:layout_height="40dp" android:background="@drawable/lx5" /> <TextView android:layout_width="wrap_content" android:layout_marginLeft="30dp" android:layout_height="wrap_content" android:text="考研出国" android:gravity="center" android:textSize="30dp" android:textColor="@color/bt_text_color" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#5b5858" ></View> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:clickable="true" android:layout_marginLeft="10dp" android:id="@+id/l6" android:layout_weight="1" android:layout_marginBottom="80dp" > <ImageView android:layout_width="40dp" android:layout_height="40dp" android:background="@drawable/lx6" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="便利工具" android:layout_marginLeft="30dp" android:gravity="center" android:textSize="30dp" android:textColor="@color/bt_text_color" /> </LinearLayout> </LinearLayout> </RelativeLayout>
作者:冲天之峰 20160728