Android ViewFlipper简单应用
程序员文章站
2024-02-24 08:49:42
android viewflipper 简单应用,废话不多说,直接看代码
activity_guide.xml
android viewflipper 简单应用,废话不多说,直接看代码
activity_guide.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/guide" tools:context="com.example.weijian.homeletterapplication.guideactivity"> <viewflipper android:id="@+id/main_viewflipper" android:layout_width="match_parent" android:layout_height="match_parent" > </viewflipper> </linearlayout>
guideactivity.java
import android.content.intent; import android.support.v7.app.actionbar; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.gesturedetector; import android.view.motionevent; import android.view.view; import android.view.windowmanager; import android.widget.imageview; import android.widget.viewflipper; public class guideactivity extends appcompatactivity implements gesturedetector.ongesturelistener { private gesturedetector detector; private viewflipper flipper; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_guide); //隐藏标题栏 actionbar actionbar = getsupportactionbar(); actionbar.hide(); detector = new gesturedetector(this); flipper = (viewflipper) this.findviewbyid(r.id.main_viewflipper); flipper.addview(addimageview(r.mipmap.guide1)); flipper.addview(addimageview(r.mipmap.guide2)); flipper.addview(addimageview(r.mipmap.guide3)); } private view addimageview(int id) { imageview iv = new imageview(this); //imageview拉伸,充满整个viewflipper iv.setscaletype(imageview.scaletype.fit_xy); iv.setimageresource(id); return iv; } @override public boolean ontouchevent(motionevent event) { // todo auto-generated method stub return this.detector.ontouchevent(event); } @override public boolean ondown(motionevent e) { // todo auto-generated method stub return false; } @override public boolean onfling(motionevent e1, motionevent e2, float velocityx, float velocityy) { if (e1.getx() - e2.getx() > 120) { this.flipper.shownext(); } if (e1.getx() - e2.getx() < -120) { this.flipper.showprevious(); } return false; } @override public void onlongpress(motionevent e) { // todo auto-generated method stub } @override public boolean onscroll(motionevent e1, motionevent e2, float distancex, float distancey) { // todo auto-generated method stub return false; } @override public void onshowpress(motionevent e) { // todo auto-generated method stub } @override public boolean onsingletapup(motionevent e) { // todo auto-generated method stub return false; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。