一个仿WIN8磁铁效果的ImageView

源地址忘了



Java代码   收藏代码
  1. import android.content.Context;  
  2. import android.graphics.Camera;  
  3. import android.graphics.Canvas;  
  4. import android.graphics.Matrix;  
  5. import android.graphics.Paint;  
  6. import android.graphics.PaintFlagsDrawFilter;  
  7. import android.graphics.drawable.BitmapDrawable;  
  8. import android.graphics.drawable.Drawable;  
  9. import android.os.Handler;  
  10. import android.os.Message;  
  11. import android.util.AttributeSet;  
  12. import android.view.MotionEvent;  
  13. import android.widget.ImageView;  
  14.   
  15. public class MyImageView extends ImageView {  
  16.   
  17.     private boolean onAnimation = true;  
  18.     private int rotateDegree = 10;  
  19.   
  20.     private boolean isFirst = true;  
  21.     private float minScale = 0.95f;  
  22.     private int vWidth;  
  23.     private int vHeight;  
  24.     private boolean isFinish = true,isActionMove=false,isScale=false;  
  25.     private Camera camera;  
  26.   
  27.     boolean XbigY = false;  
  28.     float RolateX = 0;  
  29.     float RolateY = 0;  
  30.   
  31.     OnViewClick οnclick=null;  
  32.       
  33.     public MyImageView(Context context) {  
  34.         super(context);  
  35.         // TODO Auto-generated constructor stub  
  36.         camera = new Camera();  
  37.     }  
  38.   
  39.     public MyImageView(Context context, AttributeSet attrs) {  
  40.         super(context, attrs);  
  41.         // TODO Auto-generated constructor stub  
  42.         camera = new Camera();  
  43.     }  
  44.   
  45.     public void SetAnimationOnOff(boolean oo) {  
  46.         onAnimation = oo;  
  47.     }  
  48.     public void setOnClickIntent(OnViewClick onclick){  
  49.         this.οnclick=onclick;  
  50.     }  
  51.     @Override  
  52.     protected void onDraw(Canvas canvas) {  
  53.         super.onDraw(canvas);  
  54.         if (isFirst) {  
  55.             isFirst = false;  
  56.             init();  
  57.         }  
  58.         canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG  
  59.                 | Paint.FILTER_BITMAP_FLAG));  
  60.     }  
  61.   
  62.     public void init() {  
  63.         vWidth = getWidth() - getPaddingLeft() - getPaddingRight();  
  64.         vHeight = getHeight() - getPaddingTop() - getPaddingBottom();  
  65.         Drawable drawable = getDrawable();  
  66.         BitmapDrawable bd = (BitmapDrawable) drawable;  
  67.         bd.setAntiAlias(true);  
  68.     }  
  69.   
  70.     @Override  
  71.     public boolean onTouchEvent(MotionEvent event) {  
  72.         super.onTouchEvent(event);  
  73.         if (!onAnimation)  
  74.             return true;  
  75.   
  76.           
  77.         switch (event.getAction() & MotionEvent.ACTION_MASK) {  
  78.         case MotionEvent.ACTION_DOWN:  
  79.             float X = event.getX();  
  80.             float Y = event.getY();  
  81.             RolateX = vWidth / 2 - X;  
  82.             RolateY = vHeight / 2 - Y;  
  83.             XbigY = Math.abs(RolateX) > Math.abs(RolateY) ? true : false;  
  84.   
  85.             isScale = X > vWidth / 3 && X < vWidth * 2 / 3 && Y > vHeight / 3&& Y < vHeight * 2 / 3;  
  86.             isActionMove=false;  
  87.               
  88.             if (isScale) {  
  89.                 handler.sendEmptyMessage(1);  
  90.             } else {  
  91.                 rolateHandler.sendEmptyMessage(1);  
  92.             }  
  93.             break;  
  94.         case MotionEvent.ACTION_MOVE:  
  95.             float x=event.getX();float y=event.getY();  
  96.             if(x>vWidth || y>vHeight || x<0 || y<0){  
  97.                 isActionMove=true;  
  98.             }else{  
  99.                 isActionMove=false;  
  100.             }  
  101.               
  102.             break;  
  103.         case MotionEvent.ACTION_UP:  
  104.             if (isScale) {  
  105.                 handler.sendEmptyMessage(6);  
  106.             } else {  
  107.                 rolateHandler.sendEmptyMessage(6);  
  108.             }  
  109.             break;  
  110.         }  
  111.         return true;  
  112.     }  
  113.     public interface OnViewClick {  
  114.         public void onClick();  
  115.     }  
  116.     private Handler rolateHandler = new Handler() {  
  117.         private Matrix matrix = new Matrix();  
  118.         private float count = 0;  
  119.   
  120.         @Override  
  121.         public void handleMessage(Message msg) {  
  122.             super.handleMessage(msg);  
  123.             matrix.set(getImageMatrix());  
  124.             switch (msg.what) {  
  125.             case 1:  
  126.                 count = 0;  
  127.                 BeginRolate(matrix, (XbigY ? count : 0), (XbigY ? 0 : count));  
  128.                 rolateHandler.sendEmptyMessage(2);  
  129.                 break;  
  130.             case 2:  
  131.                 BeginRolate(matrix, (XbigY ? count : 0), (XbigY ? 0 : count));  
  132.                 if (count < getDegree()) {  
  133.                     rolateHandler.sendEmptyMessage(2);  
  134.                 } else {  
  135.                     isFinish = true;  
  136.                 }  
  137.                 count++;  
  138.                 count++;  
  139.                 break;  
  140.             case 3:  
  141.                 BeginRolate(matrix, (XbigY ? count : 0), (XbigY ? 0 : count));  
  142.                 if (count > 0) {  
  143.                     rolateHandler.sendEmptyMessage(3);  
  144.                 } else {  
  145.                     isFinish = true;  
  146.                     if(!isActionMove&&onclick!=null){  
  147.                         onclick.onClick();  
  148.                     }  
  149.                 }  
  150.                 count--;  
  151.                 count--;  
  152.                 break;  
  153.             case 6:  
  154.                 count = getDegree();  
  155.                 BeginRolate(matrix, (XbigY ? count : 0), (XbigY ? 0 : count));  
  156.                 rolateHandler.sendEmptyMessage(3);  
  157.                 break;  
  158.             }  
  159.         }  
  160.     };  
  161.   
  162.     private synchronized void BeginRolate(Matrix matrix, float rolateX,  
  163.             float rolateY) {  
  164.         // Bitmap bm = getImageBitmap();  
  165.         int scaleX = (int) (vWidth * 0.5f);  
  166.         int scaleY = (int) (vHeight * 0.5f);  
  167.         camera.save();  
  168.         camera.rotateX(RolateY > 0 ? rolateY : -rolateY);  
  169.         camera.rotateY(RolateX < 0 ? rolateX : -rolateX);  
  170.         camera.getMatrix(matrix);  
  171.         camera.restore();  
  172.         // 控制中心点  
  173.         if (RolateX > 0 && rolateX != 0) {  
  174.             matrix.preTranslate(-vWidth, -scaleY);  
  175.             matrix.postTranslate(vWidth, scaleY);  
  176.         } else if (RolateY > 0 && rolateY != 0) {  
  177.             matrix.preTranslate(-scaleX, -vHeight);  
  178.             matrix.postTranslate(scaleX, vHeight);  
  179.         } else if (RolateX < 0 && rolateX != 0) {  
  180.             matrix.preTranslate(-0, -scaleY);  
  181.             matrix.postTranslate(0, scaleY);  
  182.         } else if (RolateY < 0 && rolateY != 0) {  
  183.             matrix.preTranslate(-scaleX, -0);  
  184.             matrix.postTranslate(scaleX, 0);  
  185.         }  
  186.         setImageMatrix(matrix);  
  187.     }  
  188.   
  189.     private Handler handler = new Handler() {  
  190.         private Matrix matrix = new Matrix();  
  191.         private float s;  
  192.         int count = 0;  
  193.   
  194.         @Override  
  195.         public void handleMessage(Message msg) {  
  196.             super.handleMessage(msg);  
  197.             matrix.set(getImageMatrix());  
  198.             switch (msg.what) {  
  199.             case 1:  
  200.                 if (!isFinish) {  
  201.                     return;  
  202.                 } else {  
  203.                     isFinish = false;  
  204.                     count = 0;  
  205.                     s = (float) Math.sqrt(Math.sqrt(minScale));  
  206.                     BeginScale(matrix, s);  
  207.                     handler.sendEmptyMessage(2);  
  208.                 }  
  209.                 break;  
  210.             case 2:  
  211.                 BeginScale(matrix, s);  
  212.                 if (count < 4) {  
  213.                     handler.sendEmptyMessage(2);  
  214.                 } else {  
  215.                     isFinish = true;  
  216.                     if(!isActionMove&&onclick!=null){  
  217.                         onclick.onClick();  
  218.                     }  
  219.                 }  
  220.                 count++;  
  221.                 break;  
  222.             case 6:  
  223.                 if (!isFinish) {  
  224.                     handler.sendEmptyMessage(6);  
  225.                 } else {  
  226.                     isFinish = false;  
  227.                     count = 0;  
  228.                     s = (float) Math.sqrt(Math.sqrt(1.0f / minScale));  
  229.                     BeginScale(matrix, s);  
  230.                     handler.sendEmptyMessage(2);  
  231.                 }  
  232.                 break;  
  233.             }  
  234.         }  
  235.     };  
  236.   
  237.     private synchronized void BeginScale(Matrix matrix, float scale) {  
  238.         int scaleX = (int) (vWidth * 0.5f);  
  239.         int scaleY = (int) (vHeight * 0.5f);  
  240.         matrix.postScale(scale, scale, scaleX, scaleY);  
  241.         setImageMatrix(matrix);  
  242.     }  
  243.   
  244.     public int getDegree() {  
  245.         return rotateDegree;  
  246.     }  
  247.   
  248.     public void setDegree(int degree) {  
  249.         rotateDegree = degree;  
  250.     }  
  251.   
  252.     public float getScale() {  
  253.         return minScale;  
  254.     }  
  255.   
  256.     public void setScale(float scale) {  
  257.         minScale = scale;  
  258.     }  
  259. }  


用法:
Java代码   收藏代码
  1. joke=(MyImageView) findViewById(R.id.c_joke);  
  2.         joke.setOnClickIntent(new MyImageView.OnViewClick() {  
  3.               
  4.             @Override  
  5.             public void onClick() {  
  6.                 // TODO Auto-generated method stub  
  7.                 Toast.makeText(TestRolateAnimActivity.this"事件触发"1000).show();  
  8.                 System.out.println("1");  
  9.             }  
  10.         });  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值