package com.loaderman.customviewdemo;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //创建白色透明渐变图像
findViewById(R.id.bitmap_blank_gradient).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, BmpBlankGradientActivity.class));
}
}); //裁剪图像
findViewById(R.id.bitmap_crop).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cutImage();
}
}); //剪图像并用Matrix
findViewById(R.id.bitmap_crop_matrix).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cutImageMatrix();
}
}); //指定色彩创建图像
findViewById(R.id.bitmap_colors).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
createBmpByColors();
}
}); //createScaledBitmap
findViewById(R.id.bitmap_scaled).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
createScaledBitmap();
}
});
} //裁剪图像
private void cutImage() { Bitmap src = BitmapFactory.decodeResource(getResources(), R.drawable.dog);
Bitmap cutedBmp = Bitmap.createBitmap(src, src.getWidth() / 3, src.getHeight() / 3, src.getWidth() / 3, src
.getHeight() / 3); ImageView iv = (ImageView) findViewById(R.id.bmp_img);
iv.setImageBitmap(cutedBmp);
} //裁剪图像并用Matrix
private void cutImageMatrix() {
Matrix matrix = new Matrix();
matrix.setScale(2, 1); Bitmap src = BitmapFactory.decodeResource(getResources(), R.drawable.dog);
Bitmap cutedBmp = Bitmap.createBitmap(src, src.getWidth() / 3, src.getHeight() / 3, src.getWidth() / 3, src
.getHeight() / 3, matrix, true); ImageView iv = (ImageView) findViewById(R.id.bmp_img2);
iv.setImageBitmap(cutedBmp);
} //指定色彩创建图像
private void createBmpByColors() {
int width = 300, height = 200;
int[] colors = initColors(width, height);
Bitmap bmp = Bitmap.createBitmap(colors, width, height, Bitmap.Config.ARGB_8888); ImageView iv = (ImageView) findViewById(R.id.bmp_img);
iv.setImageBitmap(bmp);
} private int[] initColors(int width, int height) {
int[] colors = new int[width * height];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int r = x * 255 / (width - 1);
int g = y * 255 / (width - 1);
int b = 255 - Math.min(r, g);
int a = Math.max(r, g);
colors[y * width + x] = Color.argb(a, r, g, b);
}
}
return colors;
}
//缩放bitmap
private void createScaledBitmap() {
try {
Bitmap src = BitmapFactory.decodeResource(getResources(), R.drawable.scenery);
Bitmap bitmap = Bitmap.createScaledBitmap(src, 300, 200, true); ImageView iv = (ImageView) findViewById(R.id.bmp_img);
iv.setImageBitmap(bitmap);
}catch (OutOfMemoryError error){
error.printStackTrace();
}
}
}
package com.loaderman.customviewdemo;

import android.content.Context;
import android.graphics.*;
import android.util.AttributeSet;
import android.view.View; public class LinearGradientView extends View {
private Bitmap mDestBmp;
private Paint mPaint;
public LinearGradientView(Context context) {
super(context);
init();
} public LinearGradientView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
} public LinearGradientView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
} private void init(){
mPaint = new Paint(); int width = 500;
int height = 300;
mDestBmp = Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(mDestBmp);
Paint paint = new Paint();
LinearGradient linearGradient = new LinearGradient(width/2,0,width/2,height,0xffffffff,0x00ffffff, Shader.TileMode.CLAMP);
paint.setShader(linearGradient);
canvas.drawRect(0,0,width,height,paint);
} @Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas); canvas.drawBitmap(mDestBmp,0,0,mPaint); mPaint.setColor(Color.RED);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(5);
canvas.drawRect(0,0,mDestBmp.getWidth(),mDestBmp.getHeight(),mPaint);
}
}
BmpBlankGradientActivity布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.loaderman.customviewdemo.BmpBlankGradientActivity">
<com.loaderman.customviewdemo.LinearGradientView
android:layout_width="match_parent"
android:layout_height="match_parent"/> </LinearLayout>

创建Bitmap之Bitmap静态方法使用示例的更多相关文章

  1. java 添加一个线程、创建响应的用户界面 。 演示示例代码

    javajava 添加一个线程.创建响应的用户界面 . 演示示例代码 来自thinking in java 4 21章  部分的代码  夹21.2.11 thinking in java 4免费下载: ...

  2. Android -- 重置Bitmap大小&&Bitmap转角度

    重置Bitmap大小                                                                           Bitmap bitMap = ...

  3. 创建一个RAS 非对称 公私密钥示例

    static void Main(string[] args) { RSAParameters pub; RSAParameters priv; using (var rsa = new RSACry ...

  4. SQL Server 中创建数据库、更改主文件组示例

    以下示例在 SQL Server 实例上创建了一个数据库.该数据库包括一个主数据文件.一个用户定义文件组和一个日志文件.主数据文件在主文件组中,而用户定义文件组包含两个次要数据文件.ALTER DAT ...

  5. Robot Framework - 4 - 创建和扩展测试库的示例

    创建和扩展Library的示例 示例:Check status on Linux OS 创建与使用library的基本步骤:           1--- library实现的内容和实现的方式     ...

  6. SpringBoot项目创建与第一个SSM项目示例

    本节介绍SpringBoot创建第一个示例SSM项目的完整过程,使用工具STS,与IDEA操作基本类似. 示例代码在:https://github.com/laolunsi/spring-boot-e ...

  7. Bitmap: 使用Bitmap作为绘图缓冲时设置抗锯齿

    android上绘图时常用的抗锯齿方法是: paint.setAntiAlias(true); 但是在以Bitmap作为绘图缓冲绘制时,绘制出来的Bitmap可能仍然有锯齿,此时可以在绘制开始前加上下 ...

  8. Angular CLI 创建你的第一个 Angular 示例程序

    第一步:安装 Angular CLI 你要使用 Angular CLI 来创建项目.创建应用和库代码,并执行多种开发任务,比如测试.打包和发布. 全局安装 Angular CLI. 要想使用 npm  ...

  9. python从命令窗口启动脚本 创建并写入内容到文件示例

    写入到文件示例: #!/usr/bin/env python3 from math import exp, log, sqrt import re from datetime import date, ...

随机推荐

  1. C语言面试题目之指针和数组

    说明:所有题目均摘录于网络以及我所见过的面试题目,欢迎补充! 无特殊说明情况下,下面所有题s目都是linux下的32位C程序. 先来几个简单的热热身. 1.计算以下sizeof的值. char str ...

  2. 【DevOps】在CentOS中安装DockerCE

    准备 安装好CentOS7,拥有root账号密码,使用客户端登录. 安装 启动进入root用户,复制以下代码执行即可 yum install -y yum-utils device-mapper-pe ...

  3. linux使用文本编辑器vi常用命令

    一:翻页 ctrl+u向上翻半页 ctrl+d   向下翻半页 ctrl+f/page up向上翻一页 ctrl+b/page on   向下翻一页 H光标移到当前页的第一个字符 M光标移到当前页的中 ...

  4. GoogLeNet网络的Pytorch实现

    1.文章原文地址 Going deeper with convolutions 2.文章摘要 我们提出了一种代号为Inception的深度卷积神经网络,它在ILSVRC2014的分类和检测任务上都取得 ...

  5. Mybatis-Plus 插件学习

    官方指南 1.逻辑删除 在相应字段上添加注解 @TableLogic private Integer deleted; 说明: 使用mp自带方法删除和查找都会附带逻辑删除功能 (自己写的xml不会) ...

  6. Hive窗口函数案例详解

    语法: 分析函数 over(partition by 列名 order by 列名 rows between 开始位置 and 结束位置) 常用分析函数: 聚合类 avg().sum().max(). ...

  7. RocketMQ的技术亮点

    高性能 存储原理 零拷贝 数据结构与存储逻辑 刷盘策略 长轮询PULL RocketMQ的Consumer都是从Broker拉消息来消费,但是为了能做到实时收消息,RocketMQ使用长轮询方式,可以 ...

  8. 使用集合方式注入IoC

    使用集合方式注入Ioc 1.创建类 //集合 private String[] arrays; //list集合 private List<Integer> lists; //map集合 ...

  9. 源码安装mongoDB

    1.安装启动 下载源码包,官方地址: wget http://downloads.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.4.22.tgz 解压 ...

  10. 验证账号密码是否为空 if格式

    当前台页面是否提示有没有输入账号密码时 这时需要验证 //验证账号是否为空 if(string.IsNullOrEmpty(zh)) { //为空 则提示输入账号 ObjToJsin.msg = &q ...