技术开发 频道

android开发ViewFlipper触摸动画

  【IT168技术】介绍:在教程中,我们可以学习如何解决在Android项目的共同问题,有更多的关于Android的项目信息,我建议你下载ADT Android Pluglins和Eclipse。

  背景介绍:

  在做Android项目开发之前你需要做三件事情:第一、下载Android SDK(你可以在这里下载),第二、准备项目编辑器(我建议使用eclipse )第三、下载Android的Eclipse插件。

  当你准备好你的环境之后,可以开始学习本教程。

  当你第一次打开Android SDK,您需要创建一个虚拟设备,我打开建议创建一个普遍的设备为目标的Android 1.6 API Level 4使用,然后你需要添加硬件功能,例如:SD卡支持,Accellerometer,相机支持等等,如果你需要帮助,安装Android虚拟设备跟随这个链接。http://developer.android.com/guide/developing/tools/avd.html

  开始eclipse项目:

  1、 选择File > New > Project.

  2、 选择Android > Android Project,点击 Next.

  3、 选择项目目录:

  A、输入一个项目名称:这是你所创建的项目所处的文件夹

  B、根据目录选择Create new project in workspace。:选择你的workspace的位置

  C、根据Target,选择Android的Target,作为该项目的创建Target使用。生成的Target指定你想建立的应用的Android平台。

  说明:除非你知道你将使用最新SDK中引入的新的API,否则你应该尽可能选择一个最低的平台版本,比如Android 1.6。

  D、根据属性填写必要的字段

  @键入应用程序的名字:

  @键入一个package 名:你的所有源代码都存放在这个package中

  @选择Create Activity项,然后为你的主Activity键入一个名字

  @输入一个Min SDK Version,这是一个整数,指示需要正确运行应用程序最低的API级别。进入这个这里会自动设置Android Manifest文件中的minSdkVersion属性。如果你不确定需要使用的合适的API 级别,为你在Target tab中所选的Build Target复制列出的API Level。

  4、 点击finish

  当你完成了之后,ADT已经为您创建好了以下文件夹和文件:

  src/:包含任何的ActivityJava文件。

  :包含编译应用程序所生成的android.jar文件。

  gen/:包含了由ADT所生成的Java文件。包含R.java文件和从AIDL文件创建的接口。

  assets/:这个是空的,可用来存储raw asset文件。

  res/:这是应用程序的资源文件,诸如:drawable files, layout files, string 值等。

  AndroidManifest.xml:项目的Android Manifest。

  default.properties:这个文件包含一些项目设置,如build target。这个文件是该项目的组成部分,因此,它被维护在一个源版本控制系统中。它绝不应手动编辑(编辑项目属性,右键单击该项目文件夹并选择“属性”)

  通过触摸屏的事件,如何滑动两个(或更多)布局,如新闻及天气。

  现在,你已经充分了解Android并准备好了SDK等环境的搭建,我们要想做Android开发的第一件事情就是仿效的图形效果。

  要做到这一点,我们需要一些Java代码和XML布局的一点技巧。

<ViewFlipper
    android:layout_margin
="6dip"
    android:id
="@+id/layoutswitcher"
    android:layout_width
="fill_parent"
    android:layout_height
="fill_parent">
    
<LinearLayout
        android:layout_width
="fill_parent"
        android:layout_height
="fill_parent">
        
<TextView
            android:layout_height
="wrap_content"
            android:id
="@+id/firstpanel"
            android:paddingTop
="10dip"
            android:text
="my first panel"
            android:layout_width
="fill_parent"
            android:layout_weight
="1"
            android:textStyle
="bold" >
        
</TextView>
    
</LinearLayout>
    
<LinearLayout
        android:layout_width
="fill_parent"
        android:layout_height
="fill_parent">
        
<TextView
            android:layout_height
="wrap_content"
            android:id
="@+id/secondpanel"
            android:paddingTop
="10dip"
            android:text
="my second panel"
            android:layout_width
="fill_parent"
            android:layout_weight
="1"
            android:textStyle
="bold" >
        
</TextView>
    
</LinearLayout>
</ViewFlipper>

  正如你所看到的,我添加了1个ViewFlipper,2 个LinearLayout并为每个内部布局simpletextview,建立一个适当的切换布局第一步是很有必要,你可以自定义你想要的两个内布局风格,以及如果你想要添加工具栏莱切换内部布局。

  第二步、Java 代码(通过触摸来切换布局)

  从src/中打开你的main class,并且在类的开始部分声明两个变量:

private ViewFlipper vf;  
    
private float oldTouchValue;

  第一个变量 VF用于constrol ViewFlipper,第二个变量时触摸事件所需要的。

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ....
        vf
= (ViewFlipper) findViewById(R.id.switchlayout);
     }  

  我们需要声明viewflipper。

  第三步:touch Event

  移动电话最重要的特点之一就是触摸屏系统,就像iPhone应用程序或其他设备,用户更喜欢用他们的手指通过接触在不同的Actives之间进行切换。

  在Android开发中,为了附加一个触摸事件你可以做两件事情:“实现touchlistener”通过重写主函数来声明onTouchEvent。在这个例子中,我们用到的是第二种情况。

@Override
    
public boolean onTouchEvent(MotionEvent touchevent) {
        switch (touchevent.getAction())
        {
            
case MotionEvent.ACTION_DOWN:
            {
                oldTouchValue
= touchevent.getX();
                break;
            }
            
case MotionEvent.ACTION_UP:
            {
                
if(this.searchOk==false) return false;
                float currentX
= touchevent.getX();
                
if (oldTouchValue < currentX)
                {
                    vf.setInAnimation(AnimationHelper.inFromLeftAnimation());
                    vf.setOutAnimation(AnimationHelper.outToRightAnimation());
                    vf.showNext();
                }
                
if (oldTouchValue > currentX)
                {
                    vf.setInAnimation(AnimationHelper.inFromRightAnimation());
                    vf.setOutAnimation(AnimationHelper.outToLeftAnimation());
                    vf.showPrevious();
                }
            break;
            }
        }
        return
false;
    }  

  正如你可以看到,当用户进行第一次触摸时,设备将会用他的手指的X坐标填补浮点变量(oldTouchValue),那么当用户将他的手指拿开时,我会检查最新的位置,以决定是否在用户是否要到下一个或上一个位置(oldTouchValue currentX)。要改变布局的代码是相同的,但如果用户想要去下一个位置或前一个位置时我会用不同的动画,为了解释这个动画和AnimationHelper,示例代码如下:

//for the previous movement
    
public static Animation inFromRightAnimation() {

        Animation inFromRight
= new TranslateAnimation(
        Animation.RELATIVE_TO_PARENT,  
+1.0f, Animation.RELATIVE_TO_PARENT,  0.0f,
        Animation.RELATIVE_TO_PARENT,  
0.0f, Animation.RELATIVE_TO_PARENT,   0.0f
        );
        inFromRight.setDuration(
350);
        inFromRight.setInterpolator(
new AccelerateInterpolator());
        return inFromRight;
        }
    
public static Animation outToLeftAnimation() {
        Animation outtoLeft
= new TranslateAnimation(
         Animation.RELATIVE_TO_PARENT,  
0.0f, Animation.RELATIVE_TO_PARENT,  -1.0f,
         Animation.RELATIVE_TO_PARENT,  
0.0f, Animation.RELATIVE_TO_PARENT,   0.0f
        );
        outtoLeft.setDuration(
350);
        outtoLeft.setInterpolator(
new AccelerateInterpolator());
        return outtoLeft;
        }    
    
// for the next movement
    
public static Animation inFromLeftAnimation() {
        Animation inFromLeft
= new TranslateAnimation(
        Animation.RELATIVE_TO_PARENT,  
-1.0f, Animation.RELATIVE_TO_PARENT,  0.0f,
        Animation.RELATIVE_TO_PARENT,  
0.0f, Animation.RELATIVE_TO_PARENT,   0.0f
        );
        inFromLeft.setDuration(
350);
        inFromLeft.setInterpolator(
new AccelerateInterpolator());
        return inFromLeft;
        }
    
public static Animation outToRightAnimation() {
        Animation outtoRight
= new TranslateAnimation(
         Animation.RELATIVE_TO_PARENT,  
0.0f, Animation.RELATIVE_TO_PARENT,  +1.0f,
         Animation.RELATIVE_TO_PARENT,  
0.0f, Animation.RELATIVE_TO_PARENT,   0.0f
        );
        outtoRight.setDuration(
350);
        outtoRight.setInterpolator(
new AccelerateInterpolator());
        return outtoRight;
        }  

  你可以决定把这些方法放在一个静态类中,或在类中进行声明。

  结论:

  现在你有基类和事件由右至左(show.previous),从左至右(show.next),来捕捉触摸运动。一个简单的代码解释了4个动画运动和一个简单的布局。当有两个以上的布局和更复杂的内容时我会建议做进一步测试。

0
相关文章