基于AnDroid FrameLayout的使用詳解
更新時(shí)間:2013年05月20日 18:01:06 作者:
本篇文章是對(duì)AnDroid FrameLayout的使用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
今天在學(xué)習(xí)實(shí)現(xiàn)墨跡天氣那樣的拖動(dòng)效果時(shí),看到用的是重寫FrameLayout。翻了翻書,突然想明白,為什么用FrameLayout.
在FrameLayout中,用我看的書中的話說(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都是在同一塊空間的??梢哉f(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中,用我看的書中的話說(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都是在同一塊空間的??梢哉f(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 studio節(jié)省C盤空間的配置方法
這篇文章主要介紹了android studio節(jié)省C盤空間的配置方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-07-07
Android使用URLConnection提交請(qǐng)求的實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Android使用URLConnection提交請(qǐng)求的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
Android GuideView實(shí)現(xiàn)首次登陸引導(dǎo)
這篇文章主要為大家詳細(xì)介紹了Android GuideView實(shí)現(xiàn)首次登陸引導(dǎo),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03
Android開(kāi)發(fā)中使用顏色矩陣改變圖片顏色,透明度及亮度的方法
這篇文章主要介紹了Android開(kāi)發(fā)中使用顏色矩陣改變圖片顏色,透明度及亮度的方法,涉及Android針對(duì)圖片的讀取、運(yùn)算、設(shè)置等相關(guān)操作技巧,需要的朋友可以參考下2017-10-10
Window下adb shell中文亂碼問(wèn)題解決方法
這篇文章主要介紹了Window下adb shell中文亂碼問(wèn)題解決方法,本文講解了出現(xiàn)這個(gè)問(wèn)題的原因以及解決方法,需要的朋友可以參考下2015-04-04
基于android實(shí)現(xiàn)五子棋開(kāi)發(fā)
這篇文章主要為大家詳細(xì)介紹了基于android實(shí)現(xiàn)五子棋開(kāi)發(fā),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-02-02
Android下Button實(shí)現(xiàn)圖文混排效果
這篇文章主要為大家詳細(xì)介紹了Android下Button實(shí)現(xiàn)圖文混排效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
Android Activity啟動(dòng)模式之singleTask實(shí)例詳解
這篇文章主要介紹了Android Activity啟動(dòng)模式之singleTask,結(jié)合實(shí)例形式較為詳細(xì)的分析了singleTask模式的功能、使用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-01-01
Android模仿知乎的回答詳情頁(yè)的動(dòng)畫效果
這篇文章主要介紹了Android模仿“知乎”的回答詳情頁(yè)的動(dòng)畫效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-02-02

