标签:
组件由原始状态向终极状态转变时,为了让过渡更自然,而自动生成的动画
TranslateAnimation ta = new TranslateAnimation(10, 100, 20, 200);
10:表示的x坐标起始位置
100:表示x坐标的结束位置
20:表示y坐标的起始位置
200:表示y坐标的结束位置
iv的真实y + 200
TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 1, Animation.RELATIVE_TO_SELF, 3, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 2);
Animation.RELATIVE_TO_SELF, 1:x坐标的初始位置
Animation.RELATIVE_TO_SELF, 0.5f:y坐标的起始位置
ScaleAnimation sa = new ScaleAnimation(0.5f, 2, 0.1f, 3, iv.getWidth() / 2, iv.getHeight() / 2);
iv.getWidth() / 2:表示缩放点的x坐标
iv的真实x + iv.getWidth() / 2
ScaleAnimation sa = new ScaleAnimation(0.5f, 2, 0.1f, 3, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
AlphaAnimation aa = new AlphaAnimation(0, 0.5f);
<LinearLayout 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=".MainActivity"android:orientation="horizontal"><FrameLayoutandroid:id="@+id/fl"android:layout_weight="1"//写成1,就不会把后面的顶出去了android:layout_width="0dp"android:layout_height="match_parent"></FrameLayout><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="match_parent"android:orientation="vertical"><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="fragment01"android:onClick="click1"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="fragment02"android:onClick="click2"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="fragment03"android:onClick="click3"/></LinearLayout></LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:background="#ff0000"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="热情的红色"android:textSize="20sp"/></LinearLayout>
public class Fragment01 extends Fragment{//返回的view对象会作为fragment01的内容显示在屏幕上@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// TODO Auto-generated method stubView v = inflater.inflate(R.layout.fragment01, null);return v;}}
public class MainActivity extends Activity {private Fragment03 fg3;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);fg3 = new Fragment03();//获取fragment管理器FragmentManager fm = getFragmentManager();//打开事务FragmentTransaction ft = fm.beginTransaction();//把内容显示至帧布局ft.replace(R.id.fl, fg3);//提交ft.commit();}public void click1(View v){//把fragment01的界面显示至帧布局中//创建fragment对象Fragment01 fg1 = new Fragment01();//获取fragment管理器FragmentManager fm = getFragmentManager();//打开事务FragmentTransaction ft = fm.beginTransaction();//把内容显示至帧布局ft.replace(R.id.fl, fg1);//提交ft.commit();}
<FrameLayoutandroid:id="@+id/right_layout"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1" ><fragmentandroid:id="@+id/right_fragment"android:name="com.example.fragmenttest.RightFragment"android:layout_width="match_parent"android:layout_height="match_parent" /></FrameLayout>
public class MainActivity extends FragmentActivity {private Fragment03 fg3;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);fg3 = new Fragment03();//获取fragment管理器FragmentManager fm = getSupportFragmentManager();//打开事务FragmentTransaction ft = fm.beginTransaction();//把内容显示至帧布局ft.replace(R.id.fl, fg3);//提交ft.commit();}
public void click4(View v){String text = et_main.getText().toString();//传递数据fg3.setText(text);}public void setText(String text){et_main.setText(text);}
//返回的view对象会作为fragment03的内容显示在屏幕上@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// TODO Auto-generated method stubView v = inflater.inflate(R.layout.fragment03, null);tv = (TextView) v.findViewById(R.id.tv);return v;}public void setText(String text){tv.setText(text);}
final EditText et = (EditText) v.findViewById(R.id.et);Button bt = (Button) v.findViewById(R.id.bt);bt.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {String text = et.getText().toString();//把数据传递给activity((MainActivity)getActivity()).setText(text);}});
RightFragment rightFragment = (RightFragment) getFragmentManager().findFragmentById(R.id.right_fragment);
MainActivity activity = (MainActivity) getActivity();
AnotherRightFragment fragment = new AnotherRightFragment();FragmentManager fragmentManager = getFragmentManager();FragmentTransaction transaction = fragmentManager.beginTransaction();transaction.replace(R.id.right_layout, fragment);transaction.addToBackStack(null);transaction.commit();

一张张图片不断的切换,形成动画效果
在drawable目录下定义xml文件,子节点为animation-list,在这里定义要显示的图片和每张图片的显示时长
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"><item android:drawable="@drawable/g1" android:duration="200" /><item android:drawable="@drawable/g2" android:duration="200" /><item android:drawable="@drawable/g3" android:duration="200" /></animation-list>
在屏幕上播放帧动画
ImageView iv = (ImageView) findViewById(R.id.iv);//把动画文件设置为imageView的背景iv.setBackgroundResource(R.drawable.animations);AnimationDrawable ad = (AnimationDrawable) iv.getBackground();//播放动画ad.start();
参数150指的是X的终点坐标,它的值是imageview的 真实X + 150
//创建为位移动画对象,设置动画的初始位置和结束位置TranslateAnimation ta = new TranslateAnimation(10, 150, 20, 140);
y坐标的终点位置,如果传入2,那么终点坐标就是 真实Y + 2 * iv高度
TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 2, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 2);
动画播放相关的设置
//设置动画持续时间ta.setDuration(2000);//动画重复播放的次数ta.setRepeatCount(1);//动画重复播放的模式ta.setRepeatMode(Animation.REVERSE);//动画播放完毕后,组件停留在动画结束的位置上ta.setFillAfter(true);//播放动画iv.startAnimation(ta);
缩放的中心点在iv左上角
ScaleAnimation sa = new ScaleAnimation(0.1f, 4, 0.1f, 4);
中心点Y坐标 = 真实Y + 0.5 * iv高度
ScaleAnimation sa = new ScaleAnimation(0.1f, 4, 0.1f, 4, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
0为完全透明,1为完全不透明
AlphaAnimation aa = new AlphaAnimation(0, 0.5f);
默认旋转的圆心在iv左上角
RotateAnimation ra = new RotateAnimation(20, 360);
圆心的Y坐标:真实Y + iv高度 * 0.5
RotateAnimation ra = new RotateAnimation(20, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
//创建动画集合AnimationSet set = new AnimationSet(false);//往集合中添加动画set.addAnimation(aa);set.addAnimation(sa);set.addAnimation(ra);iv.startAnimation(set);
传入100,代表x终点坐标:当前x + 100
//具有get、set方法的成员变量就称为属性ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "translationX", 0, 100) ;
2是到原本高度的2倍结束
ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "scaleY", 0.1f, 2);
透明度,0是完全透明,1是完全不透明
ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "alpha", 0.1f, 1);
270是结束角度
ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "rotation", 20, 270);
属性指定为rotationY是水平翻转
ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "rotationY", 20, 180);
第三个参数可变参数可以传入多个参数,可以实现往回位移(旋转、缩放、透明)
ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "translationX", 0, 70, 30, 100) ;
//创建动画师集合AnimatorSet set = new AnimatorSet();//设置要播放动画的组件set.setTarget(bt);//所有动画有先后顺序的播放//set.playSequentially(oa, oa2, oa3, oa4);//所有动画一起播放set.playTogether(oa, oa2, oa3, oa4);set.start();
标签:
原文地址:http://www.cnblogs.com/liuyu0529/p/4912566.html