码迷,mamicode.com
首页 > 移动开发 > 详细

android中的图片处理

时间:2016-07-19 20:37:37      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:

大图片处理


大图片处理是将原来像素高的转换为像素低的图片,比如原来图片是1024*768的,而手机屏幕是800*600的,这时候就需要进行转换。转换的方式很简单就是等比例缩放。

package xidian.dy.com.chujia;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.Display;
import android.view.View;
import android.widget.ImageView;

import java.io.File;

public class MainActivity extends AppCompatActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    public void load(View v){
        BitmapFactory.Options options = new BitmapFactory.Options();
        File file = new File(Environment.getExternalStorageDirectory(), "a.dog");
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(file.toString(), options);
        //获取图片宽高
        int imgWidth = options.outWidth;
        int imgHeight = options.outHeight;
        //获取屏幕宽高
        Display dsp = getWindowManager().getDefaultDisplay();
        int screanWidth = dsp.getWidth();
        int screanHeight = dsp.getHeight();
        //计算缩放比例
        int scaleWidth = imgWidth / screanWidth;
        int scaleHeight = imgHeight / screanHeight;
        int scale = scaleHeight >= scaleWidth ? scaleHeight:scaleWidth;
        if(scale < 1)
            scale = 1;
        //设置缩放比例(scale应大于1缩放才能其作用)
        options.inSampleSize = scale;
        options.inJustDecodeBounds = false;
        Bitmap bm = BitmapFactory.decodeFile(file.toString(), options);

        ImageView img = (ImageView) findViewById(R.id.iv);
        if(img != null)
            img.setImageBitmap(bm);
    }
}

图片副本


创建图片副本是为了对图片进行修改,下面的代码将图片进行了水平反转

package xidian.dy.com.chujia;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Bitmap bm = BitmapFactory.decodeFile("/sdcard/a.dog");
        //创建一个与原图等宽高的bitmap对象,可以看作是画纸
        Bitmap bmcpy = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(),bm.getConfig());

        //创建画笔对象
        Paint paint = new Paint();
        //创建画板对象,将画纸铺在画板上
        Canvas canvas = new Canvas(bmcpy);
        //开始作画
        //将图片水平翻转
        Matrix mt = new Matrix();
        mt.setScale(-1, 1);
        mt.postTranslate(bmcpy.getWidth(), 0);
        canvas.drawBitmap(bm, mt, paint);
        ImageView iv = (ImageView) findViewById(R.id.iv2);
        iv.setImageBitmap(bmcpy);
        ImageView iv1 = (ImageView) findViewById(R.id.iv1);
        iv1.setImageBitmap(bm);
    }
}

 

android中的图片处理

标签:

原文地址:http://www.cnblogs.com/xidongyu/p/5685796.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!