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

Android 實現縮小圖片像素

自定義View類控制焦點以及Android現實縮小圖片的像素和如何通過BitmapFactory讀得圖片資源,做了筆記。以下代碼提供給大伙參考。 

更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

  1. package com.view;   
  2.   
  3. import java.util.HashMap;   
  4.   
  5. import com.pojo.Constants;   
  6. import com.test.R;   
  7. import android.content.Context;   
  8. import android.graphics.Bitmap;   
  9. import android.graphics.BitmapFactory;   
  10. import android.graphics.Canvas;   
  11. import android.graphics.Color;   
  12. import android.graphics.Matrix;   
  13. import android.graphics.Paint;   
  14. import android.util.Log;   
  15. import android.view.KeyEvent;   
  16. import android.view.View;   
  17. import android.view.ViewGroup;   
  18.   
  19. public class ADWView extends View {   
  20.     Context myContext = this.getContext();   
  21.     //讀得String.xml裡面的資源   
  22.     private String a = myContext.getString(R.string.app_a);   
  23.     private String b = myContext.getString(R.string.app_b);   
  24.     private String c = myContext.getString(R.string.app_c);   
  25.     private String d = myContext.getString(R.string.app_d);   
  26.     private String e = myContext.getString(R.string.app_e);   
  27.     private int focusID = 0;   
  28.     private Bitmap focusbmp, progressBar_A,progressBar_B,progressBar_C,progressBar_D,progressBar_E,progressBar;   
  29.     private Bitmap  bitmap_A,bitmap_B,bitmap_C,bitmap_D,bitmap_E;   
  30.     HashMap<Integer, Integer> progressBarMap = null;   
  31.            
  32.     public ADWView(Context context) {   
  33.         super(context);   
  34.         this.setFocusable(true);   
  35.         this.requestFocus();   
  36.         initData();   
  37.         //焦點圖片通過BitmapFactory來得到圖片資源R.drawable.shortcut_bg_sel   
  38.         focusbmp = BitmapFactory.decodeResource(this.getResources(), R.drawable.shortcut_bg_sel);         
  39.      }   
  40.        
  41.   
  42.        
  43.     @Override  
  44.     protected void onDraw(Canvas canvas) {   
  45.         //焦點圖片初始位置   
  46.         if (focusbmp != null) {   
  47.             canvas.drawBitmap(focusbmp, this.getPaddingLeft()-20,   
  48.                 this.getPaddingTop() + focusID * 100 + 230null);   
  49.         }    
  50.         super.onDraw(canvas);   
  51.         Paint paint = new Paint();   
  52.         paint.setColor(Color.WHITE);   
  53.         paint.setTextSize(25);   
  54.         paint.setAntiAlias(true);   
  55.         //獲得圖片的集合progressBarMap.get(Constants.progressBarValueA)   
  56.         progressBar_A = BitmapFactory.decodeResource(this.getResources(),progressBarMap.get(Constants.progressBarValueA));   
  57.         progressBar_B = BitmapFactory.decodeResource(this.getResources(),progressBarMap.get(Constants.progressBarValueB));   
  58.         progressBar_C = BitmapFactory.decodeResource(this.getResources(),progressBarMap.get(Constants.progressBarValueC));   
  59.         progressBar_D = BitmapFactory.decodeResource(this.getResources(),progressBarMap.get(Constants.progressBarValueD));   
  60.         progressBar_E = BitmapFactory.decodeResource(this.getResources(),progressBarMap.get(Constants.progressBarValueE));   
  61.         //獲得圖片的寬和高   
  62.         int w = 22000/progressBar_A.getWidth();   
  63.         int h = 22000/progressBar_A.getHeight();   
  64.         //刷新圖片的像素大小   
  65.         bitmap_A = progressBar.createScaledBitmap(progressBar_A, w, h, true);   
  66.         bitmap_B = progressBar.createScaledBitmap(progressBar_B, w, h, true);   
  67.         bitmap_C = progressBar.createScaledBitmap(progressBar_C, w, h, true);   
  68.         bitmap_D = progressBar.createScaledBitmap(progressBar_D, w, h, true);   
  69.         bitmap_E = progressBar.createScaledBitmap(progressBar_E, w, h, true);   
  70.                
  71.             //文字放置在屏幕的位置   
  72.             canvas.drawText(a + "   " + String.valueOf(Constants.A),   
  73.                 getPaddingLeft() + 80, getPaddingTop() + 300, paint);   
  74.             //圖片的放置在屏幕的位置   
  75.             canvas.drawBitmap(bitmap_A,getPaddingLeft()+ 244,   
  76.                     getPaddingTop() + 247null);   
  77.   
  78.             canvas.drawText(b + "   " + String.valueOf(Constants.B),   
  79.                 getPaddingLeft() + 80, getPaddingTop() + 400, paint);   
  80.             canvas.drawBitmap(bitmap_B, getPaddingLeft()+ 244,   
  81.                     getPaddingTop() + 347null);    
  82.                
  83.             canvas.drawText(c + "   " + String.valueOf(Constants.C),   
  84.                 getPaddingLeft() + 80,getPaddingTop() + 500, paint);   
  85.             canvas.drawBitmap(bitmap_C, getPaddingLeft()+ 244,   
  86.                     getPaddingTop() + 447null);    
  87.   
  88.             canvas.drawText(d + "   " + String.valueOf(Constants.D),   
  89.                 getPaddingLeft() + 80,getPaddingTop() + 600, paint);   
  90.             canvas.drawBitmap(bitmap_D, getPaddingLeft()+ 244,   
  91.                     getPaddingTop() + 547null);    
  92.   
  93.             canvas.drawText(e + "   " + String.valueOf(Constants.E),   
  94.                 getPaddingLeft() + 80,getPaddingTop() + 700, paint);   
  95.             canvas.drawBitmap(bitmap_E, getPaddingLeft()+ 244,   
  96.                     getPaddingTop() + 647null);    
  97.   
  98.     }   
  99.   
  100.     public boolean onKeyDown(int keyCode, KeyEvent event) {   
  101.         super.onKeyDown(keyCode, event);   
  102.         switch (keyCode) {   
  103.             case KeyEvent.KEYCODE_DPAD_UP:   
  104.                 if (focusID > 0) {   
  105.                     focusID--;   
  106.                 } else {   
  107.                     focusID = 4;   
  108.                 }   
  109.                 this.invalidate();   
  110.                 break;   
  111.             case KeyEvent.KEYCODE_DPAD_DOWN:   
  112.                 if (focusID < 4) {   
  113.                     focusID++;   
  114.                 } else {   
  115.                     focusID = 0;   
  116.                 }   
  117.                 this.invalidate();   
  118.                 break;   
  119.                 //左鍵的響應   
  120.             case KeyEvent.KEYCODE_DPAD_LEFT:   
  121.                 if (focusID == 0) {   
  122.                     if (Constants.A >-10) {   
  123.                         Constants.A--;   
  124.                         Constants.progressBarValueA--;   
  125.                     } else {   
  126.                         Constants.A = -10;   
  127.                         Constants.progressBarValueA=6;   
  128.                     }   
  129.                     this.invalidate();   
  130.                 } else if (focusID == 1) {   
  131.                     if (Constants.B > -10) {   
  132.                         Constants.B--;   
  133.                         Constants.progressBarValueB--;   
  134.                     } else {   
  135.                         Constants.B = -10;   
  136.                         Constants.progressBarValueB=6;   
  137.                     }   
  138.                     this.invalidate();   
  139.                 }else if (focusID == 2) {   
  140.                     if (Constants.C > -10) {   
  141.                         Constants.C--;   
  142.                         Constants.progressBarValueC--;   
  143.                     } else {   
  144.                         Constants.C = -10;   
  145.                         Constants.progressBarValueC=6;   
  146.                     }   
  147.                     this.invalidate();   
  148.                 }else if (focusID == 3) {   
  149.                     if (Constants.D > -10) {   
  150.                         Constants.D--;   
  151.                         Constants.progressBarValueD--;   
  152.                     } else {   
  153.                         Constants.D = -10;   
  154.                         Constants.progressBarValueD=6;   
  155.                     }   
  156.                     this.invalidate();   
  157.                 }else if (focusID == 4) {   
  158.                     if (Constants.E > -10) {   
  159.                         Constants.E--;   
  160.                         Constants.progressBarValueE--;   
  161.                     } else {   
  162.                         Constants.E = -10;   
  163.                         Constants.progressBarValueE=6;   
  164.                     }   
  165.                     this.invalidate();   
  166.                 }   
  167.                 break;   
  168.              //右鍵的響應   
  169.             case KeyEvent.KEYCODE_DPAD_RIGHT:   
  170.                 if (focusID == 0) {   
  171.                     if (Constants.A < (10-1)) {   
  172.                         Constants.A++;   
  173.                         Constants.progressBarValueA++;   
  174.                     } else {   
  175.                         Constants.A = 10;   
  176.                         Constants.progressBarValueA = 26;   
  177.                     }   
  178.                     this.invalidate();   
  179.                 } else if (focusID == 1) {   
  180.                     if (Constants.B < (10 - 1)) {   
  181.                         Constants.B++;   
  182.                         Constants.progressBarValueB++;   
  183.                     } else {   
  184.                         Constants.B = 10;   
  185.                         Constants.progressBarValueB = 26;   
  186.                     }   
  187.                     this.invalidate();   
  188.                 }else if (focusID == 2) {   
  189.                     if (Constants.C < (10 - 1)) {   
  190.                         Constants.C++;   
  191.                         Constants.progressBarValueC++;   
  192.                     } else {   
  193.                         Constants.C = 10;   
  194.                         Constants.progressBarValueC = 26;   
  195.                     }   
  196.                     this.invalidate();   
  197.                 }else if (focusID == 3) {   
  198.                     if (Constants.D < (10 - 1)) {   
  199.                         Constants.D++;   
  200.                         Constants.progressBarValueD++;   
  201.                     } else {   
  202.                         Constants.D = 10;   
  203.                         Constants.progressBarValueD = 26;   
  204.                     }   
  205.                     this.invalidate();   
  206.                 }else if (focusID == 4) {   
  207.                     if (Constants.E < (10 - 1)) {   
  208.                         Constants.E++;   
  209.                         Constants.progressBarValueE++;   
  210.                     } else {   
  211.                         Constants.E = 10;   
  212.                         Constants.progressBarValueE = 26;   
  213.                     }   
  214.                     this.invalidate();   
  215.                 }   
  216.                 break;   
  217.                    
  218.             //焦點的控制       
  219.             case KeyEvent.KEYCODE_ENTER:   
  220.                 switch (focusID) {   
  221.                     case 0:   
  222.                         focusID = 1;   
  223.                         break;   
  224.                     case 1:   
  225.                         focusID = 2;   
  226.                         break;   
  227.                     case 2:   
  228.                         focusID = 3;   
  229.                         break;   
  230.                     case 3:   
  231.                         focusID = 4;   
  232.                         break;   
  233.                     case 4:   
  234.                         focusID = 0;   
  235.                         break;   
  236.                     default:   
  237.                         break;   
  238.                 }   
  239.                 this.invalidate();   
  240.                 break;   
  241.              //退出按鍵的響應   
  242.             case KeyEvent.KEYCODE_BACK:   
  243.                 ((ViewGroup) (this.getParent())).removeView(this);   
  244.                 break;   
  245.             default:   
  246.                 break;   
  247.         }   
  248.         return true;   
  249.     }   
  250.        
  251.     //做一個圖片資源的集合   
  252.     public void initData() {   
  253.         progressBarMap = new HashMap<Integer, Integer>();   
  254.         progressBarMap.put(1, R.drawable.sound_1);   
  255.         progressBarMap.put(2, R.drawable.sound_2);   
  256.         progressBarMap.put(3, R.drawable.sound_3);   
  257.         progressBarMap.put(4, R.drawable.sound_4);   
  258.         progressBarMap.put(5, R.drawable.sound_5);   
  259.         progressBarMap.put(6, R.drawable.sound_6);   
  260.         progressBarMap.put(7, R.drawable.sound_7);   
  261.         progressBarMap.put(8, R.drawable.sound_8);   
  262.         progressBarMap.put(9, R.drawable.sound_9);   
  263.         progressBarMap.put(10, R.drawable.sound_10);   
  264.         progressBarMap.put(11, R.drawable.sound_11);   
  265.         progressBarMap.put(12, R.drawable.sound_12);   
  266.         progressBarMap.put(13, R.drawable.sound_13);   
  267.         progressBarMap.put(14, R.drawable.sound_14);   
  268.         progressBarMap.put(15, R.drawable.sound_15);   
  269.         progressBarMap.put(16, R.drawable.sound_16);   
  270.         progressBarMap.put(17, R.drawable.sound_17);   
  271.         progressBarMap.put(18, R.drawable.sound_18);   
  272.         progressBarMap.put(19, R.drawable.sound_19);   
  273.         progressBarMap.put(20, R.drawable.sound_20);   
  274.         progressBarMap.put(21, R.drawable.sound_21);   
  275.         progressBarMap.put(22, R.drawable.sound_22);   
  276.         progressBarMap.put(23, R.drawable.sound_23);   
  277.         progressBarMap.put(24, R.drawable.sound_24);   
  278.         progressBarMap.put(25, R.drawable.sound_25);   
  279.         progressBarMap.put(26, R.drawable.sound_26);   
  280.         progressBarMap.put(27, R.drawable.sound_27);   
  281.         progressBarMap.put(28, R.drawable.sound_28);   
  282.         progressBarMap.put(29, R.drawable.sound_29);   
  283.         progressBarMap.put(30, R.drawable.sound_30);   
  284.         progressBarMap.put(31, R.drawable.sound_31);   
  285.         progressBarMap.put(32, R.drawable.sound_32);   
  286.     }   
  287.        
  288. }  
Copyright © Linux教程網 All Rights Reserved