歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Android 圖片放大縮小

Android 圖片放大縮小
  1. package com.wansha;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.AlertDialog;  
  5. import android.content.DialogInterface;  
  6. import android.graphics.Bitmap;  
  7. import android.graphics.BitmapFactory;  
  8. import android.graphics.Matrix;  
  9. import android.os.Bundle;  
  10. import android.util.DisplayMetrics;  
  11. import android.util.Log;  
  12. import android.view.KeyEvent;  
  13. import android.view.View;  
  14. import android.view.View.OnClickListener;  
  15. import android.widget.Button;  
  16. import android.widget.ImageView;  
  17. import android.widget.RelativeLayout;  
  18.   
  19. public class Activity31 extends Activity {  
  20.     private RelativeLayout layout;  
  21.     private Button big;  
  22.     private Button small;  
  23.     private ImageView bg;  
  24.     private Bitmap map;  
  25.     private float scaleH = 1;  
  26.     private float scaleW = 1;  
  27.     private int id;  
  28.     private DisplayMetrics metrics;  
  29.     @Override  
  30.     public void onCreate(Bundle savedInstanceState) {  
  31.         super.onCreate(savedInstanceState);  
  32.         setContentView(R.layout.main);  
  33.         this.layout = (RelativeLayout)this.findViewById(R.id.la);  
  34.         this.big = (Button)this.findViewById(R.id.big);  
  35.         this.small = (Button)this.findViewById(R.id.small);  
  36. //        this.image2 = (ImageView)this.findViewById(R.id.image1);   
  37. //        this.image2.setImageResource(R.drawable.left);   
  38.         this.bg = (ImageView)this.findViewById(R.id.bg);  
  39. //        this.bg.setImageResource(R.drawable.tttt);   
  40.           
  41.         this.map = BitmapFactory.decodeResource(getResources(), R.drawable.tttt);  
  42.         Log.d("mydebug""imageheigh---->" + map.getWidth() + "imagewidth------->" + map.getHeight());  
  43.           
  44.         metrics = new DisplayMetrics();  
  45.         getWindowManager().getDefaultDisplay().getMetrics(metrics);  
  46.           
  47. //        Bitmap bm = Bitmap.createBitmap(map, 0, 0, 100, 100);   
  48. //        this.bg.setImageBitmap(bm);   
  49.         Log.d("mydebug""screen_width--------->" + metrics.widthPixels + "screen_height----------->" + metrics.heightPixels);  
  50.   
  51.         this.big.setOnClickListener(new OnClickListener() {  
  52.               
  53.             @Override  
  54.             public void onClick(View v) {  
  55.                 float scale = 1.25f;  
  56.                 Matrix matrix = new Matrix();  
  57.                 scaleW = scaleW * scale;  
  58.                 scaleH = scaleH * scale;  
  59.                 if(scaleH*map.getHeight() >= metrics.heightPixels || scaleW*map.getWidth() >= metrics.widthPixels){  
  60.                     big.setEnabled(false);  
  61.                     return;  
  62.                 }  
  63.                 if(id == 0){  
  64.                     layout.removeView(bg);  
  65.                 }else{  
  66.                     layout.removeView((ImageView)findViewById(id));  
  67.                 }  
  68.                 matrix.postScale(scaleW, scaleH);  
  69.                 Bitmap bitmap = Bitmap.createBitmap(map, 00, map.getWidth(), map.getHeight(), matrix, true);  
  70.                 bg.setImageBitmap(bitmap);  
  71.                 ImageView image = new ImageView(Activity31.this);  
  72.                 image.setId(++id);  
  73.                 image.setImageBitmap(bitmap);  
  74.                 layout.addView(image);  
  75.                 setContentView(layout);  
  76.                 System.out.println("bit---------->" + scaleH*scale*map.getHeight() + "    :      " + scaleW*scale*map.getWidth());  
  77.                 if(scaleH*scale*map.getHeight() >= metrics.heightPixels || scaleW*scale*map.getWidth() >= metrics.widthPixels){  
  78.                     big.setEnabled(false);  
  79.                 }else{  
  80.                     big.setEnabled(true);  
  81.                 }  
  82.                 small.setEnabled(true);  
  83.             }  
  84.         });  
  85.         this.small.setOnClickListener(new OnClickListener() {  
  86.               
  87.             @Override  
  88.             public void onClick(View v) {  
  89.                 if(id == 0){  
  90.                     layout.removeView(bg);  
  91.                 }else{  
  92.                     layout.removeView((ImageView)findViewById(id));  
  93.                 }  
  94.                 float scale = 0.8f;  
  95.                 Matrix matrix = new Matrix();  
  96.                 scaleW = scaleW * scale;  
  97.                 scaleH = scaleH * scale;  
  98.                 matrix.postScale(scaleW, scaleH);  
  99.                 Bitmap bitmap = Bitmap.createBitmap(map, 00, map.getWidth(), map.getHeight(), matrix, true);  
  100.                 bg.setImageBitmap(bitmap);  
  101.                 ImageView image = new ImageView(Activity31.this);  
  102.                 image.setId(++id);  
  103.                 image.setImageBitmap(bitmap);  
  104.                 layout.addView(image);  
  105.                 setContentView(layout);  
  106.                 if(scaleH*scale <=  0.4){  
  107.                     small.setEnabled(false);  
  108.                 }else{  
  109.                     big.setEnabled(true);  
  110.                 }  
  111.                 big.setEnabled(true);  
  112.             }  
  113.   
  114.         });  
  115.     }  
  116.     @Override  
  117.     protected void onSaveInstanceState(Bundle outState) {  
  118.         // TODO Auto-generated method stub   
  119.         super.onSaveInstanceState(outState);  
  120.     }  
  121.     @Override  
  122.     public boolean onKeyDown(int keyCode, KeyEvent event) {  
  123.         System.out.println("keycode--------->" + keyCode);  
  124.         if(keyCode == KeyEvent.KEYCODE_BACK){  
  125.             new AlertDialog.Builder(this).setTitle("退出").setMessage("確定要退出嗎?").setPositiveButton("確定"new DialogInterface.OnClickListener() {  
  126.                   
  127.                 @Override  
  128.                 public void onClick(DialogInterface dialog, int which) {  
  129.                     finish();  
  130.                     android.os.Process.killProcess(android.os.Process.myPid());  
  131.                 }  
  132.             }).setNegativeButton("取消"new DialogInterface.OnClickListener() {  
  133.                   
  134.                 @Override  
  135.                 public void onClick(DialogInterface dialog, int which) {  
  136.                       
  137.                 }  
  138.             }).show();  
  139.               
  140.         }  
  141.         return super.onKeyDown(keyCode, event);  
  142.     }  
  143. }  
Copyright © Linux教程網 All Rights Reserved