package android.study.shift;
import android.study.shift.ItemView;
import android.study.shift.MainView;
import android.study.shift.Moveview;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.MeasureSpec;
public class Moveview extends ViewGroup{
private final static int TOUCH_STATE_REST = 0;
private final static int TOUCH_STATE_MOVING = 1;
private final static int MOVE_TO_LEFT = 1;
//private final static int MOVE_TO_RIGHT = 2;
private final static int MOVE_TO_REST = 0;
public final static int MAIN = 0;
public final static int LEFT = 1;
public final static int RIGHT = 2;
private int touch_state = TOUCH_STATE_REST;
private int move_state = MOVE_TO_REST;
private int now_state = MAIN;
private final float WIDTH_RATE = 0.18f;
private MainView main_show_view;
private ItemView left_show_view;
private int min_distance = 30;//此处只是初始化为30 ,其实不起作用
private int screen_w;
private int screen_h;
private int move_x_v;
private boolean isAimationMoving = false;
private Handler mHandler = new Handler() {
public void handleMessage(Message msg) {//处理消息
synchronized (Moveview.this) {//同步
isAimationMoving = true;
int move_change = (int) (screen_w * WIDTH_RATE / 5);
int left = main_show_view.getView().getLeft();
if (msg.what == 1) {//主屏在向右拖曳。。。
move(move_change + left);//参数为拖出来后主屏的坐标。
}
if (msg.what == 11) {
isAimationMoving = false;
moveToLeft(false); //满足可以显示的要求开始显示,false只是一个判定条件,以区别按钮起的效果
}
if (msg.what == 2) {//代表主屏向左在拖曳。。。
move(-1 * move_change + left);
}
if (msg.what == 12) {
isAimationMoving = false;
//moveToRight(false);
}
if (msg.what == 0) {
if (now_state == LEFT) {//如果现在已经显示左边
move(-1 * move_x_v);//那么还原成主屏
} else {
move(move_x_v);
}
}
if (msg.what == 10) {
isAimationMoving = false;
moveToMain(false);
}
}
}
};
public Moveview(Context context) {
super(context);
}
public Moveview(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public Moveview(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public void initView() {
if (main_show_view == null) {
main_show_view = new MainView(this.getContext(), this);
left_show_view = new ItemView(this.getContext());
//right_show_view = new ItemView(this.getContext(), "aaa");
}
this.addView(left_show_view.getView());
//this.addView(right_show_view.getView());
this.addView(main_show_view.getView());
}
public void initContent() {
}
public void move(int start) {//本函数显示拖动的时候屏幕的显示情况。
int left = main_show_view.getView().getLeft();//left代表主屏左边缘的位置坐标。
if (now_state == MAIN) {
if (left > 0) {//如果屏幕的左边被移向右,则left变成正数
if (move_state != MOVE_TO_LEFT) {
move_state = MOVE_TO_LEFT;
}
left_show_view.getView().setVisibility(View.VISIBLE);//设置左边可见
//right_show_view.getView().setVisibility(View.GONE);
} //else if (left < 0) {//如果屏幕的左边被移向左,则left变成负数
//if (move_state != MOVE_TO_RIGHT) {
// move_state = MOVE_TO_RIGHT;
//}
//right_show_view.getView().setVisibility(View.VISIBLE);
//left_show_view.getView().setVisibility(View.GONE);
//}
else {
move_state = MOVE_TO_REST;
}
main_show_view.getView().layout(start, 0, start + screen_w,
screen_h);//参数依次为左、上、右、下。
} else {
left = (int) (screen_w * WIDTH_RATE);//left为左边应该留出的控件空间,或者主屏左边应该在的位置
if (now_state == RIGHT) {
left = -1 * left;//若状态是在右边空出来,则主屏的左边为负数
}
left = left + start;
main_show_view.getView().layout(left, 0, left + screen_w, screen_h);
}
}
@Override
protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
if (move_state == MOVE_TO_REST) {
if (now_state == MAIN) {
int w = (int) (screen_w * WIDTH_RATE);//留下预定的宽度,显示各个view的参数设置。
main_show_view.getView().layout(0, 0, screen_w, screen_h);
left_show_view.getView().layout(0, 0, w, screen_h);
//right_show_view.getView().layout(screen_w - w, 0, screen_w,
//screen_h);
} else if (now_state == LEFT) {
moveToLeft(false);
} else {
//moveToRight(false);
}
}
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// Log.d("msg", "" + widthMeasureSpec);
main_show_view.getView().measure(widthMeasureSpec, heightMeasureSpec);
left_show_view.getView().measure(MeasureSpec.UNSPECIFIED,//unspecified为未指定的
heightMeasureSpec);
//right_show_view.getView().measure(MeasureSpec.UNSPECIFIED,
//heightMeasureSpec);
left_show_view.setWidth((int) (screen_w * WIDTH_RATE));//设置view宽度
//right_show_view.setWidth((int) (screen_w * WIDTH_RATE));
super.onMeasure(widthMeasureSpec, heightMeasureSpec);//调用父类的构造函数
}
private int start_x;
private int start_y;
private boolean isMoved;
public boolean dispatchTouchEvent(MotionEvent ev) {//分发触摸消息事件
if (isAimationMoving) {
return super.dispatchTouchEvent(ev);
} else {
int action = ev.getAction();
float x = ev.getX();
float y = ev.getY();
switch (action) {
case MotionEvent.ACTION_DOWN:
super.dispatchTouchEvent(ev);
start_y = (int) y;
move_x_v = 0;
if (this.touch_state == TOUCH_STATE_REST) {
this.touch_state = TOUCH_STATE_MOVING;
start_x = (int) x;
isMoved = false;
move_state = MOVE_TO_REST;
}
break;
case MotionEvent.ACTION_MOVE:
int last_y = (int) y;
int last_x = (int) x;
super.dispatchTouchEvent(ev);
if (!isMoved) {
if (Math.abs(last_y - start_y) > Math.abs(last_x - start_x)) {//如果Y上面移动的距离大于X上面移动的距离。
super.onTouchEvent(ev);
return true;
} else {//X移动距离大于Y
if (Math.abs(last_x - start_x) > 5) {//X移动距离大于5就被认为移动了
isMoved = true;
}
}
}
if (isMoved) {
if (this.touch_state == TOUCH_STATE_MOVING) {
if (Math.abs(last_x - start_x) > 10) {//如果移动的X距离大于10
int left = main_show_view.getView().getLeft();
Log.d("msg", "left:" + left);
Log.d("msg", "x:" + last_x);
isMoved = true;
int move_x = last_x - start_x;
if (move_x > 0 && now_state == LEFT) {//方向正确的移动才算是移动的状态
isMoved = false;
break;
}
if (move_x < 0 && now_state == RIGHT) {//如果现在在显示右边,那么再向右移就不反应。
isMoved = false;
break;
}
if (move_x < 0 && now_state ==MAIN){//如果现在在主屏,向右移动也是无效的
isMoved = false;
break;
}
if (move_x > 234 && now_state ==MAIN){
isMoved = true;//设置为true那么移动还是发生了,break之后还是会在ACTIONUP中起作用
break;//break之后不会执行下面的move()语句,
}
if (move_x < -234 && now_state ==LEFT){
isMoved = true;
break;
}
move(move_x);//以移动的距离为参数调用move()实现移动的动态显示
}
}
return false;
}
break;
case MotionEvent.ACTION_UP://
if (this.touch_state == TOUCH_STATE_MOVING) {
if (isMoved) {
last_x = (int) x;
if (Math.abs(last_x - start_x) > min_distance) {//
if (now_state == MAIN) {
if (move_
没有合适的资源?快使用搜索试试~ 我知道了~
android侧边栏的滑动,以及实现启动另外的Activity与更换布局
共65个文件
png:22个
class:20个
java:8个
4星 · 超过85%的资源 需积分: 14 531 下载量 136 浏览量
2012-08-21
15:05:25
上传
评论 1
收藏 480KB ZIP 举报
温馨提示
根据别人的demo加以改进与完善,实现了一些其他的功能的同时方便运用到程序中实现特设效果,(自己写了一些注释,但是很不规范,将就着看吧。)
资源推荐
资源详情
资源评论
收起资源包目录
shift.zip (65个子文件)
shift
.project 841B
project.properties 361B
proguard.cfg 1KB
src
android
study
shift
MainView.java 2KB
ItemView.java 2KB
SearchView.java 2KB
Moveview.java 12KB
SearchMoveview.java 10KB
ShiftActivity.java 882B
SearchActivity.java 770B
AndroidManifest.xml 1KB
res
drawable-ldpi
ic_launcher.png 2KB
drawable-hdpi
png_icon_466.png 1KB
ic_launcher.png 4KB
png_icon_019.png 2KB
png_icon_165.png 1KB
mb.jpg 89KB
png_icon_020.png 786B
png_icon_086.png 3KB
bq_bz.jpg 23KB
png_icon_029.png 2KB
png_icon_156.png 2KB
png_icon_014.png 2KB
drawable-mdpi
ic_launcher.png 3KB
layout
layout_change.xml 504B
search_layout.xml 489B
item_layout.xml 2KB
main.xml 2KB
values
strings.xml 413B
assets
gen
android
study
shift
R.java 2KB
.classpath 364B
bin
resources.ap_ 137KB
shift.apk 147KB
classes
android
study
shift
R$layout.class 506B
MainView.class 2KB
ShiftActivity.class 1KB
R$drawable.class 753B
SearchView$3.class 956B
R$attr.class 343B
SearchView$2.class 956B
Moveview.class 6KB
SearchMoveview.class 6KB
SearchView.class 2KB
SearchMoveview$1.class 2KB
R$id.class 822B
SearchView$1.class 932B
ItemView$1.class 1KB
R.class 538B
R$string.class 607B
ItemView.class 2KB
Moveview$1.class 2KB
MainView$MainAdapter.class 1KB
SearchActivity.class 1KB
res
drawable-ldpi
ic_launcher.png 2KB
drawable-hdpi
png_icon_466.png 788B
ic_launcher.png 4KB
png_icon_019.png 929B
png_icon_165.png 1KB
png_icon_020.png 397B
png_icon_086.png 2KB
png_icon_029.png 2KB
png_icon_156.png 1KB
png_icon_014.png 1KB
drawable-mdpi
ic_launcher.png 2KB
classes.dex 18KB
共 65 条
- 1
fanfan913
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页