android中的图片处理
大图片处理
大图片处理是将原来像素高的转换为像素低的图片,比如原来图片是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); } }
画画
让用户在画板上作画
package xidian.dy.com.chujia; import android.content.Intent; 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.net.Uri; import android.os.Bundle; import android.os.Environment; import android.support.v7.app.AppCompatActivity; import android.view.MotionEvent; import android.view.View; import android.widget.ImageView; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; public class MainActivity extends AppCompatActivity { int preX,preY; Bitmap bmcpy; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.bg); bmcpy = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), bm.getConfig()); final Paint paint = new Paint(); final Canvas canvas = new Canvas(bmcpy); //设置画笔的颜色为红色 paint.setColor(Color.RED); canvas.drawBitmap(bm, new Matrix(), paint); final ImageView iv = (ImageView) findViewById(R.id.iv); iv.setImageBitmap(bmcpy); if(iv != null) iv.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { int action = event.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: preX = (int) event.getX(); preY = (int) event.getY(); break; case MotionEvent.ACTION_UP: break; case MotionEvent.ACTION_MOVE: int x = (int) event.getX(); int y = (int) event.getY(); canvas.drawLine(preX, preY, x, y, paint); iv.setImageBitmap(bmcpy); preX = x; preY = y; break; } return true; } }); } //将画好的图片进行保存 public void save(){ File file = new File("/sdcard/tmp.png"); try { FileOutputStream fos = new FileOutputStream(file); bmcpy.compress(Bitmap.CompressFormat.PNG,10,fos); Intent intent = new Intent(); //发出SDcard挂载广播来刷新MediaStore数据库 intent.setAction(Intent.ACTION_MEDIA_MOUNTED); intent.setData(Uri.fromFile(Environment.getExternalStorageDirectory())); startActivity(intent); } catch (FileNotFoundException e) { e.printStackTrace(); } } }