当前位置: 移动技术网 > IT编程>移动开发>Android > 基于AnDroid FrameLayout的使用详解

基于AnDroid FrameLayout的使用详解

2019年07月24日  | 移动技术网IT编程  | 我要评论

270004,张飞跃事件,旌德社区

今天在学习实现墨迹天气那样的拖动效果时,看到用的是重写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>

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网