当前位置:  编程技术>移动开发
本页文章导读:
    ▪探索图片填充形式二-CenterCrop模式的实现        探索图片填充模式二--CenterCrop模式的实现 CenterCrop模式解决的就是:     1. 图片要去两头,留中间      2. 图片要填充满控件   这个模式和fitCenter还有有很大的不同。借鉴上篇文章的思路,.........
    ▪ onFling onScroll各个手势成效实现        onFling onScroll各个手势效果实现 package com.lenovo.anyclock; import android.annotation.SuppressLint; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; impo.........
    ▪ java 面临 Socket 操作,TCP 和 UDP 示例       java 面向 Socket 操作,TCP 和 UDP 示例 TCP/IP 参考模型 应用层-->传输层-->网络层--->物理+数据链路层  .TCP 是一种提供可靠的、端到端的字节流通讯协议 。是一种面向连接的协议。TCP连.........

[1]探索图片填充形式二-CenterCrop模式的实现
    来源: 互联网  发布时间: 2014-02-18
探索图片填充模式二--CenterCrop模式的实现

CenterCrop模式解决的就是:

    1. 图片要去两头,留中间 

    2. 图片要填充满控件

  这个模式和fitCenter还有有很大的不同。借鉴上篇文章的思路,我们同样按照Image与ImageView的宽高比差值,分两种情况进行讨论。

  假设原始图片高h,宽w , Imageview的高y,宽x ,比较两者高宽比。裁剪出的图称为Image1:

  1、 当 y / x - h / w > 0 时

  说明Imageview的高宽比大于图片的高宽比,如图所示:

  左图实线标示的是图片Image的高和宽,右图是Imageview。 我们需要从Image的中间按照比例y/x裁剪出一幅图Image1来,如图中虚线所示。

  显而易见,Image1的宽度是w1,高度是h 。

  Image1的高宽比必须和Imageview的高宽比相同,即 y / x = h / w1 , 计算出 w1 = (h * x )/ y

  在FitCenter模式中,我们需要计算Image在画布上的绘制点。在CenterCrop模式中,都是裁剪操作,我们只需要计算在Image的什么地方进行裁剪。从图上可以看出,裁剪点  P的坐标是((w - w1) / 2, 0)。

 2、当 y / x - h / w <= 0时

  说明Imageview的高宽比小于图片的高宽比,如图所示:

  显而易见,Image1高度为h1, 宽度为w ,依据 y / x = h1 / w , 计算出 h1 = (w * y) / x

  裁剪点P 的坐标是 (0, (h - h1) / 2)

  好了,算法我们分析出来了。在Android中绘制图片的某一个部分使用到了函数 Bitmap.createBitmap(Bitmap source, int x, int y, int width, int height)

  source是原始图片,x指的是绘制的横坐标,y指的是绘制的纵坐标。width,height分别代表宽高。

   下面给出核心代码实现:

/**
 * 以CenterCrop方式resize图片
 * @param src  原始图片
 * @param destWidth  目标图片宽度
 * @param destHeight 目标图片高度
 * @return
 */
public Bitmap resizeBitmapByCenterCrop(Bitmap src, int destWidth, int destHeight) {
    if (src == null || destWidth == 0 || destHeight == 0) {
        return null;
    }
    // 图片宽度
    int w = src.getWidth();
    // 图片高度
    int h = src.getHeight();
    // Imageview宽度
    int x = destWidth;
    // Imageview高度
    int y = destHeight;
    // 高宽比之差
    int temp = (y / x) - (h / w);
    /**
     * 判断高宽比例,如果目标高宽比例大于原图,则原图高度不变,宽度为(w1 = (h * x) / y)拉伸
     * 画布宽高(w1,h),在原图的((w - w1) / 2, 0)位置进行切割
     */
 
    if (temp > 0) {
        // 计算画布宽度
        int w1 = (h * x) / y;
        // 创建一个指定高宽的图片
        Bitmap newb = Bitmap.createBitmap(src, (w - w1) / 2, 0, w1, h);
        //原图回收
        src.recycle();
        return newb;
    } else {
        /**
         * 如果目标高宽比小于原图,则原图宽度不变,高度为(h1 = (y * w) / x),
         * 画布宽高(w, h1), 原图切割点(0, (h - h1) / 2)
         */
 
        // 计算画布高度
        int h1 = (y * w) / x;
        // 创建一个指定高宽的图片
        Bitmap newb = Bitmap.createBitmap(src, 0, (h - h1) / 2, w, h1);
        //原图回收
        src.recycle();
        return newb;
    }
}

  


  效果图:

  附:Demo下载

 

  原文链接:http://www.67tgb.com/?p=578

  转载注明:望月听涛

2楼StubbornPotatoes昨天 20:59顶顶Re: wangyuetingtao10小时前回复StubbornPotatoesn哈哈,谢谢1楼lfmilaoshi昨天 18:59开源呀

    
[2] onFling onScroll各个手势成效实现
    来源: 互联网  发布时间: 2014-02-18
onFling onScroll各个手势效果实现
package com.lenovo.anyclock;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Display;
import android.view.GestureDetector;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.TabHost;

import com.lenovo.anyclock.alarm.Alarm;
import com.lenovo.anyclock.leftview.LeftListView;
import com.lenovo.anyclock.setup.Config;
import com.lenovo.anyclock.setup.Setup;
import com.lenovo.anyclock.sleep.Sleep;
import com.lenovo.anyclock.utils.Constants;
import com.lenovo.anyclock.utils.Util;
import com.umeng.analytics.MobclickAgent;

@SuppressLint("NewApi")
public class AnyClockActivity extends Activity implements OnClickListener, OnTouchListener {
    private static final String TAG = "AnyClockActivity";
    public static MainSurfaceView mMainView;
    private static View mListView;
    private static View mSetupView;
    private static View mSleepView;
    private LeftListView mLeftListView;
    private static Setup mSetup;
    private static Sleep mSleep;

    private GestureDetector mGestureDetector;
    private long mKeyReturnTime;

    private int mSDKVersion = 0;

    private final BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
            if (Intent.ACTION_SCREEN_OFF.equals(action)) {
                if (Constants.STATE == Constants.VIEW_ALARM) {
                    if (Alarm.AlreadySnooze >= Alarm.SNOOZE_TIMES) {
                        Log.d(TAG, "snooze count is max, so return");
                        Alarm.snooze(context, 1);
                    } else {
                        Alarm.AlreadySnooze++;
                        Log.d(TAG, "snooze, times=" + Alarm.AlreadySnooze);
                        Alarm.snooze(context, Alarm.SNOOZE_TIME);
                    }
                }
            }
        }
    };

    @SuppressWarnings("deprecation")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
        registerReceiver(mBatInfoReceiver, filter);

        MobclickAgent.onError(this);

        WindowManager windowManager = getWindowManager();
        Display display = windowManager.getDefaultDisplay();

        Constants.SCREEN_WIDTH = display.getWidth();
        Constants.SCREEN_HEIGTH = display.getHeight();
        Constants.WIDGET_RATIO = (float)(Constants.SCREEN_WIDTH * 1.0) / Constants.SCREEN_REF_WIDTH;
        Constants.SCREEN_WIDTH_RADIO = (float)(Constants.SCREEN_WIDTH * 1.0) / Constants.SCREEN_REF_WIDTH;
        Constants.SCREEN_HEIGTH_RADIO = (float)(Constants.SCREEN_HEIGTH * 1.0) / Constants.SCREEN_REF_HEIGTH;

        setContentView(R.layout.main);
        mMainView = (MainSurfaceView)findViewById(R.id.main_view);
        mMainView.init(this, Constants.SCREEN_WIDTH, Constants.SCREEN_HEIGTH);
        Constants.STATE = Constants.VIEW_MAIN;
        // 初始化闹钟的启动参数
        Config mConfig = new Config();
        mConfig.init(this);

        findViewById(R.id.touch_mask).setOnTouchListener(this);
        mGestureDetector = new GestureDetector(new MyGestureListener(this));
        if (getIntent().getAction().equalsIgnoreCase(Alarm.ALARM_ALERT_ACTION)) {
            Log.d(TAG, "ChangeToAlarmGroup()");
            mMainView.changeToAlarmGroup();
        }

        mSDKVersion = Util.getAndoirdSDKVersion();
    }

    @Override
    public void onDestroy() {
        if (mBatInfoReceiver != null) {
            try {
                unregisterReceiver(mBatInfoReceiver);
            } catch (Exception e) {
                Log.d(TAG, "unregisterReceiver mBatInfoReceiver failure :" + e.getCause());
            }
        }
        super.onDestroy();
    }

    @Override
    protected void onPause() {
        mMainView.saveEnvirment();
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mMainView.loadEnvirment();
        MobclickAgent.onResume(this);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Log.d(TAG, "onKeyDown:" + keyCode);
        boolean ret = true;
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (Constants.STATE == Constants.VIEW_MAIN) {
                if ((System.currentTimeMillis() - mKeyReturnTime) < 500)  // 500ms以内的按键认为是重复按按键,被抛弃
                    ret = false;
                else
                    ret = super.onKeyDown(keyCode, event);
            } else if (Constants.STATE == Constants.VIEW_SETUP ||
                    Constants.STATE == Constants.VIEW_ACHIVEMENT ||
                    Constants.STATE == Constants.VIEW_SLEEP) {
                changeToMainGroup();
            } else {
                mMainView.changeToMainGroup();
            }
            mKeyReturnTime = System.currentTimeMillis();
            Constants.STATE = Constants.VIEW_MAIN;
            return ret;
        } else
            return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if ((isUpdateViewRunning || mScrollDirection != SCROLL_DIREC.SCROLL_IDLE)) {

            if (((mScrollDirection == SCROLL_DIREC.SCROLL_DOWN || mScrollDirection == SCROLL_DIREC.SCROLL_UP)
            && Constants.STATE == Constants.VIEW_ACHIVEMENT)) {
                if (!dispatchTouchEvent(v, event)) {
                    mGestureDetector.onTouchEvent(event);
                    onTouchEvent(event);
                }
                return true;
            }

            mGestureDetector.onTouchEvent(event);
            onTouchEvent(event);
            return true;
        }

        if (!dispatchTouchEvent(v, event)) {
            mGestureDetector.onTouchEvent(event);
            onTouchEvent(event);
        }

        return true;
    }

    private boolean dispatchTouchEvent(View v, MotionEvent event) {
        if (Constants.STATE == Constants.VIEW_ACHIVEMENT) {
            return mLeftListView.processTouchEvent(event);
        } else if (Constants.STATE == Constants.VIEW_SETUP) {
            return mSetup.processTouchEvent(event);
        } else if (Constants.STATE == Constants.VIEW_SLEEP) {
            return mSleep.processTouchEvent(event);
        } else {
            return mMainView.processTouchEvent(event);
        }
    }

    @Override
    public void onClick(View view) {}

    // -1, not judged; 1 direction x; 2 direction y
    private SCROLL_DIREC mScrollDirection = SCROLL_DIREC.SCROLL_IDLE;;
    private static final int MIN_DISTANCE = 10;
    private int PIXEL = 60;
    Handler mHandler = new Handler();
    private int TIME_UNIT = 1; //ms

    public enum SCROLL_DIREC {
        SCROLL_IDLE,
        SCROLL_LEFT,
        SCROLL_RIGHT,
        SCROLL_UP,
        SCROLL_DOWN,
        SCROLL_HORISON,
        SCROLL_VERTICAL
    }

    boolean isUpdateViewRunning = false;

    @SuppressLint("NewApi")
    private void animListView(int x) {
        int posX = (int)mListView.getX();
        int width = mListView.getWidth();

        Log.v(TAG, "updateView running, animListView, posX : " + posX);

        if (posX >= x) {
            if (posX == 0) {
                isUpdateViewRunning = false;
                mScrollDirection = SCROLL_DIREC.SCROLL_IDLE;
                Constants.STATE = Constants.VIEW_ACHIVEMENT;
                return;
            }
            posX += PIXEL;
            if (posX > 0)
                posX = 0;
            mListView.setX(posX);
            mHandler.postDelayed(updateView, TIME_UNIT);
        } else if (posX < x) {
            if (posX == -width) {
                isUpdateViewRunning = false;
                mScrollDirection = SCROLL_DIREC.SCROLL_IDLE;
                Constants.STATE = Constants.VIEW_MAIN;
                mListView.setVisibility(View.INVISIBLE);
            }
            posX -= PIXEL;
            if (posX < -width)
                posX = -width;
            mListView.setX(posX);
            mHandler.postDelayed(updateView, TIME_UNIT);
        }
    }

    @SuppressLint("NewApi")
    private void animSleepView(int y) {
        int posY = (int)mSleepView.getY();
        int height = mSleepView.getHeight();

        Log.v(TAG, "updateView running, animSleepView, posY : " + posY);

        if (posY >= y) {
            if (posY == 0) {
                isUpdateViewRunning = false;
                mScrollDirection = SCROLL_DIREC.SCROLL_IDLE;
                Constants.STATE = Constants.VIEW_SLEEP;
                return;
            }
            posY += PIXEL;
            if (posY > 0)
                posY = 0;
            mSleepView.setY(posY);
            mHandler.postDelayed(updateView, TIME_UNIT);
        } else if (posY < y) {
            if (posY == -height) {
                isUpdateViewRunning = false;
                mScrollDirection = SCROLL_DIREC.SCROLL_IDLE;
                Constants.STATE = Constants.VIEW_MAIN;
                mSleepView.setVisibility(View.INVISIBLE);
                mSleep.reset();
                mSleep = null;
                mSleepView = null;
                Constants.STATE = Constants.VIEW_MAIN;
                return;
            }
            posY -= PIXEL;
            if (posY < -height)
                posY = -height;
            mSleepView.setY(posY);
            mHandler.postDelayed(updateView, TIME_UNIT);
        }
    }

    @SuppressLint("NewApi")
    private void animSetupView(int x) {
        int posX = (int)mSetupView.getX();
        int width = mSetupView.getWidth();

        Log.v(TAG, "updateView running, animSetupView, posX : " + posX);

        if (posX < x) {
            if (posX == 0) {
                isUpdateViewRunning = false;
                mScrollDirection = SCROLL_DIREC.SCROLL_IDLE;
                Constants.STATE = Constants.VIEW_SETUP;
                return;
            }
            posX -= PIXEL;
            if (posX < 0)
                posX = 0;
            mSetupView.setX(posX);
            mHandler.postDelayed(updateView, TIME_UNIT);
        } else if (posX >= x) {
            if (posX == width) {
                isUpdateViewRunning = false;
                mScrollDirection = SCROLL_DIREC.SCROLL_IDLE;
                Constants.STATE = Constants.VIEW_MAIN;
                mSetupView.setVisibility(View.INVISIBLE);
                mSetup.saveConfig();
                mSetup = null;
                mSetupView = null;
                return;
            }
            posX += PIXEL;
            if (posX > width)
                posX = width;
            mSetupView.setX(posX);
            mHandler.postDelayed(updateView, TIME_UNIT);
        }
    }

    Runnable updateView = new Runnable() {
        public void run() {
            if ((mScrollDirection == SCROLL_DIREC.SCROLL_RIGHT && Constants.STATE == Constants.VIEW_MAIN)) {
                animListView(-mListView.getWidth() + 50);
            } else if ((mScrollDirection == SCROLL_DIREC.SCROLL_LEFT && Constants.STATE == Constants.VIEW_ACHIVEMENT)) {
                animListView(-50);
            } else if ((mScrollDirection == SCROLL_DIREC.SCROLL_LEFT && Constants.STATE == Constants.VIEW_MAIN)) {
                animSetupView(mSetupView.getWidth() - 50);
            } else if ((mScrollDirection == SCROLL_DIREC.SCROLL_RIGHT && Constants.STATE == Constants.VIEW_SETUP)) {
                animSetupView(50);
            } else if ((mScrollDirection == SCROLL_DIREC.SCROLL_DOWN && Constants.STATE == Constants.VIEW_MAIN)) {
                animSleepView(-mSleepView.getHeight() + 50);
            } else if ((mScrollDirection == SCROLL_DIREC.SCROLL_UP && Constants.STATE == Constants.VIEW_SLEEP)) {
                animSleepView(-50);
            }

            // This is important to reset parameters of scroll event
            else {
                isUpdateViewRunning = false;
                mScrollDirection = SCROLL_DIREC.SCROLL_IDLE;
            }
        }
    };

    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                break;

            case MotionEvent.ACTION_UP:
                if (mScrollDirection != SCROLL_DIREC.SCROLL_IDLE) {
                    Log.v(TAG, "onTouchEvent, ACTION_UP");
                    isUpdateViewRunning = true;
                    mHandler.postDelayed(updateView, TIME_UNIT);
                }

                // if (((mScrollDirection == SCROLL_DIREC.SCROLL_DOWN || mScrollDirection == SCROLL_DIREC.SCROLL_UP)
                // && Constants.STATE == Constants.VIEW_ACHIVEMENT)) {
                // mScrollDirection = SCROLL_DIREC.SCROLL_IDLE;
                // }
                //
                break;

            case MotionEvent.ACTION_MOVE:
                break;

            case MotionEvent.ACTION_CANCEL:
                break;
        }
        return false;
    }

    public static void changeToMainGroup() {
        if (Constants.STATE == Constants.VIEW_ACHIVEMENT) {
            Util.slideOut(mListView, Util.DIRECTION_LEFT);
        } else if (Constants.STATE == Constants.VIEW_SETUP) {
            Util.slideOut(mSetupView, Util.DIRECTION_RIGHT);
            mSetup.saveConfig();
            mSetup = null;
            mSetupView = null;
        } else if (Constants.STATE == Constants.VIEW_SLEEP) {
            Util.slideOut(mSleepView, Util.DIRECTION_UP);
            mSleep.reset();
            mSleep = null;
            mSleepView = null;
        }
        Constants.STATE = Constants.VIEW_MAIN;
    }

    private class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
        private Context mContext;

        public MyGestureListener(Context context) {
            mContext = context;
        }

        @Override
        public boolean onDown(MotionEvent arg0) {
            return true;
        }

        private static final int MIN_VERTICAL = 120;
        private static final int MIN_VELOCITY = 0;

        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            if (mSDKVersion < 0)
                return true;

            if (Constants.STATE == Constants.VIEW_MAIN) {
                if (e1.getX() - e2.getX() > MIN_VERTICAL && Math.abs(velocityX) > MIN_VELOCITY) {
                    if (mSetupView == null) {
                        mSetupView = findViewById(R.id.setup_panel);
                        mSetupView.setVisibility(View.INVISIBLE);
                        mSetup = new Setup(mContext, mSetupView);
                    }
                    Util.slideIn(mSetupView, Util.DIRECTION_RIGHT);
                    Constants.STATE = Constants.VIEW_SETUP;
                } else if (e2.getX() - e1.getX() > MIN_VERTICAL && Math.abs(velocityX) > MIN_VELOCITY) {
                    if (mListView == null) {
                        mListView = findViewById(R.id.tabhost);
                        mListView.setVisibility(View.INVISIBLE);
                        mLeftListView = new LeftListView(mContext, (TabHost)mListView);
                        mLeftListView.initPreference();
                    }
                    Util.slideIn(mListView, Util.DIRECTION_LEFT);
                    Constants.STATE = Constants.VIEW_ACHIVEMENT;
                } else if (e2.getY() - e1.getY() > MIN_VERTICAL && Math.abs(velocityY) > MIN_VELOCITY) {
                    if (mSleepView == null) {
                        mSleepView = findViewById(R.id.sleep_panel);
                        mSleepView.setVisibility(View.INVISIBLE);
                        mSleep = new Sleep(mContext, mSleepView);
                    }
                    Util.slideIn(mSleepView, Util.DIRECTION_UP);
                    Constants.STATE = Constants.VIEW_SLEEP;
                }
            } else if (Constants.STATE == Constants.VIEW_ACHIVEMENT) {
                if (e1.getX() - e2.getX() > MIN_VERTICAL && Math.abs(velocityX) > MIN_VELOCITY) {
                    changeToMainGroup();
                }
            } else if (Constants.STATE == Constants.VIEW_SETUP) {
                if (e2.getX() - e1.getX() > MIN_VERTICAL && Math.abs(velocityX) > MIN_VELOCITY) {
                    changeToMainGroup();
                }
            } else if (Constants.STATE == Constants.VIEW_SLEEP) {
                if (e1.getY() - e2.getY() > MIN_VERTICAL && Math.abs(velocityY) > MIN_VELOCITY) {
                    changeToMainGroup();
                }
            }
            return true;
        }

        @Override
        public void onLongPress(MotionEvent arg0) {
            // TODO Auto-generated method stub
            Log.v(TAG, "onLongPress 2");

        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float arg2, float arg3) {
            // method view.setX() not found in lower SDK version, use fling
            if (mSDKVersion > 0)
                return true;

            if (isUpdateViewRunning)
                return true;

            // Judge direction
            if (mScrollDirection == SCROLL_DIREC.SCROLL_IDLE) {

                if (e2.getX() - e1.getX() > MIN_DISTANCE) {
                    mScrollDirection = SCROLL_DIREC.SCROLL_RIGHT;
                } else if (e1.getX() - e2.getX() > MIN_DISTANCE) {
                    mScrollDirection = SCROLL_DIREC.SCROLL_LEFT;
                } else if (e2.getY() - e1.getY() > MIN_DISTANCE) {
                    mScrollDirection = SCROLL_DIREC.SCROLL_DOWN;
                } else if (e1.getY() - e2.getY() > MIN_DISTANCE) {
                    mScrollDirection = SCROLL_DIREC.SCROLL_UP;
                }
            }

            // Make sure scroll direction is judged
            if (mScrollDirection == SCROLL_DIREC.SCROLL_IDLE)
                return true;

            // Calculate scroll value
            float scrollX = e2.getX() - e1.getX();
            float scrollY = e2.getY() - e1.getY();

            // Scroll value must match the direction
            if (mScrollDirection == SCROLL_DIREC.SCROLL_RIGHT) {
                scrollX = Util.caliper(0, Constants.SCREEN_WIDTH, scrollX);
                scrollY = 0;
            } else if (mScrollDirection == SCROLL_DIREC.SCROLL_LEFT) {
                scrollX = Util.caliper(-Constants.SCREEN_WIDTH, 0, scrollX);
                scrollY = 0;
            } else if (mScrollDirection == SCROLL_DIREC.SCROLL_DOWN) {
                scrollY = Util.caliper(0, Constants.SCREEN_HEIGTH, scrollY);
                scrollX = 0;
            } else if (mScrollDirection == SCROLL_DIREC.SCROLL_UP) {
                scrollY = Util.caliper(-Constants.SCREEN_HEIGTH, 0, scrollY);
                scrollX = 0;
            }

            // Log.v(TAG, "mScrollDirection : " + mScrollDirection);
            // Log.v(TAG, "scrollX : " + scrollX + ", scrollY : " + scrollY);

            // Scroll view
            if (Constants.STATE == Constants.VIEW_MAIN) {
                if (mScrollDirection == SCROLL_DIREC.SCROLL_RIGHT) {
                    if (mListView == null) {
                        mListView = findViewById(R.id.tabhost);
                        mListView.setVisibility(View.INVISIBLE);
                        mLeftListView = new LeftListView(mContext, (TabHost)mListView);
                        mLeftListView.initPreference();
                    }
                    mListView.setVisibility(View.VISIBLE);
                    mListView.setX(-mListView.getWidth() + scrollX);
                } else if (mScrollDirection == SCROLL_DIREC.SCROLL_LEFT) {
                    if (mSetupView == null) {
                        mSetupView = findViewById(R.id.setup_panel);
                        mSetupView.setVisibility(View.INVISIBLE);
                        mSetup = new Setup(mContext, mSetupView);
                    }
                    mSetupView.setVisibility(View.VISIBLE);
                    mSetupView.setX(mSetupView.getWidth() + (int)scrollX);
                } else if (mScrollDirection == SCROLL_DIREC.SCROLL_DOWN) {
                    if (mSleepView == null) {
                        mSleepView = findViewById(R.id.sleep_panel);
                        mSleepView.setVisibility(View.INVISIBLE);
                        mSleep = new Sleep(mContext, mSleepView);
                    }
                    mSleepView.setVisibility(View.VISIBLE);
                    mSleepView.setY(-mSleepView.getHeight() + scrollY);
                }
            } else if (Constants.STATE == Constants.VIEW_ACHIVEMENT) {
                if (mScrollDirection == SCROLL_DIREC.SCROLL_LEFT)
                    mListView.setX(scrollX);
            } else if (Constants.STATE == Constants.VIEW_SETUP) {
                if (mScrollDirection == SCROLL_DIREC.SCROLL_RIGHT)
                    mSetupView.setX(scrollX);
            } else if (Constants.STATE == Constants.VIEW_SLEEP) {
                if (mScrollDirection == SCROLL_DIREC.SCROLL_UP)
                    mSleepView.setY(scrollY);
            }

            return true;
        }

        @Override
        public void onShowPress(MotionEvent arg0) {
            // TODO Auto-generated method stub
            Log.v(TAG, "onShowPress 2");

        }

        @Override
        public boolean onSingleTapUp(MotionEvent arg0) {
            // TODO Auto-generated method stub
            Log.v(TAG, "onSingleTapUp 2");
            return true;
        }
    }
}

 


    
[3] java 面临 Socket 操作,TCP 和 UDP 示例
    来源: 互联网  发布时间: 2014-02-18
java 面向 Socket 操作,TCP 和 UDP 示例
TCP/IP 参考模型

应用层-->传输层-->网络层--->物理+数据链路层  .

TCP 是一种提供可靠的、端到端的字节流通讯协议 。是一种面向连接的协议。TCP连接是字节流而非报文流。

UDP向应用程序提供了一种发送封装的原始ip数据报的方法、并且发送时无需建立连接。是一种不可靠的连接。


网关  、 路由器 。Socket编程 。

双向的网络通信连接实现数据交换,这个双向链路的一端称为一个Socket。

java.net  包中的 Socket类 实现clinet端   ServerSocket类 实现TCP服务端

建立连接时所需要的寻址信息为远程计算机的IP地址和端口号 。

端口号 也分为 TCP端口  和 UDP端口  每一个都有 65536 个端口 。

accept(); 方法是阻塞式的  .

异步式的网络编程,接收数据 . 1.5只后.

1 TCP

import java.net.*;
import java.io.*;

public class TCPServer{

public static void main(String[] args) throws Exception{
ServerSocket ss=new ServerSocket(4567);
while(true){
Socket s=ss.accept();
DataInputStream dataIn=new DataInputStream(s.getInputStream());

System.out.println("client connection!");
System.out.println(dataIn.readUTF());
dataIn.close();
s.close();

}
}
}


import java.net.*;
import java.io.*;

public class TCPClient{

public static void main(String [] args) throws Exception{
Socket s=new Socket("127.0.0.1",4567);
OutputStream out = s.getOutputStream();
DataOutputStream dataOut=new DataOutputStream(out);
dataOut.writeUTF("hello server");
dataOut.flush();
dataOut.close();
s.close();
}
}



2 UDP

import java.net.*;
import java.io.*;

public class UDPServer{

public static void main(String[] args) throws Exception{

byte[] buf=new byte[1024];
DatagramPacket dp=new DatagramPacket(buf, buf.length);
DatagramSocket udp=new DatagramSocket(5678);
while(true){
udp.receive(dp);
//System.out.println(new String(buf,0,dp.getLength()));

//接受一个long类型的数据
ByteArrayInputStream byteIn=new ByteArrayInputStream(buf);
DataInputStream dataIn=new DataInputStream(byteIn);

System.out.println(dataIn.readLong());
}
}
}


import java.net.*;
import java.io.*;

public class UDPServer{

public static void main(String[] args) throws Exception{

byte[] buf=new byte[1024];
DatagramPacket dp=new DatagramPacket(buf, buf.length);
DatagramSocket udp=new DatagramSocket(5678);
while(true){
udp.receive(dp);
//System.out.println(new String(buf,0,dp.getLength()));

//接受一个long类型的数据
ByteArrayInputStream byteIn=new ByteArrayInputStream(buf);
DataInputStream dataIn=new DataInputStream(byteIn);

System.out.println(dataIn.readLong());
}
}
}



    
最新技术文章:
▪Android开发之登录验证实例教程
▪Android开发之注册登录方法示例
▪Android获取手机SIM卡运营商信息的方法
▪Android实现将已发送的短信写入短信数据库的...
▪Android发送短信功能代码
▪Android根据电话号码获得联系人头像实例代码
▪Android中GPS定位的用法实例
▪Android实现退出时关闭所有Activity的方法
▪Android实现文件的分割和组装
论坛 iis7站长之家
▪Android双击返回键退出程序的实现方法
▪Android实现侦听电池状态显示、电量及充电动...
▪Android获取当前已连接的wifi信号强度的方法
▪Android实现动态显示或隐藏密码输入框的内容
▪根据USER-AGENT判断手机类型并跳转到相应的app...
▪Android Touch事件分发过程详解
▪Android中实现为TextView添加多个可点击的文本
▪Android程序设计之AIDL实例详解
▪Android显式启动与隐式启动Activity的区别介绍
▪Android按钮单击事件的四种常用写法总结
▪Android消息处理机制Looper和Handler详解
▪Android实现Back功能代码片段总结
▪Android实用的代码片段 常用代码总结
▪Android实现弹出键盘的方法
▪Android中通过view方式获取当前Activity的屏幕截...
▪Android提高之自定义Menu(TabMenu)实现方法
▪Android提高之多方向抽屉实现方法
▪Android提高之MediaPlayer播放网络音频的实现方法...
▪Android提高之MediaPlayer播放网络视频的实现方法...
▪Android提高之手游转电视游戏的模拟操控
 


站内导航:


特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

©2012-2021,,E-mail:www_#163.com(请将#改为@)

浙ICP备11055608号-3