您的位置:首页 > 其它

一个左右拖动的控件

2016-03-18 18:03 393 查看

请标注原文地址:http://write.blog.csdn.net/postedit

public class CustomView extends LinearLayout{

private static final String TAG = "CustomView";

private OverScroller mScroller;

private float init_x, end_x, start_x, final_x;

private ScaleAnimation animation, animation1;

private Context context;

private View layout_bitmap;

private LinearLayout.LayoutParams lp_big,lp_small;

private int width_big,height_big,width_small,height_small,distance,Top_distance;

private float width_scale_big = 1.0f,height_scale_big = 1.0f;

private float width_scale_small = 1.0f,height_scale_small = 1.0f;

private int slect_num = 0,first_num = 0,delay_multiple = 0,scroll_direct = 0;

private final int max_num = 7,animation_delaytime = 150;

private final int SCROLL_ONE = 1,SCROLL_TWO = 2,SCROLL_THREE = 3,

SCROLL_FOUR = 4,SCROLL_FIVE = 5,SCROLL_SIX = 6,CLICK_STATE = 10;

public static final String ACTION_CLICK_ONE = "com.example.deskscroll.clickone";

private boolean mScrolling = false;

public CustomView(Context context, AttributeSet attrs) {

super(context, attrs);

this.context = context;

getScrollwidth_height();

mScroller = new OverScroller(context);

animation = new ScaleAnimation(0.8f, 1.0f, 0.8f, 1.0f);//(width_scale_small,width_scale_big,height_scale_small,height_scale_big);//

animation1 = new ScaleAnimation(1.1f, 1.0f, 1.1f, 1.0f);//(width_scale_big,width_scale_small,height_scale_big,height_scale_small);//

animation.setDuration(300);

animation1.setDuration(100);

//animation.setFillAfter(true);

//animation1.setFillAfter(true);

OnTimer_trace();

Log.i(TAG, "slect_num = "+slect_num+" first_num = "+first_num + "click_num = "+MainActivity.click_num);

initView();

}

private void initView(){

Log.i(TAG, " MainActivity.first_temp = "+MainActivity.first_temp+"first_num = "+first_num);

if(first_num != MainActivity.first_temp){

if(MainActivity.first_temp - first_num > 0)

smoothScrollBy((width_small+10)*(MainActivity.first_temp - first_num), 0);

first_num = MainActivity.first_temp;

}

lp_big = new LinearLayout.LayoutParams(width_big, height_big);

lp_small = new LinearLayout.LayoutParams(width_small, height_small);

}

public void getScrollwidth_height(){

DisplayMetrics dm = new DisplayMetrics();

((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE))

.getDefaultDisplay().getMetrics(dm);

//设置窗口的大小

if(dm.widthPixels==1024){ //1024*600

width_big = 235;

height_big = 280;

width_small = 182;

height_small = 200;

distance = 10;

Top_distance = 12;

}

else{ //800*480

width_big = 180;

height_big = 280;

width_small = 140;

height_small = 200;

distance = 10;

Top_distance = 8;

}

Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.backcar_d);

int height = bitmap.getHeight();

int width= bitmap.getWidth();

width_scale_big = (float)width_big/width;

height_scale_big = (float)height_big/height;

width_scale_small = (float)width_small/width;

height_scale_small = (float)height_small/height;

//Log.i(TAG, "width_scale_big = "+width_scale_big+" width_scale_small = "+width_scale_small);

}

// 调用此方法滚动到目标位置

public void smoothScrollTo(int fx, int fy) {

int dx = fx - mScroller.getFinalX();

int dy = fy - mScroller.getFinalY();

smoothScrollBy(dx, dy);

}

// 调用此方法设置滚动的相对偏移

public void smoothScrollBy(int dx, int dy) {

// 设置mScroller的滚动偏移量

mScroller.startScroll(mScroller.getFinalX(), mScroller.getFinalY(), dx, dy);

invalidate();// 这里必须调用invalidate()才能保证computeScroll()会被调用,否则不一定会刷新界面,看不到滚动效果

}

@Override

public void computeScroll() {

// 先判断mScroller滚动是否完成

if (mScroller.computeScrollOffset()) {

//true 说明滚动尚未完成

// 这里调用View的scrollTo()完成实际的滚动

scrollTo(mScroller.getCurrX(), mScroller.getCurrY());

// 必须调用该方法,否则不一定能看到滚动效果

postInvalidate();

}

super.computeScroll();

}

@Override

public boolean onTouchEvent(MotionEvent event) {

// TODO Auto-generated method stub

switch (event.getAction()) {

case MotionEvent.ACTION_MOVE:

Log.i(TAG, "ACTION_MOVE");

end_x = event.getRawX();

if (start_x - end_x > 60) { // 左移

scroll_direct = 1;

delay_multiple++;

switch (delay_multiple) {

case 1:

myHandler.sendEmptyMessageDelayed(SCROLL_ONE, 0);

break;

case 2:

myHandler.sendEmptyMessageDelayed(SCROLL_TWO, animation_delaytime * 1);

break;

case 3:

myHandler.sendEmptyMessageDelayed(SCROLL_THREE, animation_delaytime * 2);

break;

case 4:

myHandler.sendEmptyMessageDelayed(SCROLL_FOUR, animation_delaytime * 3);

break;

case 5:

myHandler.sendEmptyMessageDelayed(SCROLL_FIVE, animation_delaytime * 4);

break;

default:

break;

}

} else if (end_x - start_x > 60) {

scroll_direct = 0;

delay_multiple++;

switch (delay_multiple) {

case 1:

myHandler.sendEmptyMessageDelayed(SCROLL_ONE, 0);

break;

case 2:

myHandler.sendEmptyMessageDelayed(SCROLL_TWO, animation_delaytime * 1);

break;

case 3:

myHandler.sendEmptyMessageDelayed(SCROLL_THREE, animation_delaytime * 2);

break;

case 4:

myHandler.sendEmptyMessageDelayed(SCROLL_FOUR, animation_delaytime * 3);

break;

case 5:

myHandler.sendEmptyMessageDelayed(SCROLL_FIVE, animation_delaytime * 4);

break;

default:

break;

}

}

if (Math.abs(end_x - start_x) > 60)

start_x = end_x;

break;

case MotionEvent.ACTION_DOWN:

Log.i(TAG, "ACTION_DOWN");

myHandler.removeMessages(SCROLL_ONE);

myHandler.removeMessages(SCROLL_TWO);

myHandler.removeMessages(SCROLL_THREE);

myHandler.removeMessages(SCROLL_FOUR);

myHandler.removeMessages(SCROLL_FIVE);

init_x = event.getRawX();

delay_multiple = 0;

break;

case MotionEvent.ACTION_UP:

Log.i(TAG, "ACTION_UP");

break;

default:

break;

}

return true;

}

@Override

public boolean onInterceptTouchEvent(MotionEvent ev) {

// TODO Auto-generated method stub

switch (ev.getAction()) {

case MotionEvent.ACTION_DOWN:

myHandler.removeMessages(SCROLL_ONE);

myHandler.removeMessages(SCROLL_TWO);

myHandler.removeMessages(SCROLL_THREE);

myHandler.removeMessages(SCROLL_FOUR);

myHandler.removeMessages(SCROLL_FIVE);

delay_multiple = 0;

Log.i("daxiang", "custom:onInterceptTouchEvent--down");

start_x = ev.getRawX();

mScrolling = false;

break;

case MotionEvent.ACTION_MOVE:

Log.i("daxiang", "custom:onInterceptTouchEvent--move");

if (Math.abs(start_x - ev.getRawX()) >= 0) {

mScrolling = true;

} else {

mScrolling = false;

}

break;

case MotionEvent.ACTION_UP:

Log.i("daxiang", "custom:onInterceptTouchEvent--up");

mScrolling = false;

break;

default:

break;

}

return mScrolling;

}

public void scroll_process(int right_left) {

Log.i("yxl", "slect_num = " + slect_num + " first_num = " + first_num);

if (right_left == 1) {// 左移

if (slect_num < max_num) {

if (slect_num - first_num < 4) {

slect_num++;

smoothScrollBy(0, 0);

} else {

slect_num++;

first_num++;

smoothScrollBy(width_small+distance, 0);

}

MainActivity.click_num = slect_num;

set_anim(getChildAt(slect_num), getChildAt(slect_num - 1));

getChildAt(slect_num).setLayoutParams(lp_big);

lp_big.setMargins(distance, Top_distance, 0, 0);

((TextView) getChildAt(slect_num)).setTextSize(25);
getChildAt(slect_num - 1).setLayoutParams(lp_small);

((TextView) getChildAt(slect_num - 1)).setTextSize(20);

lp_small.setMargins(distance, 0, 0, 0);

if (mOnInfoChange != null) {

mOnInfoChange.onAction(slect_num,first_num);

}

}

}

else{//右移

if(slect_num > 0){

if(slect_num - first_num > 0){

slect_num--;

smoothScrollBy(0, 0);

}

else{
slect_num--;

first_num--;

smoothScrollBy(-width_small-distance, 0);

}

MainActivity.click_num = slect_num;

set_anim(getChildAt(slect_num), getChildAt(slect_num + 1));

getChildAt(slect_num).setLayoutParams(lp_big);

lp_big.setMargins(distance, Top_distance, 0, 0);

((TextView) getChildAt(slect_num)).setTextSize(25);

getChildAt(slect_num + 1).setLayoutParams(lp_small);

lp_small.setMargins(distance, 0, 0, 0);

((TextView) getChildAt(slect_num + 1)).setTextSize(20);

if (mOnInfoChange != null) {

mOnInfoChange.onAction(slect_num,first_num);

}

}

}
}

public void set_anim(View view, View view2) {

view.startAnimation(animation);

view2.startAnimation(animation1);

}

public void click_process(int click_num){

Log.i(TAG, "click_num = "+click_num);

set_anim(getChildAt(click_num), getChildAt(slect_num));

getChildAt(click_num).setLayoutParams(lp_big);

lp_big.setMargins(distance, Top_distance, 0, 0);

((TextView)getChildAt(click_num)).setTextSize(25);

getChildAt(slect_num).setLayoutParams(lp_small);

lp_small.setMargins(distance, 0, 0, 0);

((TextView)getChildAt(slect_num)).setTextSize(20);

slect_num = click_num;
if (mOnInfoChange != null) {

mOnInfoChange.onAction(slect_num,first_num);

}

}

public void OnTimer_trace() {

Timer timer = new Timer();

timer.schedule(new TimerTask() {

public void run() {

myHandler.sendEmptyMessageDelayed(CLICK_STATE, 0);

}

}, 0, 100);

}

Handler myHandler = new Handler() {

public void handleMessage(Message msg) {

switch(msg.what){

case CLICK_STATE:
myHandler.removeMessages(CLICK_STATE);

if(slect_num != MainActivity.click_num)

click_process(MainActivity.click_num);

break;

case SCROLL_ONE:

case SCROLL_TWO:

case SCROLL_THREE:

case SCROLL_FOUR:

case SCROLL_FIVE:

scroll_process(scroll_direct);

break;

}

}

};

public int getSlectNum(){

return slect_num;

}

public int getFirstNum(){

return first_num;

}

public interface OnInfoChangeSlect{

public void onAction(int slect_num,int first_num);

}

private static OnInfoChangeSlect mOnInfoChange = null;

public static void setOnInfoChange(OnInfoChangeSlect mOnInfoChangeSlect){

mOnInfoChange = mOnInfoChangeSlect;

}

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: