android开发步步为营之81:android图片处理技术之一(截取与缩放)
2015-11-04 15:30
573 查看
项目需要最近研究了一下图片处理的相关技术,本次分享一下截取与缩放的技术。效果图如下:
代码如下:
代码如下:
package com.figo.study.activity; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Color; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.ImageView; import com.figo.study.R; import com.nostra13.universalimageloader.core.decode.BaseImageDecoder; public class BitmapApiActivity extends Activity { private ImageView ivTest, ivNew; private Button btnCut, btnScale; private String tag="BitmapApiActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_bitmap_api); ivTest = (ImageView) findViewById(R.id.img_test); ivNew = (ImageView) findViewById(R.id.img_new); btnCut = (Button) findViewById(R.id.btn_cut); btnCut.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { cutImage(); } }); btnScale = (Button) findViewById(R.id.btn_scale); //图片缩放 btnScale.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Bitmap bitmapSource = BitmapFactory.decodeResource(getResources(), R.mipmap.ab); Log.i(tag,"w="+bitmapSource.getWidth()+",h="+bitmapSource.getHeight()); //Bitmap bitmapNew = Bitmap.createScaledBitmap(bitmapSource, 200, 200 * bitmapSource.getHeight() / bitmapSource.getWidth(), true); Bitmap bitmapNew = Bitmap.createScaledBitmap(bitmapSource, 600, 600 * bitmapSource.getHeight() / bitmapSource.getWidth(), true); ivNew.setImageBitmap(bitmapNew); } }); } //从原图截取蒙版区域不透明部分的图片 private void cutImage() { Bitmap bitmapSource = BitmapFactory.decodeResource(getResources(), R.mipmap.ab); int w = bitmapSource.getWidth(); int h = bitmapSource.getHeight(); int[] pixelsSource = new int[w * h]; //获取原图所有像素 bitmapSource.getPixels(pixelsSource, 0, w, 0, 0, w, h); Bitmap bitmapMask = BitmapFactory.decodeResource(getResources(), R.mipmap.facetype_0); int w1 = bitmapMask.getWidth(); int h1 = bitmapMask.getHeight(); int[] pixels1 = new int[w1 * h1]; //获取蒙版所有像素 bitmapMask.getPixels(pixels1, 0, w1, 0, 0, w1, h1); //扣出蒙版区域的原图 蒙版宽高为120*120px,蒙版中心点与原图中心点重合 int total = w1 * h1; int xNew = (w - w1) / 2; int yNew = (h - h1) / 2; //参数为bitmap,x,y,w,h Bitmap bitmapSourceNew = Bitmap.createBitmap(bitmapSource, xNew, yNew, w1, h1); int[] sourceNew = new int[total]; bitmapSourceNew.getPixels(sourceNew, 0, w1, 0, 0, w1, h1); int[] targetNew = new int[total]; for (int i = 0; i < total; i++) { if (pixels1[i] != Color.TRANSPARENT) targetNew[i] = sourceNew[i]; } Bitmap output = Bitmap.createBitmap(targetNew, w1, h1, Bitmap.Config.ARGB_8888); //显示截取的图片 ivNew.setImageBitmap(output); } }
相关文章推荐
- Android Studio系列教程三--快捷键
- Android Studio系列教程二--基本设置与运行
- Android开发:Fragment不同操作的生命周期
- android ExpandableListView 四级目录结构
- 【Android学习笔记】Android概述与开发环境搭建
- Android源码编译
- Android:Layout_weight的深刻理解
- Android环境配置
- Android Studio 快捷键
- Android适配屏幕
- Android编程解析Json格式数据的方法
- Android MVC模式实例(三)
- Android客户端性能测试常见指标及测试方法--转载
- Android merge标签讲解与实例
- Android MVC模式实例(二)
- android studio首次运行出错
- Android客户端性能测试小结--转载
- Android动画的优化
- Android ViewTreeObserver
- Android编程中EditText限制文字输入的方法