Android项目开发经验汇总

1、图片byte数据转Bitmap

使用Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath())容易导致内存溢出。

public static Bitmap decodeImg(byte[] imgByte) {
        Bitmap bitmap = null;

        InputStream input = null;
        try {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 8;
            input = new ByteArrayInputStream(imgByte);
            SoftReference softRef = new SoftReference(BitmapFactory.decodeStream(input, null, options));
            bitmap = (Bitmap) softRef.get();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (imgByte != null) {
                imgByte = null;
            }

            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return bitmap;
    }

2、使用Timer+TimerTask+Handler实现定时器

public class HanderDemoActivity extends Activity {  
    TextView tvShow;  
    private int i = 0;  
    private int TIME = 1000;  
  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
        tvShow = (TextView) findViewById(R.id.tv_show);  
        timer.schedule(task, 1000, 1000); // 1s后执行task,经过1s再次执行  
    }  
  
    Handler handler = new Handler() {  
        public void handleMessage(Message msg) {  
            if (msg.what == 1) {  
                tvShow.setText(Integer.toString(i++));  
            }  
            super.handleMessage(msg);  
        };  
    };  
    Timer timer = new Timer();  
    TimerTask task = new TimerTask() {  
  
        @Override  
        public void run() {  
            // 需要做的事:发送消息  
            Message message = new Message();  
            message.what = 1;  
            handler.sendMessage(message);  
        }  
    };  
}  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值