Android開發經驗之點擊圖片判斷是否在圖片範圍之內

來源:互聯網
上載者:User

標籤:ret   word   extend   imp   decode   圖片   bool   str   extends   

  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. }  


 

 

 

 

Android開發經驗之點擊圖片判斷是否在圖片範圍之內

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.