当前位置:编程学习 > JAVA >>

PageFlipper动态加载View

知识点:

     1.PageFlipper基本用法及动态添加View;

     2.PageFlipper结合手势翻页+特效;

     3.WindowManager的基本用法。

任意张图片实现循环滑动,其实PageFlipper当前最多的子View个数(ChildViewCount)小于等于2.

 

 fliper_item.xml:


[java] 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center" > 
 
    <ImageView 
        android:id="@+id/img" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:gravity="center" /> 
 
</LinearLayout> 

main.xml:

[java] 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
 
    <ViewFlipper 
        android:id="@+id/myViewFlipper" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" > 
         
    </ViewFlipper> 
 
</LinearLayout> 

MainActivity.java:

[java]
package com.xyz.pagefilter; 
 
import android.app.Activity; 
import android.content.Context; 
import android.graphics.PixelFormat; 
import android.os.Bundle; 
import android.os.Handler; 
import android.os.Message; 
import android.view.GestureDetector; 
import android.view.GestureDetector.OnGestureListener; 
import android.view.Gravity; 
import android.view.LayoutInflater; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnTouchListener; 
import android.view.Window; 
import android.view.WindowManager; 
import android.view.WindowManager.LayoutParams; 
import android.widget.ImageView; 
import android.widget.ViewFlipper; 
 
public class MainActivity extends Activity implements OnTouchListener, 
        OnGestureListener { 
 
    private LayoutInflater mInflater; 
    private WindowManager wm = null; 
    private WindowManager.LayoutParams wmParams = null; 
 
    private ImageView leftbtn = null; 
    private ImageView rightbtn = null; 
 
    private int mAlpha = 0; 
    private boolean isHide; 
 
    private int mCurrPos = 0; 
 
    private ViewFlipper viewFlipper = null; 
    private GestureDetector mGestureDetector; 
 
    private int[] mImages = new int[] { R.drawable.img_0, R.drawable.img_1, 
            R.drawable.img_2, R.drawable.img_3, R.drawable.img_4, 
            R.drawable.img_5, R.drawable.img_6, R.drawable.img_7, 
            R.drawable.img_8, R.drawable.img_9 }; 
 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        requestWindowFeature(Window.FEATURE_NO_TITLE); 
        setContentView(R.layout.main); 
 
        mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
        viewFlipper = (ViewFlipper) this.findViewById(R.id.myViewFlipper); 
        setView(mCurrPos, 0); 
        initFloatView(); 
 
        // viewFlipper.setLongClickable(true); 
        viewFlipper.setOnTouchListener(this); 
        mGestureDetector = new GestureDetector(this); 
 
    } 
 
    private void initFloatView() { 
        wm = (WindowManager) getApplicationContext().getSystemService( 
                Context.WINDOW_SERVICE); 
        wmParams = new WindowManager.LayoutParams(); 
 
        wmParams.type = LayoutParams.TYPE_PHONE;  
        wmParams.format = PixelFormat.RGBA_8888;  
         
        wmParams.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL 
                | LayoutParams.FLAG_NOT_FOCUSABLE; 
 
        wmParams.x = 0; 
        wmParams.y = 0; 
 
        wmParams.width = 50; 
        wmParams.height = 50; 
 
        createLeftFloatView(); 
        createRightFloatView(); 
    } 
 
    private void createLeftFloatView() { 
        leftbtn = new ImageView(this); 
        leftbtn.setImageResource(R.drawable.prev); 
        leftbtn.setAlpha(0); 
        leftbtn.setOnClickListener(new View.OnClickListener() { 
            public void onClick(View arg0) { 
                movePrevious(); 
            } 
      &nb

补充:web前端 , JavaScript ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,