实现QQ iPad客户端的对话框平滑移动的效果

今天要写一个Android对话框,实现对话框随着手指能够移动,而且当对话框移动到一定程度的时候,能够实现对话框在左右自动隐藏。这个功能比较难,需要系统的学习一下onTouch函数,现在贴出我的源代码:

package com.yelbosh.test;

import Java.io.IOException;

import com.yelbosh.test.classes.MyButton;

import android.Manifest;
import android.opengl.Visibility;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.text.Layout;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.support.v4.app.NavUtils;
import android.webkit.*;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;


/**
 *
 * @author yelbosh
 * 2012-7-15
 */
public class MainActivity extends Activity {
private static final boolean LEFT = true;
private static final boolean RIGHT = false;

private View talkdiv;
private Button photo;
private RelativeLayout rl;
//talkdiv上面的title
private TextView titleBar;
//设置talkidv出现的方向
private boolean talkOrentation = LEFT;

private int WINDOW_HEIGHT;
private int WINDOW_WIDTH;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rl = (RelativeLayout)findViewById(R.id.mainlayout);
        photo = (Button)findViewById(R.id.myphoto);
        talkdiv = getLayoutInflater().inflate(R.layout.talk, rl,false);
        rl.addView(talkdiv);
        talkdiv.setVisibility(View.INVISIBLE);
       
        titleBar = (TextView)findViewById(R.id.titleBar);
       
        //为titleBar设置监听函数
        titleBar.setOnTouchListener(new View.OnTouchListener() {
//记录初始位置
        int temp;
        int viewLeft;
       
public boolean onTouch(View arg0, MotionEvent event) {
// TODO Auto-generated method stub
int eventaction = event.getAction();
int x = (int)event.getRawX();


switch (eventaction) {
case MotionEvent.ACTION_DOWN:
temp = (int) event.getRawX();//记录起始x坐标
viewLeft = (int)MainActivity.this.talkdiv.getLeft();
break;
case MotionEvent.ACTION_MOVE:
MainActivity.this.talkdiv.layout(viewLeft+x-temp, 0, viewLeft+x-temp+MainActivity.this.talkdiv.getWidth(), MainActivity.this.talkdiv.getHeight());
MainActivity.this.talkdiv.postInvalidate();
break;
case MotionEvent.ACTION_UP:
//根据滑动的距离进行判断
//滑到右边
if((viewLeft + x-temp) > 0.3*MainActivity.this.WINDOW_WIDTH){

//滑向右边
Animation temani = new TranslateAnimation(MainActivity.this.talkdiv.getLeft(),MainActivity.this.talkdiv.getWidth(),0,0);
temani.setDuration(600);
MainActivity.this.talkdiv.startAnimation(temani);
MainActivity.this.talkdiv.setVisibility(View.INVISIBLE);
//使其居中
MainActivity.this.talkdiv.layout(0, 0,MainActivity.this.talkdiv.getWidth(), MainActivity.this.talkdiv.getHeight());
//设置下次从右边出现
MainActivity.this.talkOrentation = MainActivity.this.RIGHT;
}else if((viewLeft + x-temp) < -0.3*MainActivity.this.WINDOW_WIDTH){
//滑向左边
Animation temani = new TranslateAnimation(viewLeft + x-temp,-MainActivity.this.talkdiv.getWidth(),0,0);
temani.setDuration(600);
MainActivity.this.talkdiv.startAnimation(temani);
MainActivity.this.talkdiv.setVisibility(View.INVISIBLE);
MainActivity.this.talkdiv.layout(0, 0,MainActivity.this.talkdiv.getWidth(), MainActivity.this.talkdiv.getHeight());
//设置下次从左边出现
MainActivity.this.talkOrentation = MainActivity.this.LEFT;
}else{
//保持在(0,0)
Animation temani = new TranslateAnimation(viewLeft + x-temp,0,0,0);
temani.setDuration(600);
MainActivity.this.talkdiv.startAnimation(temani);
//使其居中
MainActivity.this.talkdiv.layout(0, 0,MainActivity.this.talkdiv.getWidth(), MainActivity.this.talkdiv.getHeight());
//重绘至中间
MainActivity.this.talkdiv.postInvalidate();
}
break;
}
return true;
}
});
       
       
        WINDOW_HEIGHT = this.getWindowManager().getDefaultDisplay().getHeight();
        WINDOW_WIDTH = this.getWindowManager().getDefaultDisplay().getHeight();
        /**
         * 为按钮设置监听函数
         */
        photo.setOnClickListener(new View.OnClickListener() {

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/pfsgd.html