1. 图形的缩放

(1)布局文件activity_main.xml如下:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
tools:context="com.himi.bitmapdemo.MainActivity" > <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iv_src" />
<ImageView
android:layout_marginTop="10dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iv_copyed" /> </LinearLayout>

(2)MainActivity.java,如下:

 package com.himi.bitmapdemo;

 import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.os.Bundle;
import android.widget.ImageView; public class MainActivity extends Activity {
private ImageView iv_src;
private ImageView iv_copyed; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv_src = (ImageView) findViewById(R.id.iv_src);
iv_copyed = (ImageView) findViewById(R.id.iv_copyed);
// 设置原图
Bitmap bitmapSrc = BitmapFactory.decodeResource(getResources(),
R.drawable.img01);
iv_src.setImageBitmap(bitmapSrc); // 对原图进行 缩放,创建一个原图的拷贝(副本),不能直接对原图变化
// 获取原图的纸张类型
Config config = bitmapSrc.getConfig();
// 创建的副本,里面的内容是空白的,画布
Bitmap alertBitmap = Bitmap.createBitmap(bitmapSrc.getWidth(),
bitmapSrc.getHeight(), config);
// 以alertBitmap大小为模板创建一个画板
Canvas canvas = new Canvas(alertBitmap);
// 创建画笔
Paint paint = new Paint();
// 画笔的默认颜色
paint.setColor(Color.BLACK);
// 第一参数是临摹的图片
canvas.drawBitmap(bitmapSrc, new Matrix(), paint);
iv_copyed.setImageBitmap(alertBitmap);
} }

 new Matrix默认是1:1缩放比例,上面代码获取的是1:1的图片,效果如下:

如果我们想缩小图片大小为原来2倍,就可以直接这样修改代码,如下:

MainActivity.java,如下:

 package com.himi.bitmapdemo;

 import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.os.Bundle;
import android.widget.ImageView; public class MainActivity extends Activity {
private ImageView iv_src;
private ImageView iv_copyed; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv_src = (ImageView) findViewById(R.id.iv_src);
iv_copyed = (ImageView) findViewById(R.id.iv_copyed);
// 设置原图
Bitmap bitmapSrc = BitmapFactory.decodeResource(getResources(),
R.drawable.img01);
iv_src.setImageBitmap(bitmapSrc); // 对原图进行 缩放,创建一个原图的拷贝(副本),不能直接对原图变化
// 获取原图的纸张类型
Config config = bitmapSrc.getConfig();
// 创建的副本,里面的内容是空白的,画布
Bitmap alertBitmap = Bitmap.createBitmap(bitmapSrc.getWidth()*2,
bitmapSrc.getHeight()*2, config);
// 以alertBitmap大小为模板创建一个画板
Canvas canvas = new Canvas(alertBitmap);
// 创建画笔
Paint paint = new Paint();
// 画笔的默认颜色
paint.setColor(Color.BLACK);
// 第一参数是临摹的图片
Matrix matrix = new Matrix();
matrix.setScale(2.0f, 2.0f);
canvas.drawBitmap(bitmapSrc, matrix , paint);
iv_copyed.setImageBitmap(alertBitmap);
} }

运行效果如下:

如果我们想缩小图片大小为原来一半,就可以直接这样修改代码,如下:

MainActivity.java,如下:

 package com.himi.bitmapdemo;

 import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.os.Bundle;
import android.widget.ImageView; public class MainActivity extends Activity {
private ImageView iv_src;
private ImageView iv_copyed; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv_src = (ImageView) findViewById(R.id.iv_src);
iv_copyed = (ImageView) findViewById(R.id.iv_copyed);
// 设置原图
Bitmap bitmapSrc = BitmapFactory.decodeResource(getResources(),
R.drawable.img01);
iv_src.setImageBitmap(bitmapSrc); // 对原图进行 缩放,创建一个原图的拷贝(副本),不能直接对原图变化
// 获取原图的纸张类型
Config config = bitmapSrc.getConfig();
// 创建的副本,里面的内容是空白的,画布
Bitmap alertBitmap = Bitmap.createBitmap(bitmapSrc.getWidth()/2,
bitmapSrc.getHeight()/2, config);
// 以alertBitmap大小为模板创建一个画板
Canvas canvas = new Canvas(alertBitmap);
// 创建画笔
Paint paint = new Paint();
// 画笔的默认颜色
paint.setColor(Color.BLACK);
// 第一参数是临摹的图片
Matrix matrix = new Matrix();
matrix.setScale(0.5f, 0.5f);
canvas.drawBitmap(bitmapSrc, matrix , paint);
iv_copyed.setImageBitmap(alertBitmap);
} }

运行的效果如下:

2. 图形的旋转

承接上面的代码,修改MainActivity.java,如下:

 package com.himi.bitmapdemo;

 import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.os.Bundle;
import android.widget.ImageView; public class MainActivity extends Activity {
private ImageView iv_src;
private ImageView iv_copyed; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv_src = (ImageView) findViewById(R.id.iv_src);
iv_copyed = (ImageView) findViewById(R.id.iv_copyed);
// 设置原图
Bitmap bitmapSrc = BitmapFactory.decodeResource(getResources(),
R.drawable.img01);
iv_src.setImageBitmap(bitmapSrc); // 对原图进行 缩放,创建一个原图的拷贝(副本),不能直接对原图变化
// 获取原图的纸张类型
Config config = bitmapSrc.getConfig();
// 创建的副本,里面的内容是空白的,画布
Bitmap alertBitmap = Bitmap.createBitmap(bitmapSrc.getWidth(),
bitmapSrc.getHeight(), config);
// 以alertBitmap大小为模板创建一个画板
Canvas canvas = new Canvas(alertBitmap);
// 创建画笔
Paint paint = new Paint();
// 画笔的默认颜色
paint.setColor(Color.BLACK);
// 第一参数是临摹的图片
Matrix matrix = new Matrix();
//默认以画布左上角为旋转原点,旋转30°
//matrix.setRotate(30);
//以画布中心点为旋转原点,旋转30°
matrix.setRotate(, bitmapSrc.getWidth()/2, bitmapSrc.getHeight()/2);
canvas.drawBitmap(bitmapSrc, matrix , paint);
iv_copyed.setImageBitmap(alertBitmap);
} }

效果如下:

3. 图形的平移

MainActivity代码如下:

 package com.himi.bitmapdemo;

 import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.os.Bundle;
import android.widget.ImageView; public class MainActivity extends Activity {
private ImageView iv_src;
private ImageView iv_copyed; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv_src = (ImageView) findViewById(R.id.iv_src);
iv_copyed = (ImageView) findViewById(R.id.iv_copyed);
// 设置原图
Bitmap bitmapSrc = BitmapFactory.decodeResource(getResources(),
R.drawable.img01);
iv_src.setImageBitmap(bitmapSrc); // 对原图进行 缩放,创建一个原图的拷贝(副本),不能直接对原图变化
// 获取原图的纸张类型
Config config = bitmapSrc.getConfig();
// 创建的副本,里面的内容是空白的,画布
Bitmap alertBitmap = Bitmap.createBitmap(bitmapSrc.getWidth(),
bitmapSrc.getHeight(), config);
// 以alertBitmap大小为模板创建一个画板
Canvas canvas = new Canvas(alertBitmap);
// 创建画笔
Paint paint = new Paint();
// 画笔的默认颜色
paint.setColor(Color.BLACK);
// 第一参数是临摹的图片
Matrix matrix = new Matrix();
matrix.setTranslate(0, 50);
canvas.drawBitmap(bitmapSrc, matrix , paint);
iv_copyed.setImageBitmap(alertBitmap);
} }

运行效果如下:图形竖直方向向下平移了50像素点(手机坐标系是水平向右为正,竖直向下为正)

4. 图形的倒影效果:

分析:在画布上,先把图片的向下翻转,但是这样会跳出画布(不能显示),在让图形整体向上移动,移动距离为图形的高度。

MainActivity.java,如下:

 package com.himi.bitmapdemo;

 import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.os.Bundle;
import android.widget.ImageView; public class MainActivity extends Activity {
private ImageView iv_src;
private ImageView iv_copyed; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv_src = (ImageView) findViewById(R.id.iv_src);
iv_copyed = (ImageView) findViewById(R.id.iv_copyed);
// 设置原图
Bitmap bitmapSrc = BitmapFactory.decodeResource(getResources(),
R.drawable.img01);
iv_src.setImageBitmap(bitmapSrc); // 对原图进行 缩放,创建一个原图的拷贝(副本),不能直接对原图变化
// 获取原图的纸张类型
Config config = bitmapSrc.getConfig();
// 创建的副本,里面的内容是空白的,画布
Bitmap alertBitmap = Bitmap.createBitmap(bitmapSrc.getWidth(),
bitmapSrc.getHeight(), config);
// 以alertBitmap大小为模板创建一个画板
Canvas canvas = new Canvas(alertBitmap);
// 创建画笔
Paint paint = new Paint();
// 画笔的默认颜色
paint.setColor(Color.BLACK);
// 第一参数是临摹的图片
Matrix matrix = new Matrix();
matrix.setScale(1,-1); //图形向下翻转
//post是在上一次变化的基础上进行新的变化,set设置一个新的变化,会把原来的设置给覆盖
matrix.postTranslate(0, bitmapSrc.getHeight());//图形整体向上移动
canvas.drawBitmap(bitmapSrc, matrix , paint);
iv_copyed.setImageBitmap(alertBitmap);
} }

运行效果如下:

5. 图形镜面效果:

分析:在画布上,先把图片的向左翻转,但是这样会跳出画布(不能显示),在让图形整体向右移动,移动距离为图形的宽度。

MainActivity.java,如下:

 package com.himi.bitmapdemo;

 import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.os.Bundle;
import android.widget.ImageView; public class MainActivity extends Activity {
private ImageView iv_src;
private ImageView iv_copyed; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv_src = (ImageView) findViewById(R.id.iv_src);
iv_copyed = (ImageView) findViewById(R.id.iv_copyed);
// 设置原图
Bitmap bitmapSrc = BitmapFactory.decodeResource(getResources(),
R.drawable.img01);
iv_src.setImageBitmap(bitmapSrc); // 对原图进行 缩放,创建一个原图的拷贝(副本),不能直接对原图变化
// 获取原图的纸张类型
Config config = bitmapSrc.getConfig();
// 创建的副本,里面的内容是空白的,画布
Bitmap alertBitmap = Bitmap.createBitmap(bitmapSrc.getWidth(),
bitmapSrc.getHeight(), config);
// 以alertBitmap大小为模板创建一个画板
Canvas canvas = new Canvas(alertBitmap);
// 创建画笔
Paint paint = new Paint();
// 画笔的默认颜色
paint.setColor(Color.BLACK);
// 第一参数是临摹的图片
Matrix matrix = new Matrix();
matrix.setScale(-1,1);
//post是在上一次变化的基础上进行新的变化,set设置一个新的变化,会把原来的设置给覆盖
matrix.postTranslate(bitmapSrc.getWidth(), 0);
canvas.drawBitmap(bitmapSrc, matrix , paint);
iv_copyed.setImageBitmap(alertBitmap);
} }

运行效果如下:

6. 图形透明度变化效果:

MainActivity.java,如下:

 package com.himi.bitmapdemo;

 import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.os.Bundle;
import android.widget.ImageView; public class MainActivity extends Activity {
private ImageView iv_src;
private ImageView iv_copyed; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv_src = (ImageView) findViewById(R.id.iv_src);
iv_copyed = (ImageView) findViewById(R.id.iv_copyed);
// 设置原图
Bitmap bitmapSrc = BitmapFactory.decodeResource(getResources(),
R.drawable.img01);
iv_src.setImageBitmap(bitmapSrc); // 对原图进行 缩放,创建一个原图的拷贝(副本),不能直接对原图变化
// 获取原图的纸张类型
Config config = bitmapSrc.getConfig();
// 创建的副本,里面的内容是空白的,画布
Bitmap alertBitmap = Bitmap.createBitmap(bitmapSrc.getWidth(),
bitmapSrc.getHeight(), config);
// 以alertBitmap大小为模板创建一个画板
Canvas canvas = new Canvas(alertBitmap);
// 创建画笔
Paint paint = new Paint();
//设置画笔的透明度
paint.setAlpha(80);
// 画笔的默认颜色
//paint.setColor(Color.BLACK); //这里必须注释掉这个设置画布默认颜色
// 第一参数是临摹的图片
Matrix matrix = new Matrix(); canvas.drawBitmap(bitmapSrc, matrix , paint);
iv_copyed.setImageBitmap(alertBitmap);
} }

运行效果如下:

Android(java)学习笔记180:多媒体之图形的变化处理的更多相关文章

  1. java学习笔记(二)图形用户接口

    这个学期主要放在ACM比赛上去了,比赛结束了.不知不觉就15周了,这周就要java考试了,复习一下java吧.java的学习的目的还是让我们学以致用,让我们可以运用java开发一下小项目.而不是单单应 ...

  2. Android 数字签名学习笔记

    Android 数字签名学习笔记 在Android系统中,所有安装到系统的应用程序都必有一个数字证书,此数字证书用于标识应用程序的作者和在应用程序之间建立信任关系,如果一个permission的pro ...

  3. Java学习笔记4

    Java学习笔记4 1. JDK.JRE和JVM分别是什么,区别是什么? 答: ①.JDK 是整个Java的核心,包括了Java运行环境.Java工具和Java基础类库. ②.JRE(Java Run ...

  4. 0028 Java学习笔记-面向对象-Lambda表达式

    匿名内部类与Lambda表达式示例 下面代码来源于:0027 Java学习笔记-面向对象-(非静态.静态.局部.匿名)内部类 package testpack; public class Test1{ ...

  5. 《Java学习笔记(第8版)》学习指导

    <Java学习笔记(第8版)>学习指导 目录 图书简况 学习指导 第一章 Java平台概论 第二章 从JDK到IDE 第三章 基础语法 第四章 认识对象 第五章 对象封装 第六章 继承与多 ...

  6. Android动画学习笔记-Android Animation

    Android动画学习笔记-Android Animation   3.0以前,android支持两种动画模式,tween animation,frame animation,在android3.0中 ...

  7. Java学习笔记:语言基础

    Java学习笔记:语言基础 2014-1-31   最近开始学习Java,目的倒不在于想深入的掌握Java开发,而是想了解Java的基本语法,可以阅读Java源代码,从而拓展一些知识面.同时为学习An ...

  8. 【Java学习笔记之二十六】深入理解Java匿名内部类

    在[Java学习笔记之二十五]初步认知Java内部类中对匿名内部类做了一个简单的介绍,但是内部类还存在很多其他细节问题,所以就衍生出这篇博客.在这篇博客中你可以了解到匿名内部类的使用.匿名内部类要注意 ...

  9. java学习笔记16--I/O流和文件

    本文地址:http://www.cnblogs.com/archimedes/p/java-study-note16.html,转载请注明源地址. IO(Input  Output)流 IO流用来处理 ...

  10. java学习笔记7--抽象类与抽象方法

    接着前面的学习: java学习笔记6--类的继承.Object类 java学习笔记5--类的方法 java学习笔记4--类与对象的基本概念(2) java学习笔记3--类与对象的基本概念(1) jav ...

随机推荐

  1. 并不对劲的bzoj5475:loj2983:p5206:[wc2019]数树

    题目大意 task0:有两棵\(n\)(n\leq10^5)个点的树\(T1,T2\),每个点的点权可以是一个在\([1,y]\)里的数,如果两个点既在\(T1\)中有直接连边,又在\(T2\)中有直 ...

  2. codeforces round 421 div2 补题 CF 820 A-E

    A Mister B and Book Reading  O(n)暴力即可 #include<bits/stdc++.h> using namespace std; typedef lon ...

  3. zepto.js 总结

    zepto.js 中的注意事项 ,详见:http://www.cnblogs.com/samwu/archive/2013/06/06/3121649.html zepto被弃用的原因:详见:http ...

  4. View Programming Guide for iOS ---- iOS 视图编程指南(二)---View and Window Architecture

    View and Window Architecture 视图和窗口架构 Views and windows present your application’s user interface and ...

  5. Linux系统安装完的调整和安全

    精简开机系统自启动 •五个企业环境中开机自启动的服务; sshd:远程连接linux服务器必须开启 rsyslog:日志相关软件 network:网络服务 crond:系统和用户配置的计划任务周期性进 ...

  6. 网络爬虫之Selenium模块和Xpath表达式+Lxml解析库的使用

    实际生产环境下,我们一般使用lxml的xpath来解析出我们想要的数据,本篇博客将重点整理Selenium和Xpath表达式,关于CSS选择器,将另外再整理一篇! 一.介绍: selenium最初是一 ...

  7. Python学习之旅—生成器对象的send方法详解

    前言 在上一篇博客中,笔者带大家一起探讨了生成器与迭代器的本质原理和使用,本次博客将重点聚焦于生成器对象的send方法. 一.send方法详解  我们知道生成器对象本质上是一个迭代器.但是它比迭代器对 ...

  8. iView 实战系列教程(21课时)_1.iView 实战教程之配置篇

    1.iView 实战教程之配置篇 点击添加插件,. 选中后安装 全部导入还是按需导入. 2.是否需要自定义主题变量 3.多语言的设置. 这里我们全部选择为默认 然后点击继续. 启动项目 入口文件导入了 ...

  9. J20170403-gg

    うっすら 微微的,薄薄的 グラデーション 渐变 ぼかし(暈し) 晕色 由浓到淡渐变上色的东西 シャドウ 影子,阴影 ドメイン 域名  サブドメイン 子域名

  10. bzoj 3745: [Coci2015]Norma【分治】

    参考:https://blog.csdn.net/lych_cys/article/details/51203960 真的不擅长这种-- 分治,对于一个(l,r),先递归求出(l,mid),(mid+ ...