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

Android 手势滑动动画效果收集整理

一 、 利用View自身的setAnimation来实现动画(TextView、ImageView、ListView等都可以实现)

java代码:
[java]
public void UpdateViewContent(){ 
    TextView txtview = (TextView)findViewById(R.id.content_view); 
    txtview.setText(MyGetNextText()); 
    txtview.setAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in)); 

Xml代码:

[html] 
<span style="font-weight: normal;"><?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"> 
    <translate android:fromXDelta="0" android:toXDelta="100%" android:duration="300"/> 
    <alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="300" /> 
</set></span> 

关键代码:
[html] 
<span style="font-weight: normal; ">txtview.setAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));</span> 
 java代码:
[java] 
package com.android.flip; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.GestureDetector; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.GestureDetector.OnGestureListener; 
import android.view.animation.AnimationUtils; 
import android.widget.ImageView; 
import android.widget.ViewFlipper; 
 
/**
 * Android实现左右滑动效果
 * @Description: Android实现左右滑动效果
 
 * @File: MainActivity.java
 
 * @Package com.android.flip
 
 * @Author Hanyonglu
 
 * @Date 2012-02-12 上午10:44:04
 
 * @Version V1.0
 */ 
public class MainActivity extends Activity implements OnGestureListener { 
    private ViewFlipper flipper; 
    private GestureDetector detector; 
     
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
         
        detector = new GestureDetector(this); 
        flipper = (ViewFlipper) this.findViewById(R.id.ViewFlipper1); 
 
        //添加图片 
        flipper.addView(addImageView(R.drawable.one)); 
        flipper.addView(addImageView(R.drawable.two)); 
        flipper.addView(addImageView(R.drawable.three)); 
        flipper.addView(addImageView(R.drawable.four)); 
        flipper.addView(addImageView(R.drawable.five)); 
        //添加layout 
        //flipper.addView(addView(R.layout.layout1)); 
    } 
     
    private View addImageView(int id) { 
        ImageView iv = new ImageView(this); 
        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.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in)); 
            this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out)); 
            this.flipper.showNext(); 
            return true; 
        } else if (e1.getX() - e2.getX() < -120) { 
            this.flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_in)); 
            this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_out)); 
            this.flipper.showPrevious(); 
            return true; 
        } 
         
        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) { 
      &

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