android 图片点击一下就放大到全屏,再点一下就回到原界面

转自:http://maozonghong.blog.163.com/blog/static/210596172201210921622748/




  1. public class TestanroidpicActivity extends Activity {  
  2.      /** Called when the activity is first created. */  
  3.          Bitmap bp=null;  
  4.          ImageView imageview;  
  5.          float scaleWidth;  
  6.          float scaleHeight;  
  7.            
  8.         int h;  
  9.          boolean num=false;  
  10.      @Override  
  11.      public void onCreate(Bundle savedInstanceState) {  
  12.          super.onCreate(savedInstanceState);  
  13.          setContentView(R.layout.main);  
  14.            
  15.          Display display=getWindowManager().getDefaultDisplay();  
  16.          imageview=(ImageView)findViewById(R.id.imageview);  
  17.          bp=BitmapFactory.decodeResource(getResources(),R.drawable.icon);  
  18.          int width=bp.getWidth();  
  19.          int height=bp.getHeight();  
  20.          int w=display.getWidth();  
  21.          int h=display.getHeight();  
  22.          scaleWidth=((float)w)/width;  
  23.          scaleHeight=((float)h)/height;  
  24.          imageview.setImageBitmap(bp);  
  25.      }  
  26.          @Override  
  27.          public boolean onTouchEvent(MotionEvent event) {  
  28.                    
  29.                 switch(event.getAction()){  
  30.                    
  31.                 case MotionEvent.ACTION_DOWN:  
  32.                  if(num==true)        {  
  33.                          Matrix matrix=new Matrix();  
  34.                          matrix.postScale(scaleWidth,scaleHeight);  
  35.                            
  36.                         Bitmap newBitmap=Bitmap.createBitmap(bp, 00, bp.getWidth(), bp.getHeight(), matrix, true);  
  37.                          imageview.setImageBitmap(newBitmap);  
  38.                          num=false;  
  39.                          }  
  40.                  else{  
  41.                          Matrix matrix=new Matrix();  
  42.                          matrix.postScale(1.0f,1.0f);  
  43.                          Bitmap newBitmap=Bitmap.createBitmap(bp, 00, bp.getWidth(), bp.getHeight(), matrix, true);  
  44.                      imageview.setImageBitmap(newBitmap);  
  45.                      num=true;  
  46.                  }  
  47.                          break;  
  48.                  }  
  49.                    
  50.                   
  51.                 return super.onTouchEvent(event);  
  52.          }  
  53.    
  54.       
  55.           
  56.       
  57.       
  58. }  

你可能感兴趣的:(android)