zoukankan      html  css  js  c++  java
  • Android开发经验之点击图片判断是否在图片范围之内

    1. package xiaosi.grivaty;  
    2.   
    3. import android.content.Context;  
    4. import android.graphics.Bitmap;  
    5. import android.graphics.BitmapFactory;  
    6. import android.graphics.Canvas;  
    7. import android.graphics.Rect;  
    8. import android.view.MotionEvent;  
    9. import android.view.View;  
    10.   
    11. public class Rects extends View  
    12. {  
    13.   
    14.     private Bitmap bitmap = null;  
    15.     private float x,y;  
    16.     public Rects(Context context)  
    17.     {  
    18.         super(context);  
    19.     }  
    20.   
    21.     @Override  
    22.     protected void onDraw(Canvas canvas)  
    23.     {  
    24.         super.onDraw(canvas);  
    25.         bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.v);  
    26.         canvas.drawBitmap(bitmap, 0, 0, null);  
    27.           
    28.         //创建和位图一样位置的Rect  
    29.         Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());  
    30.         if(rect.contains((int)x, (int)y)){  
    31.             System.out.println("范围之内");  
    32.         }  
    33.         else{  
    34.             System.out.println("范围之外");  
    35.         }  
    36.         System.out.println("图片宽度:" + bitmap.getWidth() + "图像高度:" + bitmap.getHeight());  
    37.         System.out.println("点击X:" + x + "点击Y:" + y);  
    38.     }  
    39.   
    40.     @Override  
    41.     public boolean onTouchEvent(MotionEvent event)  
    42.     {  
    43.         if (event.getAction() == MotionEvent.ACTION_DOWN)  
    44.         {  
    45.             x = event.getX();  
    46.             y = event.getY();  
    47.             // 重绘  
    48.             invalidate();  
    49.         }  
    50.         return true;  
    51.     }  
    52.       
    53. }  


  • 相关阅读:
    python列表作为默认参数的问题
    python 强制停止线程
    cProfile 分析python运行时间
    python global全局变量 模块通信问题
    ajax请求数据get、post
    vue中加载three.js全景图
    vue中加载three.js的gltf模型
    vue-cli2.x与vue-cli3.x的搭建
    cesium加载gltf模型
    vue/cli3引入cesium
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/6722196.html
Copyright © 2011-2022 走看看