基于AnDroid FrameLayout的使用詳解
更新時(shí)間:2013年05月20日 18:01:06 作者:
本篇文章是對(duì)AnDroid FrameLayout的使用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
今天在學(xué)習(xí)實(shí)現(xiàn)墨跡天氣那樣的拖動(dòng)效果時(shí),看到用的是重寫(xiě)FrameLayout。翻了翻書(shū),突然想明白,為什么用FrameLayout.
在FrameLayout中,用我看的書(shū)中的話說(shuō)是,空間永遠(yuǎn)用不完。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#897753"
>
<ImageView
android:id="@+id/image1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="invisible"
android:src="@drawable/sky"/>
<ImageView
android:id="@+id/image2"
android:visibility="invisible"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/cloud"/>
<ImageView
android:id="@+id/image3"
android:visibility="invisible"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/sun"/>
</FrameLayout>
其中,image1、image2、image3都是在同一塊空間的。可以說(shuō)它們是重疊著的,界面顯示的是最近用的那一個(gè)。
在我的代碼中把它們都先不可見(jiàn)。
在整體代碼中實(shí)現(xiàn)的是點(diǎn)一下屏幕就換一張圖片。
另外,我個(gè)人感覺(jué),實(shí)現(xiàn)拖動(dòng)效果的關(guān)鍵原理就是framelayout使得幾部分空間的重疊。設(shè)置只有一部分可見(jiàn)。當(dāng)拖動(dòng)時(shí),設(shè)置其他部分移動(dòng)。
發(fā)現(xiàn)下載附近要扣e幣,我把代碼也貼上,圖片可以換成自己喜歡的~
FramLayoutTestActivity.java
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.widget.ImageView;
public class FramLayoutTestActivity extends Activity {
private String TAG = "FramLayoutTestActivity";
private ImageView image1;
private ImageView image2;
private ImageView image3;
private List<ImageView> list;
private int count=0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
image1=(ImageView)findViewById(R.id.image1);
image2=(ImageView)findViewById(R.id.image2);
image3=(ImageView)findViewById(R.id.image3);
list=new ArrayList<ImageView>();
list.add(image1);
list.add(image2);
list.add(image3);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction()==MotionEvent.ACTION_DOWN)
{
Log.i(TAG,"move---");
showImage();
}
return super.onTouchEvent(event);
}
private void showImage()
{
image1.setVisibility(View.VISIBLE);
count=count%3;
for(ImageView i:list)
{
i.setVisibility(View.INVISIBLE);
}
list.get(count).setVisibility(View.VISIBLE);
count++;
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#897753"
>
<ImageView
android:id="@+id/image1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="invisible"
android:src="@drawable/sky"/>
<ImageView
android:id="@+id/image2"
android:visibility="invisible"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/cloud"/>
<ImageView
android:id="@+id/image3"
android:visibility="invisible"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/sun"/>
</FrameLayout>
在FrameLayout中,用我看的書(shū)中的話說(shuō)是,空間永遠(yuǎn)用不完。
復(fù)制代碼 代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#897753"
>
<ImageView
android:id="@+id/image1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="invisible"
android:src="@drawable/sky"/>
<ImageView
android:id="@+id/image2"
android:visibility="invisible"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/cloud"/>
<ImageView
android:id="@+id/image3"
android:visibility="invisible"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/sun"/>
</FrameLayout>
其中,image1、image2、image3都是在同一塊空間的。可以說(shuō)它們是重疊著的,界面顯示的是最近用的那一個(gè)。
在我的代碼中把它們都先不可見(jiàn)。
在整體代碼中實(shí)現(xiàn)的是點(diǎn)一下屏幕就換一張圖片。
另外,我個(gè)人感覺(jué),實(shí)現(xiàn)拖動(dòng)效果的關(guān)鍵原理就是framelayout使得幾部分空間的重疊。設(shè)置只有一部分可見(jiàn)。當(dāng)拖動(dòng)時(shí),設(shè)置其他部分移動(dòng)。
發(fā)現(xiàn)下載附近要扣e幣,我把代碼也貼上,圖片可以換成自己喜歡的~
FramLayoutTestActivity.java
復(fù)制代碼 代碼如下:
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.widget.ImageView;
public class FramLayoutTestActivity extends Activity {
private String TAG = "FramLayoutTestActivity";
private ImageView image1;
private ImageView image2;
private ImageView image3;
private List<ImageView> list;
private int count=0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
image1=(ImageView)findViewById(R.id.image1);
image2=(ImageView)findViewById(R.id.image2);
image3=(ImageView)findViewById(R.id.image3);
list=new ArrayList<ImageView>();
list.add(image1);
list.add(image2);
list.add(image3);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction()==MotionEvent.ACTION_DOWN)
{
Log.i(TAG,"move---");
showImage();
}
return super.onTouchEvent(event);
}
private void showImage()
{
image1.setVisibility(View.VISIBLE);
count=count%3;
for(ImageView i:list)
{
i.setVisibility(View.INVISIBLE);
}
list.get(count).setVisibility(View.VISIBLE);
count++;
}
}
main.xml
復(fù)制代碼 代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#897753"
>
<ImageView
android:id="@+id/image1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="invisible"
android:src="@drawable/sky"/>
<ImageView
android:id="@+id/image2"
android:visibility="invisible"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/cloud"/>
<ImageView
android:id="@+id/image3"
android:visibility="invisible"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/sun"/>
</FrameLayout>
相關(guān)文章
Android開(kāi)發(fā)中Activity屬性設(shè)置小結(jié)
Android應(yīng)用開(kāi)發(fā)中會(huì)經(jīng)常遇到Activity組件的使用,下面就來(lái)講解下Activity組件。Activity的生命周期、通信方式和IntentFilter等內(nèi)容,并提供了一些日常開(kāi)發(fā)中經(jīng)常用到的關(guān)于Activity的技巧和方法。通過(guò)本文,你可以進(jìn)一步了接Android中Activity的運(yùn)作方式。2015-05-05Android字符串資源文件format方法使用實(shí)例
本文介紹了Android的資源文件values/strings.xml中如何實(shí)現(xiàn)格式化字符串,這里舉個(gè)簡(jiǎn)單的例子供大家參考2013-11-11Android 將文件下載到指定目錄的實(shí)現(xiàn)代碼
本文通過(guò)實(shí)例代碼給大家介紹了android將文件下載到指定目錄的實(shí)現(xiàn)方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下吧2017-06-06Android實(shí)現(xiàn)電池管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)電池管理系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01Android編程中Perferences的用法實(shí)例分析
這篇文章主要介紹了Android編程中Perferences的用法,以實(shí)例形式較為詳細(xì)的分析了配置文件preferences.xml的功能、定義及使用方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11安卓GET與POST網(wǎng)絡(luò)請(qǐng)求的三種方式
今天小編就為大家分享一篇關(guān)于安卓GET與POST網(wǎng)絡(luò)請(qǐng)求的三種方式,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12Android入門(mén)之利用Spinner實(shí)現(xiàn)彈出選擇對(duì)話框
這篇文章主要為大家詳細(xì)介紹了Android里如何巧用Spinner做彈出選擇對(duì)話框,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的小伙伴可以了解一下2022-11-11