基于AnDroid FrameLayout的使用详解

所属分类: 软件编程 / Android 阅读数: 1413
收藏 0 赞 0 分享
今天在学习实现墨迹天气那样的拖动效果时,看到用的是重写FrameLayout。翻了翻书,突然想明白,为什么用FrameLayout.
在FrameLayout中,用我看的书中的话说是,空间永远用不完。
复制代码 代码如下:

<?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都是在同一块空间的。可以说它们是重叠着的,界面显示的是最近用的那一个。
在我的代码中把它们都先不可见。
在整体代码中实现的是点一下屏幕就换一张图片。
另外,我个人感觉,实现拖动效果的关键原理就是framelayout使得几部分空间的重叠。设置只有一部分可见。当拖动时,设置其他部分移动。
发现下载附近要扣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>

更多精彩内容其他人还在看

Android实现信号强度监听的方法

这篇文章主要介绍了Android实现信号强度监听的方法,是Android手机中很常见的一个实用功能,需要的朋友可以参考下
收藏 0 赞 0 分享

Android实现Activity界面切换添加动画特效的方法

这篇文章主要介绍了Android实现Activity界面切换添加动画特效的方法,非常实用的技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中Dialog去黑边的方法

这篇文章主要介绍了Android中Dialog去黑边的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Qt for Android开发实例教程

这篇文章主要介绍了Qt for Android开发的方法,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发之时间日期操作实例

这篇文章主要介绍了Android开发之时间日期操作,是Android程序开发中常见的一个功能,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发之时间日期组件用法实例

这篇文章主要介绍了Android开发之时间日期组件用法,主要介绍了TimePicker和DatePicker组件,对于Android程序开发有不错的借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发之获取网络链接状态

这篇文章主要介绍了Android获取网络链接状态的方法,主要是通过ConnectivityManager类来完成的,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发之广播机制浅析

这篇文章主要介绍了Android开发之广播机制浅析,主要包括了发布、接收及配置广播的实例,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发之登录验证实例教程

这篇文章主要介绍了Android开发之登录验证实现方法,包括发送数据、服务器端验证、配置文件等,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发之注册登录方法示例

这篇文章主要介绍了Android开发的注册登录方法,是针对Android程序设计中版本兼容性的进一步完善,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多