Android 实现缩小图片像素

自定义View类控制焦点以及Android现实缩小图片的像素和如何通过BitmapFactory读得图片资源,做了笔记。以下代码提供给大伙参考。 

更多Android相关信息见Android 专题页面 ?tid=11

package com.view;       import java.util.HashMap;       import com.pojo.Constants;    import com.test.R;    import android.content.Context;    import android.graphics.Bitmap;    import android.graphics.BitmapFactory;    import android.graphics.Canvas;    import android.graphics.Color;    import android.graphics.Matrix;    import android.graphics.Paint;    import android.util.Log;    import android.view.KeyEvent;    import android.view.View;    import android.view.ViewGroup;       public class ADWView extends View {        Context myContext = this.getContext();        //读得String.xml里面的资源        private String a = myContext.getString(R.string.app_a);        private String b = myContext.getString(R.string.app_b);        private String c = myContext.getString(R.string.app_c);        private String d = myContext.getString(R.string.app_d);        private String e = myContext.getString(R.string.app_e);        private int focusID = 0;        private Bitmap focusbmp, progressBar_A,progressBar_B,progressBar_C,progressBar_D,progressBar_E,progressBar;        private Bitmap  bitmap_A,bitmap_B,bitmap_C,bitmap_D,bitmap_E;        HashMap<Integer, Integer> progressBarMap = null;                    public ADWView(Context context) {            super(context);            this.setFocusable(true);            this.requestFocus();            initData();            //焦点图片通过BitmapFactory来得到图片资源R.drawable.shortcut_bg_sel            focusbmp = BitmapFactory.decodeResource(this.getResources(), R.drawable.shortcut_bg_sel);               }                           @Override       protected void onDraw(Canvas canvas) {            //焦点图片初始位置            if (focusbmp != null) {                canvas.drawBitmap(focusbmp, this.getPaddingLeft()-20,                    this.getPaddingTop() + focusID * 100 + 230null);            }             super.onDraw(canvas);            Paint paint = new Paint();            paint.setColor(Color.WHITE);            paint.setTextSize(25);            paint.setAntiAlias(true);            //获得图片的集合progressBarMap.get(Constants.progressBarValueA)            progressBar_A = BitmapFactory.decodeResource(this.getResources(),progressBarMap.get(Constants.progressBarValueA));            progressBar_B = BitmapFactory.decodeResource(this.getResources(),progressBarMap.get(Constants.progressBarValueB));            progressBar_C = BitmapFactory.decodeResource(this.getResources(),progressBarMap.get(Constants.progressBarValueC));            progressBar_D = BitmapFactory.decodeResource(this.getResources(),progressBarMap.get(Constants.progressBarValueD));            progressBar_E = BitmapFactory.decodeResource(this.getResources(),progressBarMap.get(Constants.progressBarValueE));            //获得图片的宽和高            int w = 22000/progressBar_A.getWidth();            int h = 22000/progressBar_A.getHeight();            //刷新图片的像素大小            bitmap_A = progressBar.createScaledBitmap(progressBar_A, w, h, true);            bitmap_B = progressBar.createScaledBitmap(progressBar_B, w, h, true);            bitmap_C = progressBar.createScaledBitmap(progressBar_C, w, h, true);            bitmap_D = progressBar.createScaledBitmap(progressBar_D, w, h, true);            bitmap_E = progressBar.createScaledBitmap(progressBar_E, w, h, true);                                //文字放置在屏幕的位置                canvas.drawText(a + "   " + String.valueOf(Constants.A),                    getPaddingLeft() + 80, getPaddingTop() + 300, paint);                //图片的放置在屏幕的位置                canvas.drawBitmap(bitmap_A,getPaddingLeft()+ 244,                        getPaddingTop() + 247null);                   canvas.drawText(b + "   " + String.valueOf(Constants.B),                    getPaddingLeft() + 80, getPaddingTop() + 400, paint);                canvas.drawBitmap(bitmap_B, getPaddingLeft()+ 244,                        getPaddingTop() + 347null);                                 canvas.drawText(c + "   " + String.valueOf(Constants.C),                    getPaddingLeft() + 80,getPaddingTop() + 500, paint);                canvas.drawBitmap(bitmap_C, getPaddingLeft()+ 244,                        getPaddingTop() + 447null);                    canvas.drawText(d + "   " + String.valueOf(Constants.D),                    getPaddingLeft() + 80,getPaddingTop() + 600, paint);                canvas.drawBitmap(bitmap_D, getPaddingLeft()+ 244,                        getPaddingTop() + 547null);                    canvas.drawText(e + "   " + String.valueOf(Constants.E),                    getPaddingLeft() + 80,getPaddingTop() + 700, paint);                canvas.drawBitmap(bitmap_E, getPaddingLeft()+ 244,                        getPaddingTop() + 647null);            }           public boolean onKeyDown(int keyCode, KeyEvent event) {            super.onKeyDown(keyCode, event);            switch (keyCode) {                case KeyEvent.KEYCODE_DPAD_UP:                    if (focusID > 0) {                        focusID--;                    } else {                        focusID = 4;                    }                    this.invalidate();                    break;                case KeyEvent.KEYCODE_DPAD_DOWN:                    if (focusID < 4) {                        focusID++;                    } else {                        focusID = 0;                    }                    this.invalidate();                    break;                    //左键的响应                case KeyEvent.KEYCODE_DPAD_LEFT:                    if (focusID == 0) {                        if (Constants.A >-10) {                            Constants.A--;                            Constants.progressBarValueA--;                        } else {                            Constants.A = -10;                            Constants.progressBarValueA=6;                        }                        this.invalidate();                    } else if (focusID == 1) {                        if (Constants.B > -10) {                            Constants.B--;                            Constants.progressBarValueB--;                        } else {                            Constants.B = -10;                            Constants.progressBarValueB=6;                        }                        this.invalidate();                    }else if (focusID == 2) {                        if (Constants.C > -10) {                            Constants.C--;                            Constants.progressBarValueC--;                        } else {                            Constants.C = -10;                            Constants.progressBarValueC=6;                        }                        this.invalidate();                    }else if (focusID == 3) {                        if (Constants.D > -10) {                            Constants.D--;                            Constants.progressBarValueD--;                        } else {                            Constants.D = -10;                            Constants.progressBarValueD=6;                        }                        this.invalidate();                    }else if (focusID == 4) {                        if (Constants.E > -10) {                            Constants.E--;                            Constants.progressBarValueE--;                        } else {                            Constants.E = -10;                            Constants.progressBarValueE=6;                        }                        this.invalidate();                    }                    break;                 //右键的响应                case KeyEvent.KEYCODE_DPAD_RIGHT:                    if (focusID == 0) {                        if (Constants.A < (10-1)) {                            Constants.A++;                            Constants.progressBarValueA++;                        } else {                            Constants.A = 10;                            Constants.progressBarValueA = 26;                        }                        this.invalidate();                    } else if (focusID == 1) {                        if (Constants.B < (10 - 1)) {                            Constants.B++;                            Constants.progressBarValueB++;                        } else {                            Constants.B = 10;                            Constants.progressBarValueB = 26;                        }                        this.invalidate();                    }else if (focusID == 2) {                        if (Constants.C < (10 - 1)) {                            Constants.C++;                            Constants.progressBarValueC++;                        } else {                            Constants.C = 10;                            Constants.progressBarValueC = 26;                        }                        this.invalidate();                    }else if (focusID == 3) {                        if (Constants.D < (10 - 1)) {                            Constants.D++;                            Constants.progressBarValueD++;                        } else {                            Constants.D = 10;                            Constants.progressBarValueD = 26;                        }                        this.invalidate();                    }else if (focusID == 4) {                        if (Constants.E < (10 - 1)) {                            Constants.E++;                            Constants.progressBarValueE++;                        } else {                            Constants.E = 10;                            Constants.progressBarValueE = 26;                        }                        this.invalidate();                    }                    break;                                    //焦点的控制                    case KeyEvent.KEYCODE_ENTER:                    switch (focusID) {                        case 0:                            focusID = 1;                            break;                        case 1:                            focusID = 2;                            break;                        case 2:                            focusID = 3;                            break;                        case 3:                            focusID = 4;                            break;                        case 4:                            focusID = 0;                            break;                        default:                            break;                    }                    this.invalidate();                    break;                 //退出按键的响应                case KeyEvent.KEYCODE_BACK:                    ((ViewGroup) (this.getParent())).removeView(this);                    break;                default:                    break;            }            return true;        }                //做一个图片资源的集合        public void initData() {            progressBarMap = new HashMap<Integer, Integer>();            progressBarMap.put(1, R.drawable.sound_1);            progressBarMap.put(2, R.drawable.sound_2);            progressBarMap.put(3, R.drawable.sound_3);            progressBarMap.put(4, R.drawable.sound_4);            progressBarMap.put(5, R.drawable.sound_5);            progressBarMap.put(6, R.drawable.sound_6);            progressBarMap.put(7, R.drawable.sound_7);            progressBarMap.put(8, R.drawable.sound_8);            progressBarMap.put(9, R.drawable.sound_9);            progressBarMap.put(10, R.drawable.sound_10);            progressBarMap.put(11, R.drawable.sound_11);            progressBarMap.put(12, R.drawable.sound_12);            progressBarMap.put(13, R.drawable.sound_13);            progressBarMap.put(14, R.drawable.sound_14);            progressBarMap.put(15, R.drawable.sound_15);            progressBarMap.put(16, R.drawable.sound_16);            progressBarMap.put(17, R.drawable.sound_17);            progressBarMap.put(18, R.drawable.sound_18);            progressBarMap.put(19, R.drawable.sound_19);            progressBarMap.put(20, R.drawable.sound_20);            progressBarMap.put(21, R.drawable.sound_21);            progressBarMap.put(22, R.drawable.sound_22);            progressBarMap.put(23, R.drawable.sound_23);            progressBarMap.put(24, R.drawable.sound_24);            progressBarMap.put(25, R.drawable.sound_25);            progressBarMap.put(26, R.drawable.sound_26);            progressBarMap.put(27, R.drawable.sound_27);            progressBarMap.put(28, R.drawable.sound_28);            progressBarMap.put(29, R.drawable.sound_29);            progressBarMap.put(30, R.drawable.sound_30);            progressBarMap.put(31, R.drawable.sound_31);            progressBarMap.put(32, R.drawable.sound_32);        }            }  

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://127.0.0.1/wywyzy.html