图片的颜色变化

<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"
    tools:context=".MainActivity" >

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="click"
        android:text="拷贝一个位图" />

    <ImageView
        android:id="@+id/iv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ImageView
        android:id="@+id/iv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>
package com.itheima.copybitmap;

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.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends Activity {
	private ImageView iv1,iv2;
	private Bitmap alterBitmap;
	private Bitmap srcBmp;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		iv1 = (ImageView) findViewById(R.id.iv1);
		iv2 = (ImageView) findViewById(R.id.iv2);
		//给第一个imageview默认设置一个位图
		srcBmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
		iv1.setImageBitmap(srcBmp);
		//创建原图的一个副本。 可修改  创建的是一个空白的图形。
		alterBitmap = Bitmap.createBitmap(srcBmp.getWidth()*2, srcBmp.getHeight()*2,srcBmp.getConfig());
	}
	/**
	 * 创建原图 bm的一个拷贝。副本
	 * @param view
	 */
	public void click(View view){
		//1.准备一个画板  在上面放上准备好的 空白的位图
		Canvas canvas = new Canvas(alterBitmap);
		//2.准备一个画笔
		Paint paint = new Paint();
		paint.setColor(Color.BLACK);
		//3.画画
		Matrix m = new Matrix();

		//ColorMatrix是颜色矩阵
		ColorMatrix cm = new ColorMatrix();
		cm.set(new float[] {
		0.5f, 0, 0, 0, 0,
		0, 0.8f, 0, 0, 0,
		0, 0, 0.6f, 0, 0,
		0, 0, 0, 1, 0
		});
		paint.setColorFilter(new ColorMatrixColorFilter(cm));

		//这行代码要在颜色矩阵后面才有效
		canvas.drawBitmap(srcBmp, m, paint);

		iv2.setImageBitmap(alterBitmap);//把原图的副本设置到界面上。
	}
}
时间: 2024-08-09 10:38:30

图片的颜色变化的相关文章

使用CSS3滤镜让图片反转颜色

CSS提供的滤镜也是一大亮点,我一直痴迷其中,有些滤镜的效果很有用,可是有些的滤镜效果可能只是为了玩玩儿,CSS常见的滤镜有这些:grayscale, blur, sepia,所有常见的过滤器.但是如何使用和转化图片呢?今天我们主要是来讲讲如何使用CSS3滤镜让图片反转颜色. CSS代码 invert滤镜就是为了设置元素的反色效果,他的值设置范围为:0-100%,100%为完全反色,0为显示正常的颜色. .normal {     filter: invert(0%); } .inverted

【原创】Android 4.4前后版本读取图库图片方式的变化

Android 4.4前后版本读取图库图片方式的变化 本文讲述Android 4.4(KitKat)前后访问图库以及访问后通过图片路径读取图片的变化 Android 4.4(KitKat)以前: 访问图库(方法一): 1 /** 2 * Access the gallery to pick up an image. 3 */ 4 private void startPickPhotoActivity() { 5 Intent intent = new Intent(Intent. ACTION_

iOS开发--QQ音乐练习,歌词的展示,歌词的滚动,歌词的颜色变化

一.歌词的展示 -- 首先歌词是在scrollView上,scrollView的大小是两个屏幕的宽度 scrollView滚动修改透明度的代码                                                             自定义展示歌词的view,继承自UIScrollView,向外界提供一个歌词文件名的属性 /** 歌词文件的名字 */ @property(nonatomic,copy) NSString *lrcFileName; 重写setter,

改变图片的颜色

定义 #import <UIKit/UIKit.h> @interface UIImage (ChangeImageColor) /** * 改变图片的颜色 * * @param tintColor <#tintColor description#> * * @return <#return value description#> */ - (UIImage *) imageWithTintColor:(UIColor *)tintColor; @end 实现 - (U

鼠标经过时背景颜色变化

<style type="text/css">#coolmenu{border: 1px solid black;border-bottom-width: 0;width: 170px;background-color: #E6E6E6;}* html #coolmenu{width: 164px;}#coolmenu a{font: bold 13px Verdana;padding: 2px;padding-left: 4px;display: block;width:

Android记住按钮选择颜色变化状态

为以后可以温习自己做过的一些特效例子,同时也没有必要再一次重写代码,特此从项目中收集代码,按钮颜色变化状态如下: 要实现如图效果,首先要编写一个布局xml文件,代码如下: <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:background="#ffffff" android:orientation="ho

W3C 事件切换 颜色变化

颜色变化代码: HTML: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" type="text/css" href="web6.css"> <scrip

代码设置文本点击按下颜色变化

代码设置文本点击按下颜色变化 [html] view plaincopy <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:color="

IOS7以后无需自定义,改变UITabbarItem的图片文字颜色

在IOS7以前,UITabbarItem的图片都是被固定渲染为蓝色,想要改变UITabbarItem的图片颜色就必须要自定义,在IOS7以后,得到了更新,方便大家自己去设定颜色,下面给出代码! 1.创建UITabbarItem的默认图片和选中图片 //第一个界面 ChildViewController *childvc = [[ChildViewController alloc]initWithNibName:nil bundle:nil]; UINavigationController *ch