Android Animation---拖动UI时UI产生动态效果

效果参见题目;

MainActivity.java

package com.example.androiddraguidemos;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AbsoluteLayout.LayoutParams;
import android.widget.TextView;

public class MainActivity extends Activity {

    private TextView mText;
    private boolean mflag=false;
    
    private Animation animation;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        mText=(TextView)findViewById(R.id.textview);
        
        /*mText.setOnTouchListener(new OnTouchListener(){

            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {
                // TODO Auto-generated method stub

                return false;
            }
            
        });*/

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        
        switch(event.getAction()){
        
        case MotionEvent.ACTION_DOWN:
            animation = AnimationUtils.loadAnimation(
                    MainActivity.this, R.anim.multi);
            mText.startAnimation(animation);
            mflag=true;
            break;
        case MotionEvent.ACTION_MOVE:
            if (mflag) {
                // flag为true即控件被点到时,执行移动控件操作
                int x = (int) event.getX();
                int y = (int) event.getY();
                // 得到X,Y座标
                LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
                        LayoutParams.WRAP_CONTENT, x - 20, y - 140);
                // 四参数分别为宽,高,X,Y座标,wrap_conent为根据内容自动调整
                // 后面-10,-40是我自己多次调试的结果,因为我发现如果不减,那个座标并不是在指头下,而是在指头的右下角
                // 暂时不知道什么原因
                mText.setLayoutParams(params);
                // 设置最终位置
     
            }
            break;
        case MotionEvent.ACTION_UP:
            
            mText.clearAnimation();
            mflag=false;
            break;
        }
        
        
        /*if (event.getAction() == MotionEvent.ACTION_UP) {
            // 手指离开屏幕时,把flag设为false
            mflag = false;
        }
        */
        return super.onTouchEvent(event);
    }




    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

 

新建资源anim文件夹

multi.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    
    <rotate 
        android:fromDegrees="0"
        android:toDegrees="360"
        android:repeatCount="10000"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="5000"/>

</set>

 

主布局文件:

<AbsoluteLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textview"
        android:text="@string/hello_world" />

</AbsoluteLayout>

 

 

运行后,手指点住屏幕移动,看看效果.

 

demo : http://pan.baidu.com/s/1eQiYBPC

 

posted @ 2015-01-07 18:01  MMLoveMeMM  阅读(207)  评论(0)    收藏  举报