package com.loaderman.customviewdemo;

import android.app.Activity;
import android.graphics.ColorMatrix;
import android.os.Bundle;
import android.util.Log; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ColorMatrix colorMatrix1 = new ColorMatrix(new float[]{
0.1f, 0.2f, 0.3f, 0.4f, 0.5f,
0, 1, 0, 0, 0,
0, 0, 1, 0, 0,
0, 0, 0, 1, 0,
}); ColorMatrix colorMatrix2 = new ColorMatrix(new float[]{
0.11f, 0, 0, 0, 0,
0, 0.22f, 0, 0, 0,
0, 0, 0.33f, 0, 0,
0, 0, 0, 0.44f, 0,
}); printSetConcat(colorMatrix1,colorMatrix2);
// printPreConcat(colorMatrix1,colorMatrix2);
// printPostConcat(colorMatrix1,colorMatrix2); } private void printPostConcat(ColorMatrix colorMatrix1, ColorMatrix colorMatrix2){
Log.d("loaderman",printArray(colorMatrix1.getArray()));
Log.d("loaderman",printArray(colorMatrix2.getArray()));
colorMatrix2.postConcat(colorMatrix1);
Log.d("loaderman",printArray(colorMatrix2.getArray()));
} private void printPreConcat(ColorMatrix colorMatrix1,ColorMatrix colorMatrix2){
Log.d("loaderman",printArray(colorMatrix1.getArray()));
colorMatrix1.preConcat(colorMatrix2);
Log.d("loaderman",printArray(colorMatrix2.getArray()));
Log.d("loaderman",printArray(colorMatrix1.getArray()));
} private void printSetConcat(ColorMatrix colorMatrix1,ColorMatrix colorMatrix2){
ColorMatrix resultMatrix = new ColorMatrix(new float[]{
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
});
resultMatrix.setConcat(colorMatrix1,colorMatrix2); Log.d("loaderman",printArray(colorMatrix1.getArray()));
Log.d("loaderman",printArray(colorMatrix2.getArray()));
Log.d("loaderman",printArray(resultMatrix.getArray()));
} private String printArray(float[] array){
StringBuilder builder = new StringBuilder("array dump:\n");
for (int i=0;i<array.length;i++){
if (i%5==0){
builder.append("\n");
}
builder.append(array[i]+" ");
}
return builder.toString();
} }
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#ffffff"
android:layout_width="fill_parent"
android:layout_height="fill_parent"> <com.loaderman.customviewdemo.MyView
android:layout_width="match_parent"
android:layout_height="match_parent"/> </LinearLayout>
package com.loaderman.customviewdemo;

import android.content.Context;
import android.graphics.*;
import android.util.AttributeSet;
import android.view.View; public class MyView extends View {
private Paint mPaint = new Paint();
private Bitmap bitmap;// 位图 public MyView(Context context, AttributeSet attrs) {
super(context, attrs); mPaint.setAntiAlias(true);
// 获取位图
bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.dog);
} @Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas); // drawRect(canvas);
drawBitmap(canvas);
} private void drawRect(Canvas canvas){
mPaint.setARGB(255,200,100,100);
// 绘制原始位图
canvas.drawRect(0,0,500,600,mPaint); canvas.translate(550,0);
// 生成色彩矩阵
ColorMatrix colorMatrix = new ColorMatrix(new float[]{
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 1, 0, 0,
0, 0, 0, 1, 0,
});
mPaint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
canvas.drawRect(0,0,500,600,mPaint);
} private void drawBitmap(Canvas canvas){
canvas.drawBitmap(bitmap, null, new Rect(0, 0, 500, 500 * bitmap.getHeight() / bitmap.getWidth()), mPaint); canvas.save();
canvas.translate(510, 0);
// 生成色彩矩阵
// ColorMatrix colorMatrix = new ColorMatrix(new float[]{
// 0.213f, 0.715f, 0.072f, 0, 0,
// 0.213f, 0.715f, 0.072f, 0, 0,
// 0.213f, 0.715f, 0.072f, 0, 0,
// 0, 0, 0, 1, 0,
// });
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.setScale(1,1.3f,1,1);
mPaint.setColorFilter(new ColorMatrixColorFilter(colorMatrix)); canvas.drawBitmap(bitmap, null, new Rect(0, 0, 500, 500 * bitmap.getHeight() / bitmap.getWidth()), mPaint); canvas.restore();
// canvas.translate(0,500);
// ColorMatrix colorMatrix2 = new ColorMatrix(new float[]{
// 0.3086f, 0.6094f, 0.0820f, 0, 0,
// 0.3086f, 0.6094f, 0.0820f, 0, 0,
// 0.3086f, 0.6094f, 0.0820f, 0, 0,
// 0 , 0 , 0 , 1, 0
// });
//
// mPaint.setColorFilter(new ColorMatrixColorFilter(colorMatrix2));
// canvas.drawBitmap(bitmap, null, new Rect(0, 0, 500, 500 * bitmap.getHeight() / bitmap.getWidth()), mPaint);
//
// canvas.translate(510,0);
// ColorMatrix colorMatrix3 = new ColorMatrix(new float[]{
// 0, 0, 0, 0, 0,
// 0, 0, 0, 0, 0,
// 0, 0, 1, 0, 0,
// 0, 0, 0, 1, 0,
// });
// mPaint.setColorFilter(new ColorMatrixColorFilter(colorMatrix3));
// canvas.drawBitmap(bitmap, null, new Rect(0, 0, 500, 500 * bitmap.getHeight() / bitmap.getWidth()), mPaint); }
}

效果图:

最新文章

  1. processModel与ASP.NET进程模型
  2. DirectShowLib directshownet 视频
  3. How Tomcat Works(十三)
  4. text输入框中按下enter键时阻止刷新页面
  5. 自己实现的库函数1(strlen,strcpy,strcmp,strcat)
  6. Android用户界面 UI组件--TextView及其子类(五) DigitalClock,AnalogClock,RadioButton,CheckBox,ToggleButton汇总
  7. codebook法分割前景目标
  8. Dos关闭进程命令
  9. MySQL的常用SQL语句.md
  10. eclipse集成dorado5插件
  11. Linux0.11进程切换和TSS结构
  12. dos命令 创建数据库,建表,两表联查,三表联查(mysql---第一篇)
  13. I/O 流
  14. 14-background
  15. [PHP] 数据结构-链表创建-插入-删除-查找的PHP实现
  16. react的super(props)
  17. Win10系列:C#应用控件基础3
  18. java程序中中常用到的linux操作
  19. 【工具引入】uiautomatorviewer 查找元素后自动生成代码
  20. wordpress必装的插件 wp最常用的十个插件

热门文章

  1. Vim使用技巧(5) -- 宏的录制与使用
  2. reverse函数的实现
  3. dt7.0百度熊掌当天主动推送方法
  4. nginx 环境 thinkphp 隐藏index.php
  5. js事件冒泡/捕获
  6. jQuery模拟键盘打字逐字逐句显示文本
  7. vue app.xxx.js 较大问题
  8. ElementUI 之 Cascader 级联选择器指定 value label
  9. 008_硬件基础电路_RC消火花电路分析方法和思路
  10. HTML 文字垂直剧中