当前位置:操作系统 > 安卓/Android >>

Android左右滑动控件

先看效果:

 

 

main.xml:

Java代码

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

    android:orientation="vertical" android:layout_width="fill_parent" 

    android:layout_height="fill_parent"> 

    <TextView android:id="@+id/textView" android:layout_width="fill_parent" 

        android:layout_height="wrap_content" android:text="ddd" /> 

    <com.diydyq.android.swipeTest.SlipView 

        android:id="@+id/slipView" android:orientation="vertical" 

        android:layout_width="fill_parent" android:layout_height="fill_parent"> 

    </com.diydyq.android.swipeTest.SlipView> 

</LinearLayout> 

 

 

SlipEntity.java

Java代码

package com.iaiai.activity; 

 

import java.util.HashMap; 

import java.util.Map; 

 

import android.graphics.Bitmap; 

import android.util.Log; 

 

/**

 * 

 * <p>

 * Title: SlipEntity.java

 * </p>

 * <p>

 * E-Mail: 176291935@qq.com

 * </p>

 * <p>

 * QQ: 176291935

 * </p>

 * <p>

 * Http: iaiai.iteye.com

 * </p>

 * <p>

 * Create time: 2011-8-11

 * </p>

 * 

 * @author 丸子

 * @version 0.0.1

 */ 

public class SlipEntity { 

    public static final int FLAG_BACK = 1; 

    public static final int FLAG_FROT = 2; 

    public static final float DOCK_L = 0; 

    public static final float DOCK_M = (float) 0.5; 

    public static final float DOCK_R = 1; 

    public static final float MICRO_X = 10; 

    /** Background image */ 

 

    Bitmap backImage; 

    /** Front image */ 

    Bitmap frotImage; 

    /** Start Position of back image */ 

    float xBackStart; 

    /** Start Position of back image */ 

    float yBackStart; 

    /** Start Position of front image */ 

    float xFrotStart; 

    /** Start Position of front image */ 

    float yFrotStart; 

    /** initial Position of front image */ 

    float xFrotInitPos; 

    /** initial Position of front image */ 

    float yFrotInitPos; 

    /** Margin of front and back image in X-Axis */ 

    float xMarginLeft; 

    /** Margin of front and back image in Y-Axis */ 

    float yMarginTop; 

    /** Containing dock position of the front image */ 

 

    Map<Float, Float> dockPosList = new HashMap<Float, Float>(); 

    /** Current dock Percentage: DOCK_L | DOCK_M | DOCK_R */ 

    float curDockPer = DOCK_L; 

    /** Weather has invoked initSlipEntity() */ 

    boolean isInit = false; 

 

    public SlipEntity() { 

    } 

 

    public SlipEntity(Bitmap backImage, Bitmap frotImage) { 

        this.backImage = backImage; 

        this.frotImage = frotImage; 

    } 

 

    public SlipEntity(float xBackStart, float yBackStart, float xFrotStart, 

            float yFrotStart) { 

        this.xBackStart = xBackStart; 

        this.yBackStart = yBackStart; 

        this.xFrotStart = xFrotStart; 

        this.yFrotStart = yFrotStart; 

    } 

 

    public void initSlipEntity(float viewWidth, float viewHeight) { 

        this.xBackStart = (viewWidth - this.backImage.getWidth()) / 2; 

        this.yBackStart = (viewHeight - this.backImage.getHeight()) / 2; 

        this.xMarginLeft = 5; 

        this.yMarginTop = (this.backImage.getHeight() - this.frotImage 

                .getHeight()) / 2; 

        this.xFrotInitPos = this.xBackStart + this.xMarginLeft; 

        this.yFrotInitPos = this.yBackStart + this.yMarginTop; 

        this.xFrotStart = this.xFrotInitPos; 

        this.yFrotStart = this.yFrotInitPos; 

        // Add dock position 

        float dockL = this.xFrotInitPos; 

        float dockR = this.xBackStart + this.backImage.getWidth() 

                - this.frotImage.getWidth() - this.xMarginLeft; 

        this.dockPosList.put(DOCK_L, dockL); 

        this.dockPosList.put(DOCK_R, dockR); 

        for (Float dockPer : this.dockPosList.keySet()) { 

            if (this.dockPosList.get(dockPer) == 0) { 

                float docPos = (dockR - dockL) * dockPer 

                        + this.dockPosList.get(DOCK_L); 

                this.dockPosList.put(dockPer, docPos); 

            }

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,