---
title: "android win8效果实现进阶(一)"
author: "adashao"
date: "2013-10-24T23:30:12Z"
tags: []
categories: ["移动开发"]
slug: "android-win8"
draft: false
lastmod: "2013-10-24T23:30:12Z"
id: 1964256
url: "https://adashao.iteye.com/blog/1964256"
---

最近在研究android 版本的win8效果，只牵强实现了一部分。
1.自定义一个view,做为要移动和展示用。
package com.shao.test;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.util.FloatMath;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.TranslateAnimation;

public class MyView2 extends View{

	
	private int alpha = 250;
	private boolean pressed = false;
	static final int NONE = 0;
    static final int DRAG = 1;	   //拖动中
    static final int ZOOM = 2;     //缩放中
    static final int BIGGER = 3;   //放大ing
    static final int SMALLER = 4;  //缩小ing
    private int mode = NONE;	   //当前的事件 

    private float beforeLenght;   //两触点距离
    private float afterLenght;    //两触点距离
    private float scale = 0.04f;  //缩放的比例 X Y方向都是这个值 越大缩放的越快
   
    private int screenW;
    private int screenH;
    
    /*处理拖动 变量 */
    private int start_x;
    private int start_y;
	private int stop_x ;
	private int stop_y ;
	
	
	private int res_id;
	
	private TranslateAnimation trans; //处理超出边界的动画
	
	
	private  int mX;
	private  int mY;
	private  int mWidth;
	private  int mHeight;
	
	private static int mStartX;
	private static int mStartY;
	private static int mEndX;
	private static int mEndY;
	
	private static int isTouchID=-1;
	
	
	public MyView2(Context context) {
		super(context);
	}

	public MyView2(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}
	
	public MyView2(Context context,int w,int h,int res)
	{
		super(context);
		this.setPadding(0, 0, 0, 0);
		screenW = w;
		screenH = h;
		res_id = res;
		
		
	}
	public MyView2(Context context,int w,int h)
	{
		super(context);
		this.setPadding(0, 0, 0, 0);
		screenW = w;
		screenH = h;
		
	}
	
	
	Matrix m;
	public void show()
	{
		new Thread(){
			public void run() {
				int time = 2000;
				try
				{
					pressed = true;
					while(time>0)
					{
						Thread.sleep(200);
						time -= 200;
						alpha-= 25;
						
						postInvalidate();
					}
					pressed = false;
				}
				catch (Exception e)
				{
					e.printStackTrace();
				}
			};
		}.start();
	}
	
	
	
	/**
	 * 就算两点间的距离
	 */
    private float spacing(MotionEvent event) {
        float x = event.getX(0) - event.getX(1);
        float y = event.getY(0) - event.getY(1);
        return FloatMath.sqrt(x * x + y * y);
    }
	
	@Override
	public boolean onTouchEvent(MotionEvent event)
	{
		
		switch (event.getAction() & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:
        	show();
        	isTouchID = 1;
        		mode = DRAG;
    	    	stop_x = (int) event.getRawX();
    	    	
    	    	stop_y = (int) event.getRawY();
        		start_x = (int) event.getX();
            	start_y = stop_y - this.getTop();
            	
            	mEndX = stop_x;
            	mEndY = stop_y;
            	mStartX = start_x;
            	mStartY = start_y;
            	if(event.getPointerCount()==2)
            		beforeLenght = spacing(event);
            	
                break;
        case MotionEvent.ACTION_POINTER_DOWN:
        	isTouchID = 1;
                if (spacing(event) > 10f) {
                        mode = ZOOM;
                		beforeLenght = spacing(event);
                }
                break;
        case MotionEvent.ACTION_UP:
        	isTouchID = 2;
        	/*判断是否超出范围     并处理*/
        		int disX = 0;
        		int disY = 0;
	        	if(getHeight()<=screenH || this.getTop()<0)
	        	{
		        	if(this.getTop()<0 )
		        	{
		        		int dis = getTop();
	                	this.layout(this.getLeft(), 0, this.getRight(), 0 + this.getHeight());
	            		disY = dis - getTop();
		        	}
		        	else if(this.getBottom()>screenH)
		        	{
		        		disY = getHeight()- screenH+getTop();
	                	this.layout(this.getLeft(), screenH-getHeight(), this.getRight(), screenH);
		        	}
	        	}
	        	if(getWidth()<=screenW)
	        	{
		        	if(this.getLeft()<0)
		        	{
		        		disX = getLeft();
	                	this.layout(0, this.getTop(), 0+getWidth(), this.getBottom());
		        	}
		        	else if(this.getRight()>screenW)
		        	{
		        		disX = getWidth()-screenW+getLeft();
	                	this.layout(screenW-getWidth(), this.getTop(), screenW, this.getBottom());
		        	}
	        	}
	        	if(disX!=0 || disY!=0)
	        	{
            		trans = new TranslateAnimation(disX, 0, disY, 0);
            		trans.setDuration(500);
            		this.startAnimation(trans);
	        	}
	        	mode = NONE;
        		break;
        case MotionEvent.ACTION_POINTER_UP:
        	
                mode = NONE;
                break;
        case MotionEvent.ACTION_MOVE:
        	isTouchID = 3;
        		/*处理拖动*/
                if (mode == DRAG) {
                	if(Math.abs(stop_x-start_x-getLeft())<88 && Math.abs(stop_y - start_y-getTop())<85)
                	{
                    	this.setPosition(stop_x - start_x, stop_y - start_y, stop_x + this.getWidth() - start_x, stop_y - start_y + this.getHeight());           	
                    	stop_x = (int) event.getRawX();
                    	stop_y = (int) event.getRawY();
                	}
                } 
                /*处理缩放*/
                else if (mode == ZOOM) {
                	if(spacing(event)>10f)
                	{
                        afterLenght = spacing(event);
                        float gapLenght = afterLenght - beforeLenght;                     
                        if(gapLenght == 0) {  
                           break;
                        }
                        else if(Math.abs(gapLenght)>5f)
                        {
                            if(gapLenght>0) { 
                                this.setScale(scale,BIGGER);   
                            }else {  
                                this.setScale(scale,SMALLER);   
                            }                             
                            beforeLenght = afterLenght; 
                        }
                	}
                }
                break;
        }
        return true;	
		
		/*
		if(event.getAction() == MotionEvent.ACTION_DOWN)
			show();

		return false;*/
	}
	
	
	
	
	@Override
	protected void onDraw(Canvas canvas)
	{
		Paint p = new Paint();
		p.setColor(Color.WHITE);
		p.setStyle(Paint.Style.STROKE);
		p.setStrokeWidth(10);
		BitmapDrawable bd=(BitmapDrawable) getBackground();
		
		//BitmapDrawable bd=null ;//= (BitmapDrawable)getResources();
		//BitmapDrawable bd = (BitmapDrawable) getDrawable();
		if(bd!=null)
		{
			canvas.drawBitmap(imageScale(bd.getBitmap(), 107, 113), 21,18, p);	
			
		}
		canvas.drawBitmap(BitmapFactory.decodeResource(getContext().getResources(), res_id), 0, 0, p);
		if(isPressed())
		{
			canvas.drawRect(5,5,140,140,p);
		}
		/*
		if(pressed)
		{
			p.setAlpha(alpha);
			canvas.drawRect(5,5,140,140,p);
		}*/
	}
	
	public static Bitmap imageScale(Bitmap bitmap, int dst_w, int dst_h){  
		  int  src_w = bitmap.getWidth();
		  int  src_h = bitmap.getHeight();
		  float scale_w = ((float)dst_w)/src_w;
		  float  scale_h = ((float)dst_h)/src_h;
		  Matrix  matrix = new Matrix();
		  matrix.postScale(scale_w, scale_h);
		  Bitmap dstbmp = Bitmap.createBitmap(bitmap, 0, 0, src_w, src_h, matrix, true);	  
		  return dstbmp;
	}
	
	
	/**
	 * 实现处理缩放
	 */
    private void setScale(float temp,int flag) {   
        /*
        if(flag==BIGGER) {   
            this.setFrame(this.getLeft()-(int)(temp*this.getWidth()),    
                          this.getTop()-(int)(temp*this.getHeight()),    
                          this.getRight()+(int)(temp*this.getWidth()),    
                          this.getBottom()+(int)(temp*this.getHeight()));      
        }else if(flag==SMALLER){   
            this.setFrame(this.getLeft()+(int)(temp*this.getWidth()),    
                          this.getTop()+(int)(temp*this.getHeight()),    
                          this.getRight()-(int)(temp*this.getWidth()),    
                          this.getBottom()-(int)(temp*this.getHeight()));   
        }  */ 
    }
    
    
  
	
	/**
	 * 实现处理拖动
	 */
    
   
    public void setPosition(int left,int top,int right,int bottom) {  
    	
    	postInvalidate();
    	
    	this.layout(left,top,right,bottom);  
    	invalidate();
    }
    
    
    
    protected void onLayout(boolean changed, int left, int top, int right,
    		int bottom) {
    	// TODO Auto-generated method stub
    	Log.i("shao1","onlayout   -left"+left+"  top="+top+" right="+right+" bottom="+bottom);
    	mX=left;
    	mY=top;
    	mWidth=right;
    	mHeight=bottom;
    	super.onLayout(changed, left, top, right, bottom);
    }
    
    
    public static int getDirection(){
    	int x = mEndX -mStartX;//
        int y = mEndY -mStartY;//
        //限制必须得划过屏幕的1/3才能算划过
       // float x_limit = screen.widthPixels / 3;
       // float y_limit = screen.heightPixels / 3;
        int x_limit = 20;
        int y_limit = 20;
        int x_abs = Math.abs(x);
        int y_abs = Math.abs(y);
        if(x_abs >= y_abs){
            //gesture left or right
            if(x > x_limit || x < -x_limit){
                if(x>0){
                    //right
                	Log.i("shao","right-----------");
                	return 0;
                   // show("right");
                }else if(x<0){
                    //left
                		Log.i("shao","left-----------");
                		return 1;
                }
            }
        }else{
            //gesture down or up
            if(y> y_limit || y < -y_limit){
                if(y>0){
                    //down
                	Log.i("shao","down-----------");
                	return 2;
                    
                }else if(y<0){
                    //up
                	Log.i("shao","up-----------");
                	return 3;
                 }
            }
        }
    	return 0;
    }
    
    
    public  int getChangePosition(){
    	int retValue={mX,mY,mWidth,mHeight};
    	return retValue;
    }
    
    public int touchId(){
    	//Log.i("shao","   istouch    ="+isTouchID);
    	return isTouchID;
    }
    
    public int getLocation(int index) {
		int  result =null;
		for(int i=0;i<position.length;i++){
			if(i==index){
				for(int j=0;j<4;j++){
					result=position;
				}
				break;
			}
			
		}
		return result;
	}

	public void setLocation(int index, int newposition) {

		for(int i=0;i<position.length;i++){
			if(i==index){
				for(int j=0;j<4;j++){
					position=newposition;
				}
				break;
			}
		}
	}

	public    int position = 
		{
			{
				0,0,200,400
			},
			{
				200, 0, 800, 200
			},
			{
				200, 200, 800, 400
			}
		};

}




待续.....