android开发步步为营之81:android图片处理技术之一(截取与缩放)
来源:互联网 发布:视频转码软件 编辑:程序博客网 时间:2024/05/12 23:25
项目需要最近研究了一下图片处理的相关技术,本次分享一下截取与缩放的技术。效果图如下:
代码如下:
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); }}
1 0
- android开发步步为营之81:android图片处理技术之一(截取与缩放)
- android开发步步为营之83:android图片处理技术之二(通过Matrix移动缩放旋转错切)
- android 图片处理(截取,缩放)
- android 图片处理(截取,缩放)
- android 图片处理(截取,缩放)
- android 图片处理(截取,缩放)
- android开发步步为营之90:android图片处理技术之三(Gradient渐变图片的绘制)
- android开发步步为营之56:Android开发常见问题技术点总结之一
- android开发步步为营之66:android图片选取
- android开发步步为营之93:android自定义view开发之一(验证码生成器)
- android开发步步为营之43:listview的使用(下篇:显示图片和按钮)
- android开发步步为营之60:IntentService与Service的区别
- android开发步步为营之73:Android开发常见问题技术点总结之二
- 图片截取与缩放
- android之图片截取
- android开发步步为营之9:反编译apk
- android开发步步为营之14:App Widgets
- android开发步步为营之20:网络设置
- Spring4.1.1 + Quartz定时任务配置
- onHiddenChanged
- comment_templates.xml
- Spring集成Hibernate
- poj 2488 dfs+回溯
- android开发步步为营之81:android图片处理技术之一(截取与缩放)
- PostgreSQL的依赖约束(系统表pg_depend和pg_constraint)详解
- 写给c++初学者 之一
- 引用第三方jar包,找不到类库
- MySQL错误代码大全
- frame和iframe的区别 .
- 6-VC Bound(P[BAD]的真正上限)
- ARM 环境配置
- 中星微zc0301pl摄像头驱动移植