日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android应用开发:动画和Fragment

發布時間:2025/4/16 Android 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android应用开发:动画和Fragment 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

幀動畫FrameAnimation

  • 多張圖片快速切換,形成動畫效果
  • 幀動畫使用xml定義
  • frame動畫是放置到res/drawable/下面,在這里定義要顯示的圖片和每張圖片的顯示時長
<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(); public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ImageView iv = (ImageView) findViewById(R.id.iv);//把幀動畫的資源文件指定為iv的背景iv.setBackgroundResource(R.drawable.frameanimation);//獲取iv的背景AnimationDrawable ad = (AnimationDrawable) iv.getBackground();ad.start();} }

補間動畫

  • tween動畫是放置到res/anim/下面
  • 組件由原始狀態向終極狀態轉變時,為了讓過渡更自然,而自動生成的動畫

1、透明動畫AlphaAnimation

  • AlphaAnimation aa = new AlphaAnimation(0, 0.5f);
    • 0表示動畫的起始透明度
    • 0.5f表示動畫的結束透明度
<?xml version="1.0" encoding="utf-8"?> <alpha xmlns:android="http://schemas.android.com/apk/res/android"android:fromAlpha="0.0" 開始透明度的值,完全不透明android:toAlpha="1.0" 結束透明度的值,完全透明android:duration="2000"android:repeatCount="0" 動畫效果重復幾次android:repeatMode="restart" 動畫效果重復的模式 參數有重新開始和倒著執行的模式android:interpolator="@android:anim/decelerate_interpolator"減速加速器> </alpha>

2、位移動畫TranslateAnimation

TranslateAnimation ta = new TranslateAnimation(10, 100, 20, 200);
  • 10:表示的x坐標起始位置

    • iv的真實x + 10
  • 100:表示x坐標的結束位置

    • iv的真實x + 100
  • 20:表示y坐標的起始位置

    • iv的真實y + 20
  • 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坐標的初始位置

    • iv的真實x + 1 * iv寬
  • Animation.RELATIVE_TO_SELF, 0.5f:y坐標的起始位置

    • iv的真實y + 0.5 * iv高
<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android"android:duration="2000"android:fromXDelta="0" 開始像素android:toXDelta="100" 水平平移結束像素android:fromYDelta="0"android:toYDelta="100"android:startOffset="1000" 動畫開始時間 1秒之后開始播放動畫></translate>
  • 動畫播放相關的設置
//設置動畫持續時間 ta.setDuration(2000); //動畫重復播放的次數 ta.setRepeatCount(1); //動畫重復播放的模式 ta.setRepeatMode(Animation.REVERSE); //動畫播放完畢后,組件停留在動畫結束的位置上 ta.setFillAfter(true); //播放動畫 iv.startAnimation(ta);

3、縮放動畫ScaleAnimation

ScaleAnimation sa = new ScaleAnimation(0.5f, 2, 0.1f, 3, iv.getWidth() / 2, iv.getHeight() / 2);
  • 0.5f:表示x坐標縮放的初始位置
    • 0.5 * iv寬
  • 2:表示x坐標縮放的結束位置
    • 2 * iv寬
  • 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);
  • Animation.RELATIVE_TO_SELF, 0.5f:表示縮放點的x坐標
    • iv的真實x + 0.5 * iv寬
<?xml version="1.0" encoding="utf-8"?> <scale xmlns:android="http://schemas.android.com/apk/res/android"android:duration="2000"android:pivotx="0.0"android:pivoty="50.0" Y軸中間android:fromXScale="0.0" 開始x軸比例android:toXScale="2.0" 結束x軸比例android:fromYScale="0.0" 開始x軸比例android:toYScale="2.0"android:repeatMode="reverse" 重復相反的模式android:repeatCount="1"></scale>

4、旋轉動畫RotateAnimation

RotateAnimation ra = new RotateAnimation(20, 360);
  • 20表示動畫開始時的iv的角度
  • 360表示動畫結束時iv的角度
  • 默認旋轉的圓心在iv左上角
RotateAnimation ra = new RotateAnimation(20, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
  • 20,360的意義和上面一樣
  • 指定圓心坐標,相對于自己,值傳入0.5,那么圓心的x坐標:真實X + iv寬度 * 0.5
  • 圓心的Y坐標:真實Y + iv高度 * 0.5
<?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android"android:fromDegrees="0" 旋轉開始的角度android:toDegrees="90" 旋轉結束的角度android:pivotX="50%p" 代表當前的中間位置 加上p的意思是當前父布局管理器中間位置android:pivotY="50%p" 代表當前的中間位置android:duration="2000"> </rotate>

5、所有動畫一起飛

//創建動畫集合 AnimationSet set = new AnimationSet(false); //往集合中添加動畫 set.addAnimation(aa); set.addAnimation(sa); set.addAnimation(ra); iv.startAnimation(set);

6、DEMO

public class MainActivity extends Activity {private ImageView iv;private RotateAnimation ra;private AlphaAnimation aa;private ScaleAnimation sa;private TranslateAnimation ta;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);iv = (ImageView) findViewById(R.id.iv);}public void translate(View v){ // ta = new TranslateAnimation(10, 100, 20, 200);ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF, -1, Animation.RELATIVE_TO_SELF, 2,Animation.RELATIVE_TO_SELF, -0.5f, Animation.RELATIVE_TO_SELF, 1.5f);//設置播放時間ta.setDuration(2000);//設置重復次數ta.setRepeatCount(1);ta.setRepeatMode(Animation.REVERSE);iv.startAnimation(ta);}public void scale(View v){ // sa = new ScaleAnimation(fromX, toX, fromY, toY, iv.getWidth() / 2, iv.getHeight() / 2);sa = new ScaleAnimation(0.5f, 2, 0.1f, 3, Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f);sa.setDuration(2000);//填充動畫的結束位置sa.setRepeatCount(1);sa.setRepeatMode(Animation.REVERSE);sa.setFillAfter(true);iv.startAnimation(sa);}public void alpha(View v){aa = new AlphaAnimation(0, 1);aa.setDuration(2000);sa.setRepeatCount(1);iv.startAnimation(aa);}public void rotate(View v){ra = new RotateAnimation(0, 720, Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f);ra.setDuration(2000);ra.setRepeatCount(1);ra.setRepeatMode(Animation.REVERSE);iv.startAnimation(ra);}public void fly(View v){AnimationSet set = new AnimationSet(false);set.addAnimation(ta);set.addAnimation(sa);set.addAnimation(ra);set.addAnimation(aa);iv.startAnimation(set);} }

三、屬性動畫

  • 補間動畫,只是一個動畫效果,組件其實還在原來的位置上,xy沒有改變,而屬性動畫改變了組件的屬性,組件不在原來的位置上,xy坐標已經改變

1、位移:

//具有get、set方法的成員變量就稱為屬性 ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "translationX", 0, 100) ;
  • 第一個參數target指定要顯示動畫的組件
  • 第二個參數propertyName指定要改變組件的哪個屬性
  • 第三個參數values是可變參數,就是賦予屬性的新的值
  • 傳入0,代表x起始坐標:當前x + 0
  • 傳入100,代表x終點坐標:當前x + 100

2、縮放:

ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "scaleY", 0.1f, 2);
  • 第三個參數指定縮放的比例
  • 0.1是從原本高度的十分之一開始
  • 2是到原本高度的2倍結束

3、透明:

ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "alpha", 0.1f, 1);
  • 透明度,0是完全透明,1是完全不透明

4、旋轉

ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "rotation", 20, 270);
  • rotation指定是順時針旋轉
  • 20是起始角度
  • 270是結束角度
ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "rotationY", 20, 180);
  • 屬性指定為rotationX是豎直翻轉
  • 屬性指定為rotationY是水平翻轉

5、可變參數

  • 第三個參數可變參數可以傳入多個參數,可以實現往回位移(旋轉、縮放、透明)
ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "translationX", 0, 70, 30, 100) ;

6、所有動畫一起飛

//創建動畫師集合AnimatorSet set = new AnimatorSet();//設置要播放動畫的組件set.setTarget(bt);//所有動畫有先后順序的播放//set.playSequentially(oa, oa2, oa3, oa4);//所有動畫一起播放set.playTogether(oa, oa2, oa3, oa4);set.start();

7、DEMO

public class MainActivity extends Activity {private ImageView iv;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);iv = (ImageView) findViewById(R.id.iv);iv.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(MainActivity.this, "點不到我", 0).show();}});}public void translate(View v){ // TranslateAnimation ta = new TranslateAnimation(0, 150, 0, 0); // ta.setDuration(2000); // ta.setFillAfter(true); // iv.startAnimation(ta);//target:動畫作用于哪個組件ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "translationX", 10, 70, 20, 100);oa.setDuration(2000);oa.setRepeatCount(1);oa.setRepeatMode(ValueAnimator.REVERSE);oa.start();}public void scale(View v){ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "scaleX", 1, 1.6f, 1.2f, 2);oa.setDuration(2000);oa.start();}public void alpha(View v){ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "alpha", 0, 0.6f, 0.2f, 1);oa.setDuration(2000);oa.start();}public void rotate(View v){ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "rotationY", 0, 180, 90, 360);oa.setDuration(2000);oa.setRepeatCount(1);oa.setRepeatMode(ValueAnimator.REVERSE);oa.start();}public void fly(View v){AnimatorSet set = new AnimatorSet();ObjectAnimator oa1 = ObjectAnimator.ofFloat(iv, "translationX", 10, 70, 20, 100);oa1.setDuration(2000);oa1.setRepeatCount(1);oa1.setRepeatMode(ValueAnimator.REVERSE);ObjectAnimator oa2 = ObjectAnimator.ofFloat(iv, "translationY", 10, 70, 20, 100);oa2.setDuration(2000);oa2.setRepeatCount(1);oa2.setRepeatMode(ValueAnimator.REVERSE);ObjectAnimator oa3 = ObjectAnimator.ofFloat(iv, "scaleX", 1, 1.6f, 1.2f, 2);oa3.setDuration(2000);oa3.setRepeatCount(1);oa3.setRepeatMode(ValueAnimator.REVERSE);ObjectAnimator oa4 = ObjectAnimator.ofFloat(iv, "rotation", 0, 180, 90, 360);oa4.setDuration(2000);oa4.setRepeatCount(1);oa4.setRepeatMode(ValueAnimator.REVERSE);//設置挨個飛 // set.playSequentially(oa1, oa2, oa3, oa4);//設置一起飛set.playTogether(oa1, oa2, oa3, oa4);set.start();}public void xml(View v){Animator at = AnimatorInflater.loadAnimator(this, R.animator.objanimator);//設置作用于哪個組件at.setTarget(iv);at.start();}}

Fragment

  • 用途:在一個Activity里切換界面,切換界面時只切換Fragment里面的內容
  • 生命周期方法跟Activity一致,可以理解把其為就是一個Activity
  • 定義布局文件作為Fragment的顯示內容
//此方法返回的View就會被顯示在Fragment上 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// TODO Auto-generated method stub//用布局文件填充成一個View對象,返回出去,那么就顯示在Fragment上了View v = inflater.inflate(R.layout.fragment01, null);return v; }
  • 把Fragment顯示至指定ViewGroup中
//把fragment顯示至界面 //new出fragment對象 Fragment01 fg = new Fragment01(); FragmentManager fm = getFragmentManager(); //開啟事務 FragmentTransaction ft = fm.beginTransaction(); //把fragment對象顯示到指定資源id的組件里面 ft.replace(R.id.fl, fg); ft.commit();

生命周期

  • fragment切換時舊fragment對象會銷毀,新的fragment對象會被創建

低版本兼容

  • 在support-v4.jar包中有相關api,也就是說fragment可以在低版本模擬器運行

動畫

幀動畫

一張張圖片不斷的切換,形成動畫效果

  • 在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();

補間動畫

  • 原形態變成新形態時為了過渡變形過程,生成的動畫就叫補間動畫
  • 位移、旋轉、縮放、透明

位移:

  • 參數10指的是X的起點坐標,但不是指屏幕x坐標為10的位置,而是imageview的 真實X + 10
  • 參數150指的是X的終點坐標,它的值是imageview的 真實X + 150
//創建為位移動畫對象,設置動畫的初始位置和結束位置 TranslateAnimation ta = new TranslateAnimation(10, 150, 20, 140);
  • x坐標的起點位置,如果相對于自己,傳0.5f,那么起點坐標就是 真實X + 0.5 * iv寬度
  • x坐標的終點位置,如果傳入2,那么終點坐標就是 真實X + 2 * iv的寬度
  • y坐標的起點位置,如果傳入0.5f,那么起點坐標就是 真實Y + 0.5 * iv高度
  • 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);

縮放:

  • 參數0.1f表示動畫的起始寬度是真實寬度的0.1倍
  • 參數4表示動畫的結束寬度是真實寬度的4倍
  • 縮放的中心點在iv左上角

ScaleAnimation sa = new ScaleAnimation(0.1f, 4, 0.1f, 4);

  • 參數0.1f和4意義與上面相同
  • 改變縮放的中心點:傳入的兩個0.5f,類型都是相對于自己,這兩個參數改變了縮放的中心點
  • 中心點x坐標 = 真實X + 0.5 * iv寬度
  • 中心點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);

旋轉:

  • 20表示動畫開始時的iv的角度
  • 360表示動畫結束時iv的角度
  • 默認旋轉的圓心在iv左上角

RotateAnimation ra = new RotateAnimation(20, 360);

  • 20,360的意義和上面一樣
  • 指定圓心坐標,相對于自己,值傳入0.5,那么圓心的x坐標:真實X + iv寬度 * 0.5
  • 圓心的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);

屬性動畫

  • 補間動畫,只是一個動畫效果,組件其實還在原來的位置上,xy沒有改變

位移:

  • 第一個參數target指定要顯示動畫的組件
  • 第二個參數propertyName指定要改變組件的哪個屬性
  • 第三個參數values是可變參數,就是賦予屬性的新的值
  • 傳入0,代表x起始坐標:當前x + 0
  • 傳入100,代表x終點坐標:當前x + 100
//具有get、set方法的成員變量就稱為屬性ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "translationX", 0, 100) ;

縮放:

  • 第三個參數指定縮放的比例
  • 0.1是從原本高度的十分之一開始
  • 2是到原本高度的2倍結束
ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "scaleY", 0.1f, 2);

透明:

  • 透明度,0是完全透明,1是完全不透明
ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "alpha", 0.1f, 1);

旋轉

  • rotation指定是順時針旋轉
  • 20是起始角度
  • 270是結束角度
ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "rotation", 20, 270);
  • 屬性指定為rotationX是豎直翻轉
  • 屬性指定為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();

幀動畫FrameAnimation

  • 多張圖片快速切換,形成動畫效果
  • 幀動畫使用xml定義

補間動畫

  • 組件由原始狀態向終極狀態轉變時,為了讓過渡更自然,而自動生成的動畫

位移動畫

TranslateAnimation ta = new TranslateAnimation(10, 100, 20, 200);
  • 10:表示的x坐標起始位置

    • iv的真實x + 10
  • 100:表示x坐標的結束位置

    • iv的真實x + 100
  • 20:表示y坐標的起始位置

    • iv的真實y + 20
  • 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坐標的初始位置

    • iv的真實x + 1 * iv寬
  • Animation.RELATIVE_TO_SELF, 0.5f:y坐標的起始位置

    • iv的真實y + 0.5 * iv高

縮放動畫

ScaleAnimation sa = new ScaleAnimation(0.5f, 2, 0.1f, 3, iv.getWidth() / 2, iv.getHeight() / 2);
  • 0.5f:表示x坐標縮放的初始位置
    • 0.5 * iv寬
  • 2:表示x坐標縮放的結束位置
    • 2 * iv寬
  • 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);
  • Animation.RELATIVE_TO_SELF, 0.5f:表示縮放點的x坐標
    • iv的真實x + 0.5 * iv寬

透明動畫

AlphaAnimation aa = new AlphaAnimation(0, 0.5f);
  • 0表示動畫的起始透明度
  • 0.5f表示動畫的結束透明度

總結

以上是生活随笔為你收集整理的Android应用开发:动画和Fragment的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。