使用 Matrix控制图片的变换
2016-06-25 13:26
302 查看
自定义 View 代码
package com.test.matrixtest; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.drawable.BitmapDrawable; import android.util.AttributeSet; import android.view.KeyEvent; import android.view.View; /** * Created by Administrator on 2016/6/25. */ public class MyView extends View { private Bitmap mBitmap; //初始的图片资源 Matrix mMatrix = new Matrix(); // Matrix 实例化对象 private float sx = 0.0f; //设置初始 倾斜度 private float scale = 1.0f; //缩放比例 private int width , height; //位图的宽高 private boolean isScale = false; //判断是否为缩放的 标记 public MyView(Context context, AttributeSet attrs) { super(context, attrs); //获取位图 mBitmap = ((BitmapDrawable)context.getResources().getDrawable(R.mipmap.a)).getBitmap(); //获取位图的宽和高 width = mBitmap.getWidth(); height = mBitmap.getHeight(); //让当前View获得焦点 setFocusable(true); } @Override protected void onDraw(Canvas canvas) { //重置 matrix mMatrix.reset(); if (!isScale){ //旋转 matrix mMatrix.setSkew(sx, 0); }else{ //缩放 matrix mMatrix.setScale(scale,scale); } //根据原始位图和 Matrix 创建新图片 Bitmap newBitmap = Bitmap.createBitmap(mBitmap, 0, 0, width, height, mMatrix, true); //对新位图进行绘制 canvas.drawBitmap(newBitmap,mMatrix,null); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { switch (keyCode){ //向左倾斜 case KeyEvent.KEYCODE_A : isScale = false; sx += 0.1; postInvalidate(); //表示在非UI线程中 刷新 界面 break; //向右倾斜 case KeyEvent.KEYCODE_D : isScale = false; sx -= 0.1; postInvalidate(); //表示在非UI线程中 刷新 界面 break; //放大 case KeyEvent.KEYCODE_W : isScale = true; if (scale<2.0){ scale+=0.1; } postInvalidate(); //表示在非UI线程中 刷新 界面 break; //缩小 case KeyEvent.KEYCODE_S : isScale = true; if (scale>0.5){ scale -= 0.1; } postInvalidate(); //表示在非UI线程中 刷新 界面 break; } return super.onKeyDown(keyCode, event); } }
package com.test.matrixtest; import android.app.Activity; import android.os.Bundle; /** * 矩阵效果 Matrix */ public class MatrixActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_matrix); } }
布局文件
<?xml version="1.0" encoding="utf-8"?> <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" tools:context="com.test.matrixtest.MatrixActivity"> <com.test.matrixtest.MyView android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
相关文章推荐
- Java方法传参
- PPT常见问题及对策
- Ubuntu两台虚拟机之间Ping不同
- openstack组件使用的默认端口
- 无线通信原理及协议栈(ZigBee、蓝牙等)解析
- javascript运算符——关系运算符
- AC自动机
- hibernate的懒加载得到的代理对象里面有什么
- oppo 环信 java.lang.UnsatisfiedLinkError(带时效性)
- ACM 打字练习
- 推荐十四款常见的Web前端开发框架
- Git基本操作
- Centos SSH和FTP配置
- JAVA模拟内存分配与释放——首次适应算法
- linux +SQL 安装
- mac parallels desktop使用host-only配置虚拟机ip
- Linux进度条
- CentOS 7运维管理笔记(3)----Linux路由器配置
- ibatis动态sql配置启动时提示:The content of elements must consist of well-formed character data...
- 10个给力的在线Web设计开发工具介绍