Fragment:
在安卓手機越來越大的情況下,一個屏幕顯示只顯示一個內容,會顯得空曠,布局不夠好看,屏幕利用的也不夠充分。通常在平板中會出現這種情況,但是由於現在的手機屏幕越來越大,所以在3.0之后引入了Fragment。也可以使用支持類庫,來向下兼容低版本。它的生命周期方法和activity一樣
使用Fragment可以讓我們更加充分地利用手機的屏幕空間,他可以在一個activity中有多個Fragment,多個Fragment可以顯示不同的內容。
我們通過簡單的demo來了解什么是Fragment和Fragment如果使用
在這個demo中我們使用FrameLayout(框架布局),Framelayout一般很少用到,在下面的Demo中使用會很方便,所有添加到這個布局中的視圖都以層疊的方式顯示。第一個添加的控件被放在最底層,最后一個添加到框架布局中的視圖顯示在最頂層,上一層的控件會覆蓋下一層的控件。
需求:我們在一個activity中的左邊豎直分布,三個button,右邊是一個Framelayout,當我們點擊不同的button時,右邊顯示不同的Fragment。
第一步:寫布局文件,布局文件左邊包含三個按鈕,右邊是一個FrameLayout。 再寫三個填充用的布局文件,這三個布局文件中background的顏色都不一樣,有一個textView用來區分不同的布局文件。
第二步:寫三個 fragment的Java類 分別將三個布局文件填充到對應的java類中作為內容顯示在屏幕上
public class fragment01 extends Fragment {
//返回的view對象會作為fragment01的內容顯示在屏幕上
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment, null);
return v;
}
第三步 在MainActivity中寫對三個Button做處理
當點擊按鈕一的時候,屏幕右邊顯示的是Fragment01 ,其它兩個按鈕功能相同。
第一步 new出Fragment01的對象
第二步 獲取Fragment的管理器getFragmentManager()
第三步 打開事物beginTransaction()
第四步 把內容顯示到幀布局replace 最后提交事物commit
public void click1(View v) {
fragment01 fg = new fragment01();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fl,fg );
ft.commit();
}
fragment是3.0之后出來的,我們可以做向下兼容,讓低版本可以使用。這個時候就用到了安卓支持類庫。 我們在引包的時候都改成support的包但是要注意的是,但是在獲去fragment管理器,沒有支持類庫,我們可以使用getSupportFragmentManager()來獲得兼容低版本的管理器
在Fragment和activity中傳遞數據
把Fragment中的數據傳遞到activity中做法:
在Fragment的xml文件中,放一個EditText。還有一個button 但我們點擊button的時候把數據傳遞到 activity的TextView中。
把activity中的數據傳遞到Fragment中
步驟:1 我們在fragment中的布局文件中,定義一個輸入框, 在定義一個button,但是onClick屬性只能在上下文中使用,在Fragment中不可以使用
我們可以給按鈕設置點擊偵聽
2 在MainActivity的布局文件中,定義一個TextView 用來顯示數據
3我們在MainActivity定義一個方法,用來給TextView 設置顯示的數據
4 在Fragment的java類中 我們拿到button 給button設置監聽,在按鈕點擊的時候,拿到輸入框中的數據通過getActivity()方法將它強轉成MainActivity就可以拿到和Fragment相關聯的activity,這個時候就可以調用activity中的方法給textview設置值了。
注意 :如果將fragment和activity中的EditText和TextView的id設置成相同,雖然findViewById是在各自的布局文件中拿,但是由於Activity中給TextView設置值得方法是在fragment中調用,那么Activity中的方法會優先在fragment的布局文件中找,而不再自己的布局文件中尋找。
如果兩個id相同的話解決方案是,在activity中把拿去布局文件的方法變量提成全局,在activity創建方法中取拿到
把activity中的數據傳遞到Fragment中的做法:
步驟:1 在fragment的xml文件中定義一個TextView,用來顯示數據。
2 我們在fragment的java類中拿到 定義的TextView, 創建一個公開的方法,這個方法內可以個TextView設置值
3 我們在MainActivity中定義一個EditText用來輸入數據,定義一個button,點擊的時候傳遞數據
4 當我們點擊按鈕的時候我們拿到EditText中的數據,同時new出對應要傳遞的fragment的java類的對象,直接用fragment對象調用它的方法傳值。
fragment的生命周期方法和activity的生命周期方法相同並且是綁定的,fragment切換時舊fragment對象會銷毀,新的fragment對象會被創建
幀動畫:
一張張圖片不斷的切換,形成動畫效果,安卓手機的開機界面就是通過幀動畫做的
如何自己使用幀動畫:
步驟:1將素材拷貝到drawable中
2 在drawable目錄下定義xml文件(在api中的drawable Animation中可以看到xml文件的格式和使用代碼)
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> //這里的false表示循環播放 true表示只播放一次 <item android:drawable="@drawable/g1" android:duration="200" />//表示duration表示每張圖片顯示的時長 <item android:drawable="@drawable/g2" android:duration="200" /> <item android:drawable="@drawable/g3" android:duration="200" /> </animation-list>
3 在屏幕上播放幀動畫
ImageView iv = (ImageView) findViewById(R.id.iv); //首先拿到imageView 將動畫播放在imageView
//把動畫文件設置為imageView的背景
iv.setBackgroundResource(R.drawable.animations); ////把幀動畫的資源文件指定為iv的背景
AnimationDrawable ad = (AnimationDrawable) iv.getBackground();//獲取iv的北京
//播放動畫
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); //iv表示動畫顯示顯示在哪個地方 通過findviewbyid拿到
旋轉
public void rotate(View v){
//表示從0到720旋轉 表示從真實的x+圖片的寬度的一半 在中心旋轉
RotateAnimation 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 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); //o.5f表示動畫的起始寬度是真實寬度的0.5倍 2表示動畫結束寬度是真實寬度的2倍 后面參數表示旋轉中心點的位置 默認是坐上角
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); //0表示完全透明。1表示不透明
aa.setDuration(2000);
sa.setRepeatCount(1);
iv.startAnimation(aa);
}
我們可以設置所有的動畫一起播放
public void fly(View v){
AnimationSet set = new AnimationSet(false);//false表示使用自己的叫對器
set.addAnimation(ta);
set.addAnimation(sa);
set.addAnimation(ra);
set.addAnimation(aa);
iv.startAnimation(set);
}
屬性動畫
補間動畫,只是一個動畫效果,組件其實還在原來的位置上,xy沒有改變。而屬性動畫是xy真實改變了,屬性動畫是3.0后加入的特性。沒有向下支持類庫
位移:
public void translate(View v){
//target:動畫作用於哪個組件 第一個參數target指定要顯示動畫的組件 第二個參數propertyName指定要改變組件的哪個屬性 第三個參數values是可變參數,就是賦予屬性的新的值
ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "translationX", 10, 70, 20, 100); //他可以實現來回移動 我們可以動過set和get來看屬性是什么
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.5f, 0.2f, 1);
oa.setDuration(2000);
oa.start();
}
旋轉
public void rotate(View v){
ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "rotationY", 0, 180, 90, 360); //rotation指定是順時針旋轉
//屬性指定為rotationX是豎直翻轉
//屬性指定為rotationY是水平翻轉
oa.setDuration(2000); oa.setRepeatCount(1); oa.setRepeatMode(ValueAnimator.REVERSE); oa.start(); }
所有動畫一起播放
//創建動畫師集合 AnimatorSet set = new AnimatorSet(); //設置要播放動畫的組件 set.setTarget(bt); //所有動畫有先后順序的播放 //set.playSequentially(oa, oa2, oa3, oa4); //所有動畫一起播放 set.playTogether(oa, oa2, oa3, oa4); set.start();
使用xml文件定義屬性動畫
<?xml version="1.0" encoding="utf-8"?> 在animator文件下 resource type 選擇property animator root element是set <set xmlns:android="http://schemas.android.com/apk/res/android" > <objectAnimator android:propertyName="translationX" android:duration="200" android:repeatCount="1" android:repeatMode="reverse" android:valueFrom="-100" android:valueTo="100" > </objectAnimator> </set>
public void xml(View v){
Animator at = AnimatorInflater.loadAnimator(this, R.animator.objanimator); //加載指定xml
//設置作用於哪個組件
at.setTarget(iv);
at.start();
}
